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

learn more… | top users | synonyms

81
votes
8answers
5k views

What is the most effective way to add functionality to unfamiliar, structurally unsound code? [duplicate]

This is probably something everyone has to face during the development sooner or later. You have an existing code written by someone else, and you have to extend it to work under new requirements. ...
31
votes
11answers
2k views

Techniques to re-factor garbage and maintain sanity? [duplicate]

So I'm sitting down to a nice bowl of c# spaghetti, and need to add something or remove something... but I have challenges everywhere from functions passing arguments that doesn't make sense, someone ...
31
votes
7answers
1k views

Code maintenance: keeping a bad pattern when extending new code for being consistent, or not?

I have to extend an existing module of a project. I don't like the way it has been done (lots of anti-pattern involved, like copy/pasted code). I don't want to perform a complete refactor for many ...
16
votes
8answers
1k views

When to refactor

I've read through most of Fowler's Refactoring book and have refactored many applications in my past big and small. One of the harder things I find to teach is "when" to refactor. I tend to do this ...
44
votes
21answers
5k views

How do you explain refactoring to a non-technical person?

(This question was inspired by the most-voted answer here) How do you go about explaining refactoring (and technical debt) to a non-technical person (typically a PHB or customer)? ("What, it's going ...
16
votes
7answers
1k views

How to justify code refactoring time?

Have a very large project more than 70k LOC. The project definitely needs some code refactoring in Core Framework and in other parts as well. There was NO time set in the beginning of the project for ...
15
votes
6answers
4k views

Refactoring Switch Statements and is there any real use for Switch Statements at all?

I was reading this article and was wondering, do we get rid of all switch statements by replacing them with a Dictionary or a Factory so that there are no switch statements at all in my projects. ...
11
votes
3answers
418 views

How do you break out of the role of a code maintainer? [closed]

In my last three jobs, I was a code maintainer. In all three cases, I was hired after the majority of the code for the project was already written. I'm a self taught programmer. Before I ever started ...
9
votes
7answers
580 views

Best supporting argument for refactoring [duplicate]

Currently I am working on a code best described as C code living in C++ body. However I haven't been able to convince power that be to re-factor on ground of ease of maintenance. What in your ...
8
votes
2answers
493 views

How do you call the process of taking a part of a function and making an individual function of it?

I know there was a technical term for this. I'm just can't remember what it was. If the title needs clarification, here is what I mean; If this is the old code: Result foobar(Param1,Param2,Param3) { ...
45
votes
6answers
2k views

Refactoring into lots of methods - is this considered clean or not? [duplicate]

So, I watched as my colleague complained a bit about a project he has inherited from someone who is, shall we say, not very experienced as a programmer (intern left to his own devices on a project). ...
24
votes
12answers
1k views

Colleague unwilling to use unit tests “as it's more to code”

A colleague is unwilling to use unit tests and instead opting for a quick test, pass it to the users, and if all is well it is published live. Needless to say some bugs do get through. I mentioned ...
21
votes
6answers
1k views

How do I prevent unknowningly duplicating code?

I work on a rather large code base. Hundreds of classes, tons of different files, lots of functionality, takes more than 15 minutes to pull down a fresh copy, etc. A big problem with such a large ...
3
votes
1answer
690 views

How to refactor a method which breaks “The law of Demeter” principle?

I often find myself breaking this principle (not intentially, just through bad design). However recently I've seen a bit of code that I'm not sure of the best approach. I have a number of classes. ...
62
votes
10answers
4k views

Does it make sense to write tests for legacy code when there is no time for a complete refactoring?

I usually try to follow the advice of the book Working Effectively with Legacy Code. I break dependencies, move parts of the code to @VisibleForTesting public static methods and to new classes to make ...
25
votes
10answers
14k views

How many lines per class is too many in Java? [closed]

In your experience, what is a useful rule of thumb for how many lines of code are too many for one class in Java? To be clear, I know that number of lines is not even close to the real standard to ...
11
votes
2answers
729 views

Adding unit tests to a legacy, plain C project

The title says it all. My company is reusing a legacy firmware project for a microcontroller device, written completely in plain C. There are parts which are obviously wrong and need changing, and ...
9
votes
5answers
1k views

How to approach refactoring an existing web application?

I've been reading and thinking a lot lately, and I've come to the conclusion that I maybe I should rethink my web development strategy. I'm doing a lot of on-the-fly programming, and in the 2 years ...
8
votes
6answers
851 views

What are the practical ways to implement the SRP?

Simply what are the practical techniques people use to check if a class violates the single responsibility principle? I know that a class should have only one reason to change, but that sentence is ...
32
votes
12answers
2k views

How to handle coworker with “obsessive refactoring disorder”

My coworker (who is very clever, but with severly limited inter-personal skills), keeps refactoring my code even when it is work in progress and assigned to me as a task. Whereas I fully subscribed to ...
9
votes
5answers
832 views

When should you rewrite? [duplicate]

Possible Duplicate: When is a BIG Rewrite the answer? In Joel Spolsky's famous (or infamous) article Thins You Should Never Do, Part I, he makes the case that doing a rewrite is always a ...
9
votes
3answers
740 views

Best Practices for Renaming, Refactoring, and Breaking Changes with Teams

What are some Best Practices for refactoring and renaming in team environments? I bring this up with a few scenarios in mind: If a library that is commonly referenced is refactored to introduce a ...
16
votes
7answers
1k views

To rewrite or slowly refactor old C++ project [duplicate]

Our team has recently inherited a relatively large project from another company (~250k lines). It was developed using C++Builder and we intend to port the Ui side to Qt. Most of the Ui code is ...
6
votes
1answer
342 views

Reasoning to wait until third time in the Rule of Three?

I just came across the article "Rule of Three" in wikipedia Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It ...
9
votes
8answers
444 views

Refactoring or upgrading databases to handle new features

Several responses to a database schema question, suggested an additional table to normalize a database for a feature that is not part of the current requirements (A UserDepartment table to allow for a ...
5
votes
3answers
1k views

Should I refactor a F class from code climate? [closed]

According to code climate (static quality analysis), a class is F, aka the worst mark you could be granted. I did submit a pull request to rearrange it here Which has been refused because the new ...
4
votes
3answers
473 views

How should I start refactoring my mostly-procedural C++ application?

We have a program written in C++ that is mostly procedural, but we do use some C++ containers from the standard library (vector, map, list, etc). We are constantly making changes to this code, so I ...
23
votes
15answers
1k views

How do I convince my team to use smaller classes/methods?

Disclaimer: I'm a newcomer (this is my third day of work), and most of my teammates are more experienced than me. When I look at our code, I see some code smells and bad engineering practices, like ...
13
votes
5answers
1k views

Using functions as a way to stylize [closed]

Have you ever broken up a large function into smaller functions knowing that those smaller functions will not be called by more than one caller? The primary purpose of a function is to promote code ...
3
votes
0answers
343 views

What is a good way to refactor a large, terribly written code base by myself? [duplicate]

Possible Duplicate: Techniques to re-factor garbage and maintain sanity? I have a fairly large PHP code base that I have been writing for the past 3 years. The problem is, I wrote this code ...
2
votes
1answer
864 views

Recovering an anemic domain model into a multitier architecture

I have spent the past several days learning about domain driven design and attempting to apply it to a current project. I decomposed the problem domain into the canonical logical components: domain, ...
31
votes
5answers
7k views

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

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 ...
38
votes
10answers
3k views

How can one best avoid writing bloated GUI code?

I find whenever I work with GUI code, the code tends to bloat faster then other kinds of code. It also seems harder to refactor. Whereas in other kinds of code I can refactor pretty easily -- I find I ...
21
votes
5answers
1k views

What is the politically correct way of refactoring other's code?

I'm currently working in a geographically distributed team in a big company. Everybody is just focused on today's tasks and getting things done, however this means sometimes things have to be done the ...
7
votes
6answers
1k views

What to do when you inherit an unmaintainable codebase? [duplicate]

Possible Duplicate: Techniques to re-factor garbage and maintain sanity? I've inherited 200K lines of spaghetti code — what now? I'm currently working at a company with 2 other ...
32
votes
17answers
2k views

Is it bad to have an “Obsessive Refactoring Disorder”?

I was reading this question and realized that could almost be me. I am fairly OCD about refactoring someone else's code when I see that I can improve it. For example, if the code contains duplicate ...
13
votes
7answers
1k views

How do you manage refactoring with a large code base and many developers?

I would like to prevent a situation where two developers refactor the same code simultaneously without talking about it first, probably using a tool of some kind, maybe an Eclipse plug-in. Can you ...
45
votes
16answers
3k views

How do I handle refactoring that takes longer than one sprint?

I work with a code base that is over 500K lines of code. It is in serious need of refactoring. There have been refactoring efforts identified that will take longer than the normal two week sprint. ...
16
votes
7answers
1k views

If a fluent coder disregards good practices, doesn't his fluency work against him?

I am working on a fairly large and buggy application - and due to the way it's written (I'll spare you details, but it violates rules in most areas you can think of), it is next to impossible ...
14
votes
8answers
759 views

Quantifying the value of refactoring in commercial terms [duplicate]

Here is the classic scenario; Dev team build a prototype. Business mgmt like it and put it into production. Dev team now have to continue to deliver new features whilst at the same time pay the ...
14
votes
9answers
1k views

We're not a software company. Is a complete re-write still a bad idea? [duplicate]

I understand the reasoning behind Joel Spolsky's article "Things You Should Never Do, Part I", but I always see it referenced in regards in situations where the end goal is the production of software. ...
10
votes
7answers
2k views

Best way to break down overwhelming code into manageable chunks?

I'm continually becoming overwhelmed by large projects, once they reach a certain level of complexity. Once I reach a certain point in a project, my progress slows to a crawl and I find myself ...
10
votes
7answers
268 views

What approaches can I take to lower the odds of introducing new bugs in a complex legacy app?

Where I work I often have to develop (and bug fix) in an old system (.NET 1) whos code is complete spaghetti - with little thought given to variable names, program structure nor comments. Because of ...
8
votes
4answers
793 views

Do I need to upgrade log4j to slf4j [closed]

We are reviewing our JEE web application for some planned refactoring and one of the suggestions is to replace log4j with logback or slf4j The team is not clear on whether we should do this - because ...
11
votes
1answer
500 views

How much time should I dedicate to refactoring my code?

I don't know if it's just with me, but when I start refactoring some code I waste a lot time doing that and it never ends. Every time I read the code again, I find something that can be improved, ...
10
votes
8answers
431 views

How can current IDEs improve the development experience? What secondary tools should be primary?

A modern IDE wouldn't be taken seriously if it didn't have a good editor with syntax highlighting, comprehensive warnings, and a source-level debugger. And there are many more tools/IDE features that ...
9
votes
5answers
1k 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 ...
19
votes
10answers
1k views

Eliminating Magic Numbers: When is it time to say “No”?

We're all aware that magic numbers (hard-coded values) can wreak havoc in your program, especially when it's time to modify a section of code that has no comments, but where do you draw the line? ...
14
votes
7answers
901 views

Should I change author name in class file if I make more than 80% changes?

I am refactoring existing set of java test classes for automated UI tests. At times I end up in making massive changes in class file or completely revamping it. This makes me think that when I am ...
8
votes
7answers
446 views

Refactoring While Programming

When posed with a problem, particularly when it is complicated in nature, I try to take some time to think about the approach I am going to take to solve the problem. Despite this, what often happens ...