Stack Exchange Network

Stack Exchange network consists of 175 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
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [refactoring]

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

0
votes
2answers
166 views

Is Pull Up Method really a good idea?

My intuition tells me that moving duplicate/common codes among subclasses to the superclass (so superclass now have a new method) is a bad idea, but I have seen this technique used/described in both ...
0
votes
1answer
106 views

How can I vary the distance function in my code?

I have a simple design question. I have a tree based C++ class. It needs to use a distance function in order to calculate nearest neighbors. But the distance function is available from an external ...
2
votes
2answers
192 views

Java: Is reflection bad for AOP situations?

Imagine the following situation: 1. Consume a 3rd party WSDL, out comes thousands of classes. 2. There's an opportunity to optimize performance by using a custom paralleled implementation. 3. Wsdl ...
3
votes
1answer
111 views

Transforming a long-running operation into a step by step operation?

I am working on a video game in Unity and at some point I'm facing a tough problem to solve: The game freezes while loading level data. Let me lay down what's happening in that process that takes a ...
40
votes
6answers
6k views

What's the best way to handle refactoring a big file?

I'm currently working on a bigger project which unfortunately has some files where software quality guidelines where not always followed. This includes big files (read 2000-4000 lines) which clearly ...
0
votes
2answers
94 views

Does creating multiple REST services calling each other from a monolithic EJB application right approach to make them microservices later?

We are doing an architectural refactoring to convert a monolithic J2EE EJB application to Spring services. In order to do that I'm creating services by breaking the application against the joints of ...
32
votes
8answers
7k views

Does “variables should live in the smallest scope as possible” include the case “variables should not exist if possible”?

According to the accepted answer on "Rationale to prefer local variables over instance variables?", variables should live in the smallest scope possible. Simplify the problem into my interpretation, ...
108
votes
12answers
15k views

Rationale to prefer local variables over instance variables?

The codebase I'm working on frequently uses instance variables to share data between various trivial methods. The original developer is adamant that this adheres to the best practices stated in the ...
2
votes
2answers
238 views

Factory that returns multiple implementations of the same interface

A few developers and I are attempting to refactor a class that has grown too large. Currently this class is around 3K lines long. The goal of the refactor is to make the logic more maintainable. The ...
1
vote
2answers
80 views

Refactoring large comment blocks

I am working in a fairly old code base. In the code base they have the classic source control, gigantic code blocks at the top of the files and above the function definitions that have something like ...
31
votes
10answers
5k views

How to encourage a team to refactor [duplicate]

I am on a team of ~20 people that is part of a very large organization. We have been using the same codebase for ~5 years and have released multiple derivative projects from it. I've had some 1:1 ...
-2
votes
1answer
95 views

What's the best design pattern approach for introducing optional feature to current code base?

I had a class hierarchy with several classes that interact with each other. After introducing new feature that is optional (but it depends on external libraries) I have the following code (just ...
1
vote
1answer
55 views

What is the difference between a migration pattern and a refactoring?

I have recently come across the terms migration pattern and refactoring on the topic of migrating monoliths to microservices. Is there any real difference between the two terms, or can they be used ...
0
votes
1answer
134 views

Two reversed dictionaries or one dictionary with key and value lookup?

I have situation where in the Repository class I have a dictionary: Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<...
0
votes
0answers
32 views

Extending prototype

I am currently refactoring a project where I come across the following kind of code multiple times: propertyFunction: function (feature) { doSomethingOnyWithFeature(feature); ... } Methods, that ...
1
vote
0answers
101 views

What are the “gotchas” of refactoring code that is binary serialized? [closed]

I have a few classes that I have to refactor and my particular concern is that they are marked as Serializable. I know with 100% certainty that these classes are parsed out into a file and read back ...
1
vote
1answer
123 views

Should I break functionality out into new classes?

I currently have an object that is composed from several simple objects. Each simple object is used to render a particular portion of the larger object as a whole. Think of it as building a 3D ...
-1
votes
1answer
61 views

What goes into .env file?

Working on a huge application with lots of legacy code, I often encounter environment "checks": db_name = Rails.env.production? ? 'staging_db' : 'production_db' ... desired_processes_count = Rails....
0
votes
1answer
87 views

Getting file format by checking file header [closed]

I'm writing a program, part of which consists of determining if a given file is a PNG. Knowing that a file doesn't have to be named with its respective filename extension to be of a certain type, I ...
3
votes
3answers
144 views

refactoring function to have a robust design

i am having a simple app example here: say i have this piece of code which handles requests from user to get a list of books stored in a database. from .handlers import all_books from flask import ...
2
votes
4answers
297 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
132 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
140 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 ...
4
votes
3answers
151 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
75 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
386 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
4answers
783 views

TDD: Uncle Bob's rules versus classic cycle

Uncle Bob says in 'The Clean Coder', chapter 5 'Test Driven Development': You are not allowed to write any production code unless it is to make a failing unit test pass. You are not allowed to ...
0
votes
2answers
87 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
179 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 ...
-3
votes
1answer
129 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
156 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
72 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
147 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
95 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
239 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
611 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
206 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
519 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
85 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 ...
6
votes
3answers
1k 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
67 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
432 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
467 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
180 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
73 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
30 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
168 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 ...