Questions tagged [refactoring]

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

Filter by
Sorted by
Tagged with
0 votes
2 answers
211 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. ...
user avatar
  • 124
1 vote
2 answers
110 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 ...
user avatar
  • 935
0 votes
3 answers
298 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 ...
user avatar
0 votes
0 answers
44 views

Refactor conditional multiple AND OR operations? [duplicate]

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 ...
user avatar
-1 votes
2 answers
139 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 ...
user avatar
-2 votes
1 answer
140 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 ...
user avatar
0 votes
1 answer
99 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,...
user avatar
  • 111
0 votes
1 answer
148 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 "...
user avatar
  • 147
0 votes
1 answer
49 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 ...
user avatar
1 vote
0 answers
137 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 ...
user avatar
0 votes
2 answers
161 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 ...
user avatar
2 votes
2 answers
316 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 ...
user avatar
1 vote
2 answers
131 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)...
user avatar
  • 195
-2 votes
2 answers
152 views

When is there a case for refactoring large parts of a codebase at once? (if ever)

I work with a specific engineer (let's call them X) that, even though they have many years of experience in the industry, has a lot of trouble to manage the scope of code changes. For example, say ...
user avatar
0 votes
1 answer
109 views

Refactoring business logic out to private methods and testing

I am currently working on a project where there is a class ContactMonitoringJob a Quartz IJob class​ which in brief keeps pulling from a database and checks if any new changes have been introduced ...
user avatar
  • 59
4 votes
2 answers
262 views

Should I use classes instead of functions with a state needed for computation?

I have implemented the cows and bulls game in C++. The code: #include <cstdio> #include <cstdlib> #include <ctime> struct DigitMatches { int matches_in_right_positions; int ...
user avatar
  • 51
0 votes
2 answers
108 views

What is the best practice for temporarily pairing an attribute with a class

Consider a case, in which a class needs an attribute only in a very limited context. (For example, only during initialization, which is handled by a factory) I do not want to include such an attribute ...
user avatar
12 votes
6 answers
4k views

Is it bad practice create "alias" variables to not use globals or arguments with long names in a method?

Is it OK to create a variable which only purpose is to increase readability? Example: public class Example { private final HowLongCanARepositoryNameReallyBeRepository ...
user avatar
1 vote
3 answers
233 views

Refactoring a class to an abstract base class - keep default implementation in place or move it to an an other class?

I have a class which aggregates some temporal data from a database and provides a bunch of methods to query said data. It looks like this: public class InfoProvider { public IEnumerable<...
user avatar
4 votes
3 answers
367 views

Should I make my class mockable by marking its methods as virtual or by creating an interface?

In the process of refactoring non-testable code we are re-designing some of ours classes so that it can be easily replaced by a stub or mock during unit tests. Here is an example of such class (...
user avatar
  • 347
0 votes
2 answers
68 views

Restructuring a “processing slip” like object used in many steps

I have worked on a library which processed data through multiple steps. It was written in R, a dynamic programming language where one could just add fields to existing “objects”, which were just ...
user avatar
1 vote
4 answers
109 views

Should I public method and test it instead of testing a caller

I have a method called GetValueA() and GetValueB() which return string that in formatted public string GetValueA(){ string a = service1.GetA(); return Format(a); } public string ...
user avatar
1 vote
2 answers
123 views

Anemic Domain Model VS coupling and dependencies- design conflict - need an Object oriented advice

on design stage , after finding my main objects , I am always find my self writing "manager" or "controllers" classes in order to handle the connection between them. some of the ...
user avatar
0 votes
4 answers
244 views

Change architecture design to API to reduce coupling

I was reading articles about TDD and I found the following one. https://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html In order to answer this question I think someone should ...
user avatar
  • 17
-1 votes
1 answer
87 views

Optimizing methods with multiple if checks on getter values

I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is ...
user avatar
  • 173
2 votes
2 answers
267 views

What methods can be used to simplify mathematical expressions?

For fun, I would like to design a mathematical expression evaluator, with variables. It could simplify the entered expressions by factoring them, reducing them to a sum of simple fractions, ...
user avatar
  • 219
2 votes
1 answer
151 views

Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?

I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case ...
user avatar
  • 173
-1 votes
1 answer
269 views

rails, how to refactor nest if statements? [duplicate]

I'm working a project where I found another developer wrote a method as you see it below. How would I clean those IF statements and refactor this method. Also is it ok ti set a variable to nil ? def ...
user avatar
  • 117
-2 votes
3 answers
153 views

How to determine if a module within an application is worth refactoring or rewriting?

We all know Joel Spolsky's famous article to never rewrite working code. How about if we don't consider overall program but a module within? Module can be e.g.: payment handling microservice in e-...
user avatar
41 votes
8 answers
6k views

Getting buy-in for cleaner and more structured code

I am at my company for half a year now and think that I have gotten a fair idea of their codebase. Initially I didn't dare to form strong opinions, but now I start to feel that the code could benefit ...
user avatar
2 votes
1 answer
124 views

Resolving lambdas that depend on each other

In the C++20 codebase that I work there are a couple of functions which have helper functions defined as lambdas. I can see the idea of locality for these helpers. By having them there in the function,...
user avatar
1 vote
2 answers
432 views

No trivial god-class refactoring

Consider you have the following code: class UserContainer { List<User> user; //some methods to get specific users, for example users, which are higher than 1,70meters } The User have a ...
user avatar
0 votes
2 answers
290 views

Good design for a class with multiple methods to test but one public method

I've been trying to refactor some existing code which is in essence a giant nested procedural call inside what should otherwise be an object oriented architecture. The entry point to the relevant code ...
user avatar
  • 23
7 votes
4 answers
586 views

Use local or nested function for readability?

I found in the catalog of Refactoring by Martin Fowler, with Kent Beck book that they mention Extract Function refactoring. It is a good practice to wrap your related code into local functions to ...
user avatar
  • 179
0 votes
4 answers
322 views

Refactoring two methods with similar for loops containing some unique logic

Please help me with the following code refactoring, I spent so much time researching it and there's still no clear answer on how to approach it. Any help is greatly appreciated! So, given 2 methods (C#...
user avatar
  • 11
3 votes
3 answers
783 views

Use inheritance to make single child, smaller

We have a relatively big class. One of my colleagues thinks we must split this class into a base & child class to make it smaller and cleaner. On the other hand, I believe when we know this parent ...
user avatar
1 vote
2 answers
183 views

How to determine that a team is not doing enough refactoring?

I'm working my way through Refactoring: Improving the Design of Existing Code by Martin Fowler and I came across the following passage (2nd ed, p. 55, emphasis mine): To a manager who is genuinely ...
user avatar
  • 129
3 votes
0 answers
100 views

Refactoring nested if-else statement [duplicate]

I have an app where every object is checked based on various types of business rules. For this, i used multiple nested if-else statement which was done in one class. I am not happy with this situation ...
user avatar
0 votes
2 answers
486 views

What is Insider Trading Bad Smell?

In Martin Fowler's refactoring book, there is a code smell named "Insider Trading" which I don't understand: Software people like strong walls between their modules and complain bitterly ...
user avatar
1 vote
1 answer
118 views

C# - Menu Implementation - Help writing it in a testable way

The Question: I'm developing an app in C# and am currently designing a menu which contains submenus and needs to be different depending on the category of the user. I've written the code but am ...
user avatar
  • 149
1 vote
1 answer
64 views

Refactoring similar integration service code block

Context: Creating a "middleware" between 2 services I have to Get data from Service Source and get it into Service ERP. There is a multiple type of Data: A, B, C. An integration process ...
user avatar
  • 119
0 votes
1 answer
126 views

Event sourced persistence and data model refactoring

I've been working on a system that is using event-sourced persistence for it's data model. It is using a standard model of sequence of events with aggregates being rehydrated from those events and ...
user avatar
  • 35.6k
0 votes
1 answer
70 views

Refactoring powershell codebase to support multiple environments

I come from a Java background, but now I'm supporting a PowerShell code base. In the PowerShell code base there are multiple modules that were written against a certain Azure environment. Now, we ...
user avatar
5 votes
6 answers
446 views

Structuring a long sequence of operations

Imagine a production line - say, building a car. Starting from the beginning, a long, defined sequence of operations takes place, and at the end a car comes out. At various points along the way, ...
user avatar
19 votes
9 answers
4k views

Which object should have the method?

I am trying to create an object model for a user and a chatroom. I'm stuck on where to place certain functionality when the objects collaborate. At the moment all the functionality for the User is ...
user avatar
-2 votes
3 answers
175 views

Code optimization tool [closed]

Would it be possible to develop a program that could optimize algorithms, detect design patterns and then rewrite the code? For example: Detect a Bubble sort and transform it into a Quick sort or a ...
user avatar
3 votes
4 answers
354 views

Isn't refactor on an existing and fairly new module a normal practice in software product development?

Currently when working in a scrum team in a software service company, who has a clean business domain. I am working on adding new features to an existing module. As the existing module is legacy, and ...
user avatar
  • 1,487
3 votes
1 answer
276 views

Are there any C++ command pattern implementations which make it easy to add additional functions and their parameters

For my situation I have a device receiving string commands such as turn_on, pump, etc. Some of these commands also have arguments attached to them such as an int. I have methods like this void turn_on(...
user avatar
96 votes
9 answers
18k views

Is it the correct practice to keep more than 10 years old spaghetti legacy code untouched without refactoring at all in big product development?

I have been in two software product houses for three years in a row. The first is a small company maintaining a fairly small management system with a monolithic legacy code base (almost twenty years). ...
user avatar
  • 1,487
5 votes
7 answers
457 views

If an entity of the business changes its name, should there be a refactoring?

This question comes when there are changes during development and some entity of the business changes its name or its purpose. When it comes the time to develop, it can get very confusing during ...
user avatar

1
2 3 4 5
15