Questions tagged [refactoring]

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

9
votes
3answers
2k 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
73 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
532 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
537 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
1k 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
191 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
141 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
75 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
32 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
178 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 ...
-1
votes
1answer
252 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($...
5
votes
4answers
3k 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
651 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 ...
4
votes
1answer
164 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
128 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
53 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 ...
1
vote
1answer
182 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
1k 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
353 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
219 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
98 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
460 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
671 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 ...
2
votes
1answer
138 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
90 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
111 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 ...
5
votes
1answer
1k 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
125 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 ...
2
votes
2answers
189 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
212 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
167 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
196 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
642 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
720 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 ...
5
votes
2answers
395 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
641 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 ...
-5
votes
2answers
182 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
387 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(): ...
3
votes
8answers
1k 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
92 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
77 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 ...
1
vote
1answer
377 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:...
0
votes
5answers
126 views

What should I consider when adding a condition to an existing function? [closed]

I often find that I need to add new conditional feature to code that already exists. This requires me to keep the existing functionality while tacking on something new. I've tried each of the ...
2
votes
3answers
557 views

is it ok that development team create refactoring tasks in the product backlog?

I'm aware of software development principles and best practices. However, in most cases I entered a project that was already on its phase 2, and that already had a significant amount of code written. ...
2
votes
4answers
1k views

Refactoring large code base legacy C++ project that mixed with C style

Background: Currently I am working on a C++ project. It's for business use, so this project is not open sourced. However, this is a project with a fairly large code base (around 10k lines of code) ...
2
votes
3answers
177 views

Conflict resolution and merging order for the project code?

A team simultaneously made several refactorings (to raise system genericity) to the same project with some overlaps (yes, unfortunately, more like "big bang"). Code is spanning representation, ...
-1
votes
3answers
238 views

Refactoring - Resolve dependencies between legacy code used by third parties

I am developing two libraries in .Net Firs one it's a library with core functionality (named it Library.Core.dll) Let's focus in User class public class User { //set of constructors and ...
-1
votes
1answer
113 views

When should a team prefer to introduce a change incrementally instead of a complete refactor of the code?

I like to try out stuff in projects I am in. When something doesn't seem right to me, I like to implement a new thing, see if it fits or not for a while, and then, slowly implement it to the rest of ...
1
vote
1answer
239 views

Testing strategy for a messy codebase

On an entry level for a codebase you are not familiar with, how do you introduce testing, an at what levels ? When introduced to codebases with no tests at all and at the same time being tangled ...