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
vote
2answers
312 views

Which programming technique can trim extra code generated by Dependency Injection?

Original Class class HomeController { function __construct() { $this->setPhpRenderer('Module'); $this->repository = new HomeRepository($id); $this->...
12
votes
4answers
2k views

Technical term to denote opposite of dependency injection?

This is more of a nomenclature (technical writing) rather than a purely technical question. I am trying to write a refactoring proposal (and get it assigned to myself) centered around expanding ...
4
votes
1answer
3k views

Interface with multiple default methods OR Interface + Class

In a code base I am "starting from" there are a handful of interfaces with many default methods. They look something like this: public interface HasXY { double getX(); double getY(); ...
4
votes
2answers
214 views

How to VCS a large-scale, “atomic” refactoring?

When migrating a project from one heavily-used library X to another library Y which is essentially equivalent but doesn't have the same API, it may be necessary to make a lot of changes. In the case I'...
4
votes
3answers
463 views

How do I refactor massive view controller

I have a view controller which has 5 different subviews, these subviews are changed according to some conditions, then there are functions which apply background color and add some minor aesthetics to ...
10
votes
5answers
420 views

Using different patterns for similar features

I'm the sole developer on a project that, like for any software project, may be taken by somebody else in the future. Let's say I used the pattern X to implement feature A. After developing and ...
3
votes
2answers
3k views

Is “Introduce Parameter Object” actually a good pattern?

I have a situation where I want to call a function which requires a number of parameters. This function is not called directly, it is called indirectly and the parameters are delegated several times. ...
1
vote
1answer
297 views

Refactoring Password Validation Utility

Given a password, I am trying to validate if it passes all validation rules such as Be at least 6 characters long Contain at least one letter (a-z or A-Z) Contain at least one number (0-9) ...
4
votes
2answers
1k views

Pure POJO refactoring Active Record Pattern using Repository Pattern, no ORM or DI frameworks for pedagogical reasons

I taught a course years ago, and then I used some JDBC exercises using what I now know is the Active Record Pattern. I would like to modernize the exercise by changing the Active Record Pattern so ...
1
vote
1answer
355 views

What's a good approach for refactoring code that starts looking like spaghetti?

Synopsis: I am working on a level editor for a game written using Unity, the game is actually a new engine for an old game. This is a +20 years old game and its data structures are by today standards,...
6
votes
1answer
215 views

Replacing Dynamic SQL w/ a Filtering Pipeline?

As part of a work assignment I've long dealt with a giant, dynamic SQL-creating stored procedure that's used for retrieving a bunch of inventory items. This stored procedure accepts a ton of ...
3
votes
1answer
184 views

Modifying the tests and refactoring duplicate code in test driven development

I am trying test driven development for the first time (test first development, actually). I wrote down my specifications, then alternated writing tests, then code, writing the code to pass the latest ...
2
votes
2answers
2k views

What to do, if I need non generic class use generic inside, but don't want pass down type arguments

Let's assume we have something like that: class Page{ Header header; Body body; Footer<TFooterModel> footer; //TFooterModel is a type of content (subcontrol in some sense) that ...
2
votes
1answer
78 views

Detecting massive classes that are good candidates for refactoring

Does anyone know of any tool that would help find classes that are taking on too much responsibility? E.g. By visualizing a file's size compared to the other files in the code base, it can be easy to ...
2
votes
2answers
229 views

Continuous Integration and a Massive Refactor

I'm currently re-factoring a part of our application at work. I'm normalising a data structure from a flat list of fields to a parent/child relationship. This has an impact on all layers in the ...
0
votes
3answers
1k views

Is it bad practice to ignore some parameters while implementing a method of a interface?

Should an interface method implemented by a class always use all parameters? By interface I do not necessarily mean actual interfaces, these could be other (abstract) classes which are extended, too. ...
1
vote
2answers
87 views

Refactoring to an inheritance hierarchy early on

I am building a management tool for recipes. Recipes have a bunch of data, including very generic stuff like an ID, tags, ratings and trivia. I currently only manage recipes, but i want to add support ...
0
votes
1answer
75 views

Recommended strategy for changing javascript UI table library?

tl;dr - On an existing angularjs grid library, am moving to another. Is it better to wipe out as much code as possible and in a sense code from scratch to the same featureset, or work around what's ...
0
votes
1answer
49 views

Separate context-dependent information from the core logic code

Say, I have a code file where all the core logic is located. In addition, I have some custom logic that is defined by some flags, like if (a && b && c) {} if (!e && f) {} ...
2
votes
2answers
672 views

Can a single Factory be used to create similar in purpose, but different in focus objects?

I am concerned about whether or not I am using FactoryMethod correctly. Background: I am refactoring legacy code and I have identified a pattern: there is a God Object that is created in memory,...
4
votes
2answers
120 views

Refactoring: Self invoking function instead of Extract method ?

I'm reading the 'Refactoring' book, and i'm at the "Extract method" technique: Original code: void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); ...
2
votes
0answers
155 views

Are there downsides to writing PDO/mysqli statements into my code directly vs using my own redirect wrapper methods?

TL;DR: To access DB functionality in my codebase I currently use a custom layer throughout the code. The layer needs an upgrade in order to begin accepting prepared statements, which it currently ...
186
votes
21answers
23k views

A large part of my code has a major design flaw. Finish it off or fix it now? [closed]

I am a high school student working on a C# project with a friend of mine with about the same skill level as me. So far, we have written roughly 3,000 lines of code and 250 lines of test code in a span ...
2
votes
2answers
328 views

Revisiting Fowler's “Public versus Published Interfaces” article with regard to versioning and microservices

In this short article Public versus Published Interfaces from 2002, Martin Fowler distinguishes between easily changeable "public interfaces" and harder to change "published interfaces": The key ...
20
votes
6answers
3k views

How to remove a function or feature when using TDD

In texts about TDD I often read about "remove duplication" or "improve readability" during the refactoring step. But what makes me remove an unused function? For example let's say there is a class C ...
55
votes
9answers
8k views

How to write unit tests before refactoring?

I've read some answers to questions along a similar line such as "How do you keep your unit tests working when refactoring?". In my case the scenario is slightly different in that I've been given a ...
6
votes
3answers
271 views

Is it better to try and implement incomplete code, or write your own?

Our application is a 6-year old legacy application made by out-of-house contractors. Occasionally we stumble upon the skeleton of something they put together but never implemented, and it causes us ...
2
votes
2answers
40 views

Refactor using CTI or create new Entities

I'm currently developing on a rather large e-commerce application that handles multiple different business-fields. Like every e-commerce application we have a Concept called an Order which has one or ...
11
votes
4answers
585 views

Should You Improve Code Quality Whilst Working on a Feature Branch

I really like this article about leaving the code/camp site in a nicer state than you found it - it seems like a practical approach in the real world to keep on top of code cleanliness. I also ...
1
vote
1answer
90 views

jQuery, deferrers and “if” conditionals

How could I simplify the following code? I suspect that it can be simplified by using the $.Deferred class, but I don't know how. if (condition) { $.get('url').done(function () { done(); ...
1
vote
2answers
89 views

Replacing buggy component with library: important to identify bug in old component before replacing?

Summary: component used to work and now doesn't, the regression happened 1+ years ago and we're not sure when, now I'm considering replacing the broken and poorly architected component with a mature ...
2
votes
2answers
422 views

Definition of Top-down Refactoring / Bottom-up Refactoring

In the context of refactoring, you sometimes come across the term "bottom up approach", which makes sense in a way that you change the smallest moving parts first when refactoring. However I never ...
0
votes
2answers
137 views

Proper design for function doing one job but involving many little tasks

I have a class in Python that its only responsibility is receive text, a font path, max font size and min font size and print the text on an image if it fits. I have a function named word_wrap() and ...
3
votes
2answers
289 views

How to stay DRY with return values

I have a bunch of repetitive C++ code that looks like this: // Compute finalOutput if possible. Return true if successful, false otherwise // finalOutput only holds a valid value if true is returned. ...
1
vote
0answers
28 views

Refactoring Code into a MVC Pattern under the Constraint of a Library God Object [duplicate]

I have 1000 LOC that I plan to refactor into different files and functions. The goal is to be able to test the business logic independently from the visual display. Hence, I am considering an MVC ...
4
votes
2answers
636 views

Define “Refactoring”

Everyone has heard this term thrown around, and most people, I would imagine, have a conceptual idea of the meaning. I, myself, am in that latter group. However, I feel that the definition Google/...
5
votes
3answers
444 views

How aggressively to change in-house smart pointer to unique_ptr?

I work on a large software product which, being over 20 years old, has a number of constructs which have been superseded by language updates. One of these is a home-rolled smart pointer template ...
1
vote
3answers
129 views

Should I keep “redirect only” methods in my Controller?

Controller: function indexAction() { if ($condition) $this->renumPosition($id); //LINE #1 } //Redirect only - function's sole purpose is to call another function function ...
2
votes
2answers
772 views

Refactoring my code that depends on a renamed library method

Say third party library Beautify renames one of their methods from beauty to makeBeautiful because they want the method to be a verb and by some lack of initial planning didn't do this from the start. ...
0
votes
1answer
518 views

Recommendations to organize code changes in GIT

I'm looking for an advice reflecting my point of view. First, I will explain my point of view. My point of view I'm working in a big company as a C++ developer, we are using GIT. I'm going to make a ...
2
votes
1answer
83 views

Refactoring a class which exposes way too much of its internals

I've got some legacy code (both in the sense of code that has no tests, like the definition given in Working Effectively with Legacy Code, and of relatively old code) that I have to extend. There is ...
2
votes
1answer
610 views

Initializing composite classes in Python

I am writing a Python system where I have a major class(PlayerModel) that has two another classes as members (StateModel and ActionModel). I was wondering, what is the correct way to initialize such ...
1
vote
1answer
292 views

Ruby: Abusing mixin

I'm currently working with some code that IMO has abused ruby's mixin features. Given that I'm new to ruby, I wonder if MO in IMO is correct or not. My primary question is what makes more sense (and ...
1
vote
4answers
178 views

How to name functions that use conditionals in refactoring [closed]

Consider this bit of code : private Norf foo(Baz baz) { // ... // Logic on baz // ... if (baz.color == Baz.BLUE) { // Do this thing } // ... // More logic // ...
7
votes
1answer
261 views

Is there a term for splitting a function into “prep” and “do” halves?

I'm revisiting old code, and I noticed that the main logic is in one method, which is longer than I'd like (~60 lines). So I split it, and there's a natural seam on which to do so: the first half ...
0
votes
2answers
83 views

Preparing to remove a tightly coupled service provider out of source code [closed]

I'm working on a huge legacy codebase that uses Bing Maps API as a service provider and I have got the task to scrap Bing which is the foundation of the software and has been referenced through the ...
3
votes
2answers
604 views

What are polymorphic messages? [duplicate]

I am reading Martin Fowler's 'Refactoring: Improving the Design of Existing Code'. I have not understood a section of the second chapter where Kent Beck describes the pros of indirection. One of the ...
6
votes
2answers
1k views

How does breaking up a big method into smaller methods improve unit testability when the methods are all private?

I'm presently reading Building Maintainable Software by Joost Visser and some of the maintenance guidelines they recommend include: A) each unit/method should be short (less than 15 lines per method) ...
16
votes
8answers
3k views

Refactoring a long method which is based on large number of switch cases [duplicate]

We are using Java as a backend development language. One year back, we wrote a method which uses switch cases based on Enums values. Since we are continuously adding enum members and according adding ...
28
votes
6answers
6k views

Is there a design pattern to remove the need to check for flags?

I'm going to save some string payload in the database. I have two global configurations: encryption compression These can be enabled or disabled using the configuration in a way that either only one ...