Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [refactoring]

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

2
votes
4answers
170 views

is it worth rearrange if else structures to shorter form [duplicate]

In our project there are deep and lengthy if-else structures which can be significantly shortened by a simple rearrangement. To me the shorter version would be easier to understand also. As an ...
25
votes
4answers
3k views

Does it ever make sense for a refactor to end up with a higher LOC? [closed]

Are there cases where more verbose code (as in more logical statements) is more clean than more concise code?
9
votes
4answers
405 views

Replace Conditional with Polymorphism in a proper way?

Consider two classes Dog and Cat both conforming to Animal protocol (in terms of Swift programming language. That'd be interface in Java/C#). We have a screen displaying a mixed list of dogs and ...
1
vote
1answer
79 views

Change request of a behaviour implemented deep in the call stack but is configured in the first calls

Problem: I have a complex system with many layers of abstractions. I need a different behaviour low in the abstractions, but to be configured high in the abstraction. Solution 1: Having a parameter ...
4
votes
3answers
229 views

How to handle foreign key constraints when migrating from monolith to microservices?

My team is migrating from a monolithic ASP.NET application to .NET Core and Kubernetes. The code changes seem to be going as well as can be expected but where my team is encountering a lot of discord ...
2
votes
1answer
61 views

Refactoring of a client calling a Rest Service

I have a class that is responsible for calling a service and returning a response. The contract is like this interface ServiceClient { ServiceResponse process(ServiceSubmissionParams params); } ...
3
votes
3answers
169 views

Abstract Parent and Inheritance vs. Strategy Pattern and Composition

I'm currently refactoring a class that looks (after some refactoring and very much simplified) somewhat like this: class Foo { public: Foo(bool someFlag) : m_flag(someFlag) { }; void ...
-1
votes
2answers
278 views

How to refactor code with if statements which scattered everywhere in the code?

I have a serious problem with application which i working on. It has a lot of procedural code and now it needs to refactoring now.. The problem: we have two different application in general, but they'...
2
votes
6answers
582 views

How to avoid code duplication in if else condition?

How can I avoid duplicate use of doSomethingCommon() in the following block of code? doSomething(); if (conditionA) { doSomethingSpecificToConditionA(); doSomethingCommon(); } else if (...
1
vote
1answer
146 views

Remove duplicated switch statements caused by not knowing type at compile time of opencv matrix?

I have a situation that occurs 5 times in my code, and continues to grow. Basically I have the same switch statement structure every time I need to perform an operation that involves my matrix and ...
1
vote
2answers
139 views

Refactoring a class which has many members

I have to make changes in a class which has many members, which are set by a function which implements some algorithm, and then are read by some other function which implements some other algorithm. ...
0
votes
1answer
51 views

How to factor apache httpd configuration?

I have a php application my_application, that needs to be ran in three specific ways (namely, dev, test, and prod) by httpd. That very application is deployed many times, one for each client (with ...
1
vote
2answers
27 views

Should I move my pricing service and creation of entity into the `SaveLineItem` service?

This is an MVC design/refactoring question. I am using a Controller and inside I do these tasks: I get Price of an object from a database, for which I use a repository pattern Create a LineItem ...
0
votes
3answers
152 views

Unit Testing method with multiple steps

I have a validation method like this def validate(a, b, c, d, e, f): Boolean = { val rs1 = check1(a, b) val rs2 = check2(c, d) val rs3 = check3(e, f) rs1 && rs2 && rs3 } I ...
0
votes
1answer
155 views

Refactoring bad code switch statment without distort everything

Once in a while I’m stumbling on switch statements during a Code Review session. I would like to find a more elegant way of this code .... $istannceOfClass = $repository->loadFoo(); switch($...
0
votes
0answers
50 views

Refactoring of unfamiliar system [duplicate]

A couple of weeks ago I got a new position of Head of Development. I've never worked in that company before, so I'm completely unaware of a domain, how the code is organized, what it looks like and so ...
5
votes
4answers
972 views

Refactoring many else if, else if, else if, etc. statements

I'm trying to parse readable data from PDFs and I keep ending up writing code like the following: if (IsDob(line)) { dob = ParseDob(line); } else if (IsGender(line)) { gender = ParseGender(...
0
votes
1answer
55 views

Refactoring code which uses a global list and updates list based on messages from an external system

I am refactoring some code where a globally defined list is updated based on messages from an external system. Currently there is a global instance of the list. Various messages update the list ...
10
votes
4answers
620 views

Good code style to introduce data checks everywhere?

I have a project that is sufficiently large in size that I can't keep every aspect in my head any more. I'm dealing with a number of classes and functions in it, and I'm passing data around. With ...
5
votes
1answer
110 views

Split ORM module without creating cyclic imports or side-effects

Preface I have an ORM library for a relational real estate database. The used framework is peewee. The library contains ~60 models, each representing a distinct table. The models represent a (pre-...
0
votes
1answer
106 views

Refactoring methods to access a database

I have many classes that retrieve databases objects. I have implemented their methods as static, so they can be called like this: class xxxDB { public static method1(Connection dbcon) { //......
2
votes
0answers
50 views

How to interface with very badly written code in Python? [duplicate]

I have to extend some very badly written Python code (no documentation, very interdependent, barely any encapsulation, very static, everything hard-coded, etc..) and therefore do I obviously have to ...
0
votes
1answer
179 views

Passing 0's (literals) to a constructor

I have a function that creates a new object by passing to it's constructor integer ids primarily with values of 0. This function is called when saving a newly created record (not an edit). public ...
8
votes
5answers
597 views

Long method refactoring: leaving as is vs separating into methods vs using local functions

Suppose I have long method like this: public void SomeLongMethod() { // Some task #1 ... // Some task #2 ... } This method doesn't have any repetitive parts that should be moved to ...
0
votes
4answers
257 views

How to prove business benefit in software refactoring? [duplicate]

By these days I work in a legacy code that uses JAVA. The project has a code tightly coupled and low cohesion. All my teammates suffer to make changes in the software, one reason is that we haven't ...
4
votes
3answers
208 views

Is there a name for iterating between refactoring and feature development?

I'm working on a personal project and I've found myself switching between doing some refactoring for a day or two and then some feature-adding for a few days. I like it, and I noted it was similar to ...
0
votes
1answer
69 views

Is raising an unrelated exception for convenience acceptable? [duplicate]

Let's say I have a function that returns a dataset. First it tries to read it and if that fails it is requested from an API and then written: def get_dataset(): try: df = pd.read_csv('...
0
votes
1answer
299 views

Refactor Django application - split monolith into REST API and frontend

I am in the process of refactoring a Django web app. It is written in the usual MVT style, and I would like to change this for a REST + frontend approach. In my first iteration I would like to do as ...
10
votes
6answers
645 views

How do you avoid endlessly iterating through equally sub-optimal designs?

So probably like many, I often find myself running into headaches with design problems in which, for example, there is some design pattern/approach that seems to intuitively fit the problem and has ...
0
votes
0answers
21 views

Dataframe function applied multiple times to different columns

I currently extract a data frame from CSV and create dummy variables for categorical columns. This seems to work fine, but looking for a way to improve my code readability. I thought of having a set ...
2
votes
1answer
111 views

Recommended methodologies for refactoring a large ISAM based DB structure to a RDBMS?

I'm meeting quite a challenge. We have an old grown software with mostly Delphi applications, and an underlying ISAM database server (ADS)1, used with a lot of free tables and manually programmed ...
3
votes
2answers
84 views

Should one create shareable private class member or keep variable in method scope to pass it as a second method argument?

Recently I had to refactor some legacy code. As in most cases, I had to split big parts of code into smaller, cleaner and readable functions. I ended with many functions, that had multiple, weird ...
-3
votes
3answers
93 views

How to refactor bunch of init methods in onCreate() method?

I create copy of this post here because this place more relevant for my question and I don't remove original question from stakOverFlow because I got comments and start conversations with users. I ...
4
votes
1answer
517 views

How do I refactor a Data Class to not be one?

We recently upgrade to PMD 6.0.0 and are getting several classes flagged as "Data Classes"? It argues that it breaks encapsulation and makes for brittle design (I understand that this site has a ...
1
vote
0answers
108 views

How to decouple a message broker and its message handlers?

Project description: I have a nodejs project where I try to build a bluetooth mesh like network, each node consists roughly of 4 parts the frontend part, the message broker, a bluetooth client and a ...
3
votes
2answers
172 views

Variable renaming throughout solution will produce lots of noise in git blame. What to do?

I inherited 1 million lines of C++ legacy code. Throughout the code variables like bCPCHAR bPCHAR, bCPDOUBLE and bPINT are used. They are defined like this: bCPCHAR: const char * bPCHAR: char * ...
1
vote
4answers
180 views

How to refactor this legacy code snippet to make it extensible? [closed]

I've come across a method resembling the below snippet. public void process(Data row) { Value value1 = row.getValue1(); Value value2 = row.getValue2(); boolean saved = false; if (...
1
vote
2answers
157 views

Is there a design pattern that I can use to simplify lots of nested methods

I'm trying to refactor some legacy code that's quite a mess. We're talking a class with 7000 lines and a handfull of methods that are violating the Single Responsibilty Principle so much it hurts to ...
2
votes
1answer
189 views

How do I triage a website effectively?

I've inherited responsibilities for maintaining a section of enterprise intranet, and my manager has tasked me with, "Clean up the code, please. We're probably going to get lots of change requests ...
12
votes
5answers
624 views

Is it useful to mini-refactor code in hopes of improving quality, or is it just “moving code around” without much of a benefit?

Example I came across monolithic code that does "everything" in one place - loading data from database, showing HTML markup, acting as a router/controller/action. I began applying SRP moving ...
-1
votes
2answers
391 views

Is it a good idea to create one git branch for each issue/story/bug?

We are a bunch of developers, without leader and I think we have a problem with project management at a fundamental level. When we got tasks (related to story or error), we have two options: Create ...
4
votes
2answers
231 views

How to approach this legacy Java project? [duplicate]

Edit: Assume that I will stay on this project until "the end". The Problem I'm currently working on a legacy interesting Java project(s).  A full rewrite is currently out of the question, as I am ...
1
vote
3answers
409 views

Legacy code: what to do in regards to maintenance and moving forward? [duplicate]

I am currently working on an application with legacy code that was built using proof of concepts (POCs). These POCs became the finished production-ready code, there were no tests, and the classes have ...
-4
votes
2answers
173 views

Term for refactoring looping logic for an event loop

Constrained by a main loop outside of my control (eg: UI event loop, game loop, etc...), I have a slow (relative to the main loop's expectations) algorithm involving a loop. Thus, I have to ...
8
votes
4answers
381 views

What is the value of writing extra unit tests when re-factoring a larger function into smaller functions?

If I have a complex unit tested function: def do_everything(): # turn twizzles # push buttons # move mountain And I re-factor it into some smaller units: def do_everything(): ...
2
votes
8answers
677 views

Refactoring for loop (clean code aspect)

I have to refactor a "for" loop which iterates over an array and does two independent things with the array element. For example: doThisOrOtherStuff(array) { for(int i=0; i<array.length; i++) { ...
1
vote
1answer
90 views

Sharing common code when re-writing an application

I have an application 'A' that was over engineered using Amazon SWF to do jobs asynchronously. We are re-writing it to app 'B' to keep it simple and do the same jobs synchronously without using SWF. ...
1
vote
1answer
74 views

Refactorings of larger types

This is a sweet spot thing for me. But I am not sure what rules others apply to the challenge. In this codebase I have many large types. Mostly types called *Service. Large types are cool. But I tend ...
0
votes
0answers
82 views

Efficient way to handle screen switching in JavaFX

So, JavaFX has the concepts of Scene, Stage, root (Parent), and such. I was trying to write a kind of Director class for my application that I could simply pass a string representing the FXML file ...
1
vote
1answer
280 views

Refactoring god-class leads to feature envy [closed]

I'm trying to refactor a 2.5KLOC god-class (with about 68 data members and 62 member functions) that performs a wide variety of text formatting and layout operations. After a careful analysis based on:...