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

95
votes
16answers
5k views

Should I remove unreferenced code?

I'm working on a medium sized (100k lines) code base, it's all relatively recent code (less than a year old) and has good unit test coverage. I keep coming across methods which are either no longer ...
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. ...
70
votes
19answers
5k views

Is it ok to replace optimized code with readable code?

Sometimes you run into a situation where you have to extend/improve some existing code. You see that the old code is very lean, but it's also difficult to extend, and takes time to read. Is it a good ...
68
votes
6answers
5k views

Fixing a spelling mistake in a method name

One of the methods that I commonly use in our codebase is misspelled (and it predated me). This really irritates me not simply because it is mispelled but more importantly it makes me ALWAYS get the ...
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 ...
59
votes
7answers
4k views

How do I take responsibility for my code when colleague makes unnecessary improvements without notice?

One of my teammates is a jack of all trades in our IT shop and I respect his insight. However, sometimes he reviews my code (he's second in command to our team leader, so that's expected) without a ...
57
votes
11answers
6k views

Is it a bad practice to modify code strictly for testing purposes

I have a debate with a programmer colleague about whether it is a good or bad practice to modify a working piece of code only to make it testable (via unit tests for example). My opinion is that it ...
53
votes
17answers
2k views

How can I make refactoring a priority for my team?

The codebase I work with daily has no automated tests, inconsistent naming and tons of comments like "Why is this here?", "Not sure if this is needed" or "This method isn't named right" and the code ...
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. ...
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). ...
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 ...
44
votes
5answers
853 views

How far should we rename code and data when end users nomenclatures change?

A long time ago we added a feature where our users could "Accept" an image after it was added to a workflow queue. Turns out, we used the wrong term, and users actually "Approve" the image. Changing ...
43
votes
12answers
3k views

Coding style (do more, then simplify) [duplicate]

I'm a CS student and I have been coding for a few months shy of a year now, and I seem to have developed what I think may be a "bad" habit and I'm wondering if anyone does the same (or whether it's a ...
41
votes
10answers
2k views

Is it a good idea to schedule regular time to clean up code? [closed]

I'm managing a small team of developers. Every so often we decide we're going to spend a day or two to clean up our code. Would it be a good idea to schedule regular time, say 1 week every 2 months, ...
38
votes
12answers
3k views

Reconciling the Boy Scout Rule and Opportunistic Refactoring with code reviews

I am a great believer in the Boy Scout Rule: Always check a module in cleaner than when you checked it out." No matter who the original author was, what if we always made some effort, no ...
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 ...
36
votes
11answers
1k views

How do people doing TDD handle loss of work when doing major refactoring

For a while I have been trying to learn to write unit tests for my code. Initially I started out doing true TDD, where I wouldn't write any code until I'd written a failing test first. However, I ...
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 ...
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 ...
32
votes
4answers
2k views

What is the term for this type of refactoring

I am sure there is a term for the following bit of refactoring, but I can't remember it and my Google-fu is failing me! The refactor moves if statements to where they are going to have most impact, ...
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 ...
31
votes
11answers
1k 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 ...
28
votes
12answers
3k views

What is best practice on ordering parameters in a function?

Sometimes (rarely), it seems that creating a function that takes a decent amount of parameters is the best route. However, when I do, I feel like I'm often choosing the ordering of the parameters at ...
27
votes
9answers
2k views

How clean should new code be? [duplicate]

I'm the lead designer in our team, which means I'm responsible for the quality of the code; functionality, maintainability and readability. How clean should I require my team members' code to be if ...
27
votes
13answers
1k views

How far can I push refactoring without changing external behavior?

According to Martin Fowler, code refactoring is (emphasis mine): Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing ...
26
votes
6answers
847 views

What payoffs have you seen from taking care of technical debt?

This article on technical debt has some good points, including: Working on the "technical matters" works best when it is driven by stories. The code base is probably in need of work everywhere, ...
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 ...
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 ...
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 ...
22
votes
18answers
2k views

Why write clean, refactored code? [duplicate]

This is a question I've been asking myself for a long time. Thought of throwing out it to you. From my experience of working on several Java based projects, I've seen tons of codes which we call ...
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 ...
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 ...
20
votes
13answers
2k views

When is it acceptable to NOT fix broken windows?

In reference to broken windows, are there times when refactoring is best left for a future activity? For example, if a project to add some new features to an existing internal system is assigned to a ...
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? ...
19
votes
3answers
1k views

Difficulties With TDD & Refactoring (Or - Why Is This More Painful Than It Should Be?)

I wanted to teach myself to use the TDD approach and I had a project I had been wanting to work on for a while. It wasn't a large project so I thought it would be a good candidate for TDD. However, I ...
19
votes
4answers
2k views

How to drastically improve code coverage?

I'm tasked with getting a legacy application under unit test. First some background about the application: It's a 600k LOC Java RCP code base with these major problems massive code duplication no ...
18
votes
15answers
1k views

Refactoring: Isn't it just a fancy word for clean up your code?

Before Martin Fowler's book "Refactoring: Improving the Design of Existing Code" came out, we used to call major changes to code "rearchitecture" and minor changes "cleanup". IMO, refactoring ...
18
votes
8answers
679 views

Dealing with coworkers when developing, need advice [closed]

I developed our current project architecture and started developing it on my own (reaching something like, revision 40). We're developing a simple subway routing framework and my design seemed to be ...
17
votes
4answers
1k views

How to convince/prove my manager that a rewriting is needed rather than a refactoring [duplicate]

My manager wants me to refactor a gigantic amount of terribly-coupled, macroed, full of private namespace methods, hierarchy-perverted (even 10+ levels of inheritance) code which hasn't been (indeed) ...
17
votes
8answers
778 views

Does anyone else have a refactoring problem? [closed]

It seems like after I've written a significant amount of code I get this anxious feeling like I haven't done it in the best possible way, and I end up continuously refactoring and spending way too ...
17
votes
7answers
657 views

What can you do about the quality of existing integration and unit tests while being the new guy in a team ?

A recurring theme that I came across in my career is being the new developer to arrive in a team, and quickly having an inherent distrust of the existing unit and integration test suites. During the ...
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 ...
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 ...
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 ...
16
votes
11answers
430 views

Refactor or Concentrate on Completing App

Would you refactor your app as you go or focus on completing app 1st. Refactoring will mean progress of app app will slow down. Completing app will mean you get a possibly very hard to maintain app ...
16
votes
12answers
876 views

Is it ok to write a quick software programme and then refactor it? [duplicate]

In my team, people have tendency to develop a POC (which is very close to actual deliverable in terms of features) which takes good amount of time to be created. And then spend a significant time to ...
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 ...
15
votes
10answers
647 views

suggesting large changes/a rewrite as an intern

The context: it's an internal project (that I don't think a lot of people use) it's old we're updating it The issues: it abuses the mvc framework (no use of models, business logic in views, etc) ...
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. ...