Questions tagged [refactoring]

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

-1
votes
0answers
10 views

Refactor multiple “if” in C#

for (int i = lstReportCount - 1; i >= 0; i--){ if ( (input.ServiceTypes == "1" && lstReport[i].A == 0) || (input.ServiceTypes == "2" && lstReport[i].B == 0) || (input....
1
vote
1answer
189 views

Best pattern to solve problem where objects only differ in one attribute

As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
1
vote
2answers
94 views

Best practice when creating classes that are solely in charge of the creation of other objects i.e. factories?

I often find myself creating classes that I suffix with 'Factory'. These classes tend to be solely in charge of the creation of a specific class or set of related classes. However I've always felt ...
-1
votes
2answers
105 views

Common way to name methods with same name as desired variable? [closed]

I frequently find myself pulling up helper methods in order to make my code better, but end up with duplicate method names. Is there a standard way to name such methods without getting duplicate names?...
2
votes
4answers
199 views

Should opportunistic refactoring happen in a different branch to functional changes

I'm not talking about the usual refactoring of new code you do as part of a development. This is more Boy Scout Principle type changes. While I understand why they're all made in the same branch, it ...
128
votes
5answers
43k views

What are the key points of Working Effectively with Legacy Code? [closed]

I've seen the book Working Effectively with Legacy Code recommended a few times. What are the key points of this book? Is there much more to dealing with legacy code than adding unit/integration ...
7
votes
5answers
693 views

Refactoring: Replacing a temporary variable with a query. Is this code example bad practice?

Before I ask my question, I want to note that this is not my own code; it comes from the Refactoring Guru website: I often find myself using the code on the left, and I have one primary motivation ...
1
vote
2answers
107 views

Refactor API to return additional object

I have a c# library that sends requests to a number of HTTP endpoints and then tries to de-serialize the result into the specified type. An indicative method is defined as: public virtual async Task&...
-1
votes
1answer
166 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 ...
3
votes
1answer
136 views

Way to hide logic behind class for better readability of method and refactor class to follow SRP

I have an algorithm to create a version for an entity and then I save that version against below 2 entity: 1) Variant 2) Category interface IEntityVersion { string GetVersion(); } public class ...
1
vote
0answers
65 views

How to make view models more DRY? [closed]

When looking at the WPF view model code samples on the web/books almost all of them show same approach for the bind-able properties that are used for the binding and input in the UI and that often ...
2
votes
2answers
332 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 ...
39
votes
6answers
5k views

Why Isn’t SQL More Refactorable? [closed]

Everyone knows that new developers write long functions. As you progress, you get better at breaking your code into smaller pieces and experience teaches you the value of doing so. Enter SQL. Yes, ...
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 ...
0
votes
2answers
95 views

Are refactorings expected to change unit tests that cover the functionality that is being refactored?

Imagine that you are using a tool that automatically applies a set of refactorings of your choice. If the refactorings breaks the existing API for whatever reason (e.g. change method name, ...
1
vote
2answers
98 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 ...
0
votes
2answers
230 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
122 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
231 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 ...
41
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 ...
3
votes
1answer
117 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 ...
28
votes
8answers
7k views

How do you keep your unit tests working when refactoring?

In another question, it was revealed that one of the pains with TDD is keeping the testing suite in sync with the codebase during and after refactoring. Now, I'm a big fan of refactoring. I'm not ...
0
votes
2answers
101 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 ...
1
vote
1answer
58 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 ...
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 ...
15
votes
5answers
6k views

How to avoid excessive method overloading?

We have quite a lot of places in the source code of our application , where one class has many methods with same names and different parameters. Those methods always have all the parameters of a '...
3
votes
5answers
319 views

Does not testing internals entail diligent refactoring and/or rely on developer talent?

I'm not asking here what the arguments are for/against testing internal methods (though I'll restate some, and don't mind hearing others). My questions relate to the implications of only testing ...
1
vote
2answers
83 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 ...
14
votes
7answers
6k views

A defense for boilerplate?

To me, boilerplate code is obviously bad. However I've met a developer who displays resistance in any attempt to reduce boilerplate. I realized I didn't have a readily formed, well thought out ...
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 ...
-1
votes
1answer
99 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
4answers
870 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
1answer
189 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
108 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
62 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
185 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
4answers
14k views

What is good practice when inheriting static classes isn't possible/allowed

I am developing a project in C# and due to a design decision it is not possible to have a static class inherit another static class. But I have, in my opinion, a case where this would make sense. I ...
2
votes
3answers
149 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
314 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 ...
2
votes
4answers
167 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
155 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....
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 ...
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 ...
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
2answers
160 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&...
1
vote
3answers
185 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
164 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 ...