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
225 views

Clean code vs clean git/jira history

I started reading the book Clean Code by Robert C. Martin and at the start I found this idea of his interesting, "Leave the code cleaner than you found it" adapted from the "Leave the campground ...
0
votes
1answer
93 views

How to split my product into multiple variants

I have a product which is a combination of mobile apps and webapi . I am working web API. The webapi part created earlier and it build with layered model. Even thought this does not have a business ...
2
votes
4answers
100 views

Putting extra code review burden on team because of refactoring

I am reading Refactoring by Martin Fowler, and working in the patterns suggested into my coding. This has caused me to put up two code reviews for my service which are significantly lengthy, which are ...
3
votes
3answers
144 views

Is checking if something exists responsibility of a caller or an API?

Is it better for an API when it provides additional methods that check if something exists before adding? For example: if (!userManager.HasPermission(user, "somePermission") userManager....
2
votes
1answer
70 views

Is it common for seemingly small feature additions to involve changing many functions?

I wrote a binary calculator & binary teaching program in the Rust programming language. I was taking an iterative approach in that I decided to first get a basic "learn binary" game started. This ...
7
votes
2answers
364 views

Class that returns multiple unrelated values, are there better way to accomplish this?

I have a "tracker" class. This class tracks objects from a given input image. But in addition to this, there is another parameter that corresponds to a value used in generating the image. The ...
0
votes
2answers
72 views

Apply Single Responsibility Refactoring in Form App

I really don't understand the SRP applied to a "real" project (really not so real but can help me to know your approach) I find very difficult to divide and assign responsibility to my classes. So I ...
1
vote
3answers
162 views

how would you plan monthly refactoring days to eliminate technical debt in your team?

My team (yet) did not do much to work on technical debt and our code base includes huge amount of TODO code, unused code, 'could get better' code, @Ignore unitests and more. I am about to start ...
-4
votes
1answer
96 views

Renaming ⊈ refactoring?

Is renaming variables a type of refactoring, or is refactoring something different? In other words, what's the difference between renaming and refactoring? I thought they were the same thing. "Code ...
1
vote
2answers
142 views

Encapsulation and input validation duplication

Consider the following example (very simplified): public class Basket { private readonly List<BasketItem> _items = new List<BasketItem>(); public IReadOnlyCollection<BasketItem&...
0
votes
3answers
63 views

Nesting Objects without Passing Arguments?

I have a couple of classes that I've nested together (not in the sense you may think so bare with me), to create what I call a prefab object (similar to Unity's prefab system). In this particular ...
0
votes
2answers
112 views

Strategy to review all occurrences of specific field in large project

The project I am working on has a status field which currently only allows YES or NO, and I am about to refactor it to allow the value MAYBE. Unfortunately, it is used over 1,000 times in 200+ files. ...
-1
votes
1answer
90 views

Downcastings refactored using generics

I am willing to refactor some code that acts like a controller class executing work embedded in other classes. On one side it looks good as the controller is generic and what changes has been well ...
4
votes
3answers
209 views

Making pull requests easier to review while practicing XP and refactoring

I practice TDD and refactor aggressively. Recently some colleagues have complained that the resulting pull requests are difficult to review, or would require a lot of regression testing. When I ...
9
votes
4answers
579 views

Refactoring - is it appropriate to simply rewrite code, as long as all tests pass?

I recently watched "All the Little Things" from RailsConf 2014. During this talk, Sandi Metz refactors a function that includes a large nested if-statement: def tick if @name != 'Aged Brie' &&...
2
votes
4answers
201 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?
10
votes
4answers
437 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
83 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 ...
5
votes
3answers
438 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
63 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
220 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
335 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
734 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
154 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
140 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
64 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
28 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
159 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
173 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
1k 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
627 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
140 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
110 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
180 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
729 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
280 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
210 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
73 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
338 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
650 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
118 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
96 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
690 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
109 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 ...