Questions tagged [refactoring]
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
756 questions
2
votes
2
answers
304
views
How to design extensible data aggregation code to avoid manual updates in multiple places?
I have a data aggregation function that collects statistics from multiple sources. The problem is that whenever I add a new metric, I need to manually update the code in three different places, which ...
1
vote
1
answer
212
views
What is the difference between Vertical Slice Architecture and Feature-Based Architecture
I am refactoring my monolithic application, in which the code is organized based on layered architecture.
I want to implement Modulith (Modular Monolit) in my app, but I've run into a problem: I haven'...
18
votes
7
answers
4k
views
Is there a reason "replace conditional with table" isn't a standard refactoring?
I'm preparing a lecture where I will start with a many-branched conditional statement and replace it with a table. For example, I could start with:
function getMonthName(monthNumber) {
if (...
1
vote
1
answer
139
views
I have a system that's part of an event-driven architecture. Need some advice on refactoring
I have a system that was built 5 years ago. It's part of a multi-system event driven architecture. And each system is owned by a different team with varied responsibility.
When the system was ...
2
votes
2
answers
2k
views
Refactoring a legacy codebase with a god Repository and incomplete Clean Architecture [duplicate]
I'm currently working on a large legacy project that tried to implement Clean Architecture combined with MVVM, but unfortunately didn't fully adhere to the principles.
One major problem:
The ...
12
votes
7
answers
1k
views
Techniques to do continuous refactoring
I've come to believe that continuous refactoring is a good way to maintain the health of a system that is in continuous development. That is, a system for which new features are always in the pipeline....
4
votes
1
answer
297
views
Refactoring a codebase from manual memory management to RAII
Edit: @Ben Cottrell's comment said this was similar to a question about spaghetti code. While both questions involve large codebases, mine addresses a specific technical pattern: manual memory ...
2
votes
5
answers
2k
views
Rewrite or Transpiler - How to move away from a proprietary SAAS solution
We are using a Software as a Service platform that allows to create custom code which integrates in the platform and all its features (dialogues for common objects like Account, Customer, Address, and ...
3
votes
5
answers
1k
views
What is the root cause of a proliferation of "null checks"?
I work with a lot of Groovy code. One feature of the language is the "safe navigation operator" (?), which I think of as an inline null check. There are many things about Groovy that I like, ...
2
votes
3
answers
2k
views
Is refactoring scattered functions into a class always a good idea?
Straight to the point, here's the original code:
integer = input(...)
size = len(integer)
# Created three dicts for matching numbers as keys to corresponding words
dic_ones = {...}
dic_tens = {...}
...
0
votes
4
answers
291
views
Refactoring the application with correct SemVer approach
Suppose I have an application with version 1.2.3.
If I refactor my application to remove deprecated calls while maintaining the same functionality and API as in version 1.2.3, what version number ...
-1
votes
1
answer
621
views
ReactJS hook vs utility function
Should we use utility functions in react or should everything be components and hooks?
I had this scenario:
Utility function for formatting money.
const formatMoney = (value) => value == null ? '' :...
2
votes
3
answers
241
views
Is it still "feature envy" if the state to make decision or the action to take after asking the state involves other classes to participate?
According to https://softwareengineering.stackexchange.com/a/212130/432039, if a class asks another class for the state, and then call methods of that class, it is called "feature envy", eg:
...
2
votes
1
answer
158
views
Refactoring Processor classes
I am writing some python 3 bioinformatics software and was wondering about the best way to write it in an OOP format. I am pretty sure a lot of my classes are violating the SRP principle, but I'm not ...
0
votes
1
answer
228
views
Our php codes base has 6 different ways to do INSERT … ON DUPLICATE KEY UPDATE, how do I fix it?
Our php codes base has 6 different ways to do INSERT … ON DUPLICATE KEY UPDATE. It happened over years because the php framework is evolving and my team members come and go, although I won't say we ...
2
votes
1
answer
286
views
To hide side effects or not?
Only loosely related to the title.
In my code I often encounter functions such as this one (e.g. from a pet project – a Discord bot written in Python – but seemingly a ubiquitous and reoccurring issue)...
4
votes
5
answers
414
views
Does testing the public surface of a class test the behavior of code or the way it is written?
A follow up question to How do unit tests facilitate refactoring without introducing regressions?.
I said that integration tests test the behavior of the code, while unit tests, being tied to ...
0
votes
4
answers
229
views
How to eliminate repetition in three subclasses: each defines an identical method that does almost the same thing, but with a different return type
TLDR:
I have three subclasses, each inherits from the same parent class, each defines an identical method that does almost the same thing, except that each of these methods has a different return type....
0
votes
2
answers
391
views
What process to get away from a 10+ years old frontend?
there is an app with lets say, 75 years worth of development in it. The frontend is a bit a chaos, there is jquery, there is vuejs (vue 2 of course), there is a lot of server side rendering with over ...
23
votes
7
answers
6k
views
Sufficient conditions for refactoring
I've found that many codebases in large companies degrade over time, only adding new features and never improving the design of the codebase. Gradually, these codebases degrade to what is considered &...
0
votes
1
answer
140
views
Return response from controller or raise exception from service
I need some guidance on how to send error responses to client from WebAPI controller for an update operation. I need to check if data is changed and if it has duplicate data. I have service class that ...
1
vote
1
answer
247
views
Refactoring to nested functions inside methods (JavaScript)
I've been reading Refactoring (2nd) by Martin Fowler. In the first chapter, he shows an example of refactoring a function where he extracts other functions from it and places them inside that function ...
1
vote
1
answer
176
views
Monolithic Database to Microservices, but with Radical Data Model Changes
We are finishing up a long (over 1 year) project to retire our monolith. We have been moving pieces of functionality into microservices guided by domain driven design.
So far we have been leaving the ...
4
votes
3
answers
270
views
How do you handle instantiating a large number of interconnected component objects?
I'm currently refactoring our instance startup code, which is currently a 2,000 line mud method. The issue is that that method needs to create a large number (>50) of components/service objects, ...
5
votes
2
answers
2k
views
C#: Refactoring an oversized try/catch/finally
Recently I've come to discover that I've inherited one of the internal auxilliary programs used. I've made a few minor fixes and features to improve it in the past, but now I've been given a major ...
1
vote
3
answers
332
views
Refactoring a "filter" design
I need to refactor a class that became too messy. The class has a method that reads some raw data from one of our repositories, and another that "filters" it:
The data is in the form of ...
0
votes
0
answers
62
views
How to handle variables for simulation? One, or multiple class, or maps?
I am trying to write a simulation that has multiple (20+) variables, which the user should be able to control without modifying the source code. Currently I'm stuck between two design choices: Should ...
-1
votes
2
answers
264
views
4 Months of Code Rewritten in 2 Days Team Dilemma [closed]
I recently had a project that I took me 4 months to do , I hit some very significant bugs that where impossible to track down. I said this code is absolute garbage and started from scratch, drank a ...
1
vote
3
answers
223
views
Handling a Refactoring Project with Limited Access to Source Code
I recently finished an interview with a company as a web developer. I'm the first and only developer that is about to be hired in this company.
They have a web application that was created by a ...
8
votes
7
answers
1k
views
Is "avoid feature envy" violating "open closed principle"?
After reading
What is a" feature envy" code and why is it considered a code smell?"
I know the following code is suffering from "feature envy":
public class Garden{
...
3
votes
2
answers
485
views
When refactoring many functions that share state in to a single class, how can you avoid writing an "escort" class?
This well-known article criticises Java on the basis that it does not allow you to write functions that do not live in a class. This flaw forces you to write classes with names that look suspiciously ...
4
votes
6
answers
2k
views
How do I determine how long a refactoring effort will take?
After much hard work, I've convinced my manager that some absolutely awful code needs refactoring. As with any project, they've asked me for a time estimate and I've found myself stumped. How can I ...
0
votes
2
answers
383
views
How to refactor this tightly-coupled method and (mostly) preserve its encapsulation?
I have recently encountered this problematic method (minimal reproducible sample in C++ but this question aims to be language agnostic past the syntax):
void MyObject::twice_bind_cycle() {
_bind1()...
0
votes
4
answers
360
views
Object matching using generic method? [closed]
I have a piece of code where two objects (incoming request object and a profile object) are to be checked for matching conditions.
So the first method is as below where I check whether the profile ...
1
vote
2
answers
195
views
Refactor chunks of code where variables from one chunk are used in others
What is a good pattern to refactor a chunk of code where you can identify smaller chunks, but the results of these smaller chunks are used in other chunks?
An example should make things more clear. ...
1
vote
3
answers
532
views
Is in my case role based access better than permissions?
Hello StackExchange community, I'm in a bit of an impasse for my current project.
The software in question is a collaborative program designed to let employees work together on the platform and assign ...
13
votes
4
answers
2k
views
How should factored-out code be tested as part of the TDD refactoring step?
When in the refactoring step of a TDD process, if we "factor out" some common functionality from two (or more) code modules, how do we avoid that "factored out" code not being ...
1
vote
6
answers
1k
views
What are some considerations when deciding to fix a bug, or rewrite the code in question?
At some organizations I'm seeing a trend where if a bug is reported, engineers will directly rewrite the offending code without knowing the root cause. In general this will fix the bug, but in my ...
0
votes
2
answers
275
views
When is it practical to design in advance of refactoring?
I have a colleague that insists refactoring to make the code testable and introduce tests should be independent from changes in architecture as part of refactoring, e.g. introducing a factory pattern. ...
1
vote
2
answers
148
views
The name of this change/refactoring pattern
At first, we have a working function name foo
void foo() {
bar1(9);
bar2(9);
}
Noted that the value 9 is hard-coded. Then, we want to make a change so we can make 9 a parameter yet preserving ...
0
votes
3
answers
783
views
How to deal with an actively developed 20 year old legacy codebase [duplicate]
I know there are potential duplicates, but imo this is different because there are around 100 developers actively working on this nightmare.
I have unfortunately gotten into a job where this GUI ...
-1
votes
2
answers
282
views
Performance hit after non-functional refactoring
I was asked to refactor some C++ code recently for the purposes of increasing unit testing coverage. The problem was that the code was tightly coupled on one compilation unit, so we had the equivalent ...
-2
votes
1
answer
407
views
How Should I Go About Refactoring My Code With Dependency Injection? [closed]
I work on WPF applications used in testing hardware. I need to make my code base more testable and re-usable. I believe DI would help, but I'm not quite sure where to start. I've done my best with the ...
0
votes
1
answer
134
views
Correct way to deduplicate conditional statements [closed]
I'm facing with problem that in every function (with serves as service for endpoint) I need to check what is value of query parameter (mode). I need to check it on many callables, E.g.
def create(self,...
0
votes
1
answer
1k
views
Unit tests and mocking when functions arguments have changed?
I'm learning how to write unit tests on a project I'm doing with my friends. One thing we tried was to mock the return values of function calls if the functions are declared in a different "...
0
votes
1
answer
97
views
Better design for a REST import into web store
I have an import that needs to grab data from a REST service and import into an web store. It's basically an ETL type of service, but because the REST service can be slow and I don't want to call it ...
1
vote
0
answers
238
views
What design pattern does this implementation follow?
For a middleware solution I expose an API, which processes data and sends the necessary information to different parties.
In the current design we create a new client, which acts sort of like an ...
0
votes
2
answers
676
views
Debugging to track attribute change
I have a very large C++ application running on a *nix environment, which is occasionally setting a particular date attribute on a structure, way off what it should be. I know which structure and which ...
2
votes
2
answers
486
views
How to handle duplicate null checks
I'm working with a legacy codebase that has a lot of functions with a nullcheck on the same object. Example:
std::vector<SessionNode*> * SessionManager::SessionMap;
Session * getSession(int ...
1
vote
2
answers
221
views
Should I add functionality by adding a new method to a class - or should I "register" the new functionality into a data structure?
I have one large class that computes ~50 different metrics (each metric has no side effects).
My code is similar to this:
class ReportingMetrics:
def __init__(self, data:pd.DataFrame, config:dict)...