275 votes
Accepted

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

If I were in your shoes, I would probably try it this way: first, finish the current project - at least partially - as soon as possible, but in a working state. Probably you need to reduce your ...
user avatar
  • 186k
229 votes

My boss asks me to stop writing small functions and do everything in the same loop

There are other problems Neither code is good, because both basically bloat the code with a debug test case. What if you want to test more things for whatever reason? phoneNumber = ...
user avatar
  • 3,496
224 votes
Accepted

My boss asks me to stop writing small functions and do everything in the same loop

Taking the code examples first. You favour: if (isApplicationInProduction(headers)) { phoneNumber = headers.resourceId; } else { phoneNumber = DEV_PHONE_NUMBER; } function ...
user avatar
  • 38.3k
224 votes
Accepted

Should I refactor the code that is marked as "don't change"?

It seems you are refactoring "just in case", without knowing exactly which parts of the codebase in detail will be changed when the new feature development will take place. Otherwise, you would know ...
user avatar
  • 186k
173 votes
Accepted

Rationale to prefer local variables over instance variables?

What is the objective, scientific rationale to favor local variables over instance variables? Scope isn't a binary state, it's a gradient. You can rank these from largest to smallest: Global > ...
user avatar
  • 38.3k
150 votes

Why should I use a factory class instead of direct object construction?

Like whatsisname said, I believe this is case of cargo cult software design. Factories, especially the abstract kind, are only usable when your module creates multiple instances of a class and you ...
user avatar
  • 35.6k
145 votes

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

It‘s a question of risk management: Refactoring a system always creates the risk of breaking something that worked before. The larger the system, the higher its complexity, and the higher the risk ...
user avatar
  • 68.5k
141 votes

Should I refactor the code that is marked as "don't change"?

Yes, you should refactor the code before you add the other features. The trouble with comments like these is that they depend on particular circumstances of the environment in which the code base is ...
user avatar
137 votes
Accepted

Fixing a spelling mistake in a method name

Should I take the opportunity to just rename the freaking method? Absolutely. That said, if your code has been released as an API, you should also generally leave the misspelled method and have it ...
user avatar
  • 107k
123 votes
Accepted

Writing tests for code whose purpose I don't understand

You are doing fine! Creating automated regression tests is often the best thing you can do for making a component refactorable. It may be surprising, but such tests can often be written without the ...
user avatar
  • 186k
119 votes

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

The only problem I see with your current code is the risk of combinatorial explosion as you add more settings, which can be easily be mitigated by structuring the code more like this: if(...
user avatar
  • 27.2k
119 votes

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

Finished IT projects, even faulty ones, are much better than unfinished ones. Unfinished ones can teach you a lot too, but not as much as finished ones. You may not see it now, but you get an ...
user avatar
  • 1,683
112 votes

How to encourage a team to refactor

Do not ask management for permission to refactor. It's none of their business. You might as well be asking permission to sharpen a pencil. Management doesn't understand refactoring. It's not a ...
user avatar
111 votes
Accepted

Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?

No. There are several reasons why: Variables with meaningful names can make code easier to comprehend. Breaking up complex formulas into smaller steps can make the code easier to read. Caching. ...
user avatar
100 votes

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

One reason is it's really difficult to measure the loss of productivity the messy code is causing, and difficult to estimate the work it will take to clean it properly and fix any regressions. The ...
user avatar
90 votes

Why should I use a factory class instead of direct object construction?

The Factory pattern vogue stems from an almost-dogmatic belief among coders in "C-style" languages (C/C++, C#, Java) that use of the "new" keyword is bad, and should be avoided at all costs (or at ...
user avatar
  • 21.6k
81 votes

Rationale to prefer local variables over instance variables?

The original code is using member variables like arguments. When he says to minimize the number of arguments, what he really means is to minimize the amount of data that the methods requires in order ...
user avatar
  • 871
70 votes

How do I avoid cascading refactorings?

Last time I tried to start a refactoring with unforeseen consequences, and I could not stabilize the build and / or the tests after one day, I gave up and reverted the codebase to the point before ...
user avatar
  • 186k
70 votes

Does it ever make sense for a refactor to end up with a higher LOC?

To answer that, let's take a real world example that happened to me. In C# a library that I maintain, I had the following code: TResult IConsFuncMatcher<T, TResult>.Result() => TryCons(...
user avatar
  • 38.3k
69 votes

Is "Parent x=new Child();" instead of "Child x=new Child();" a bad practice if we can use the latter one?

It depends on the context, but I would argue you should declare the most abstract type possible. That way your code will be as general as possible and not depend on irrelevant details. An example ...
user avatar
  • 54.8k
66 votes
Accepted

Why should I use a factory class instead of direct object construction?

Factory classes are often implemented because they allow the project to follow the SOLID principles more closely. In particular, the interface segregation and dependency inversion principles. ...
user avatar
  • 8,640
66 votes

TDD Red-Green-Refactor and if/how to test methods that become private

A lot of people think that unit testing is method-based; it's not. It should be based around the smallest unit that makes sense. For most things this means the class is what you should be testing as a ...
user avatar
  • 48k
61 votes
Accepted

When are enums NOT a code smell?

Enums are intended for use cases when you have literally enumerated every possible value a variable could take. Ever. Think use cases like days of the week or months of the year or config values of ...
user avatar
60 votes

Should I refactor the code that is marked as "don't change"?

My question is: should I refactor the code when I encounter such warnings from the authors No, or at least not yet. You imply that the level of automated testing is very low. You need tests before ...
user avatar
  • 8,027
58 votes

What factors should influence how I determine when to abandon a small project with a friend?

This may be a cultural thing. In some cultures, admitting that you made a mistake is unheard of, and asking someone to admit to making a mistake is about the rudest thing you can do. If it is that ...
user avatar
  • 38.2k
58 votes
Accepted

How to write unit tests before refactoring?

You're looking for tests that check for regressions. i.e. breaking some existing behaviour. I would start by identifying at what level that behaviour will remain the same, and that the interface ...
user avatar
  • 4,656
58 votes

My boss asks me to stop writing small functions and do everything in the same loop

There is no "right" or "wrong" answer to this. However, I will offer my opinion based on 36 years of professional experience designing and developing software systems ... There is no such thing as "...
user avatar
  • 749
58 votes

Getting buy-in for cleaner and more structured code

The 2 years of experience me was you, but more extreme. I'd always create interfaces for every class, I'd apply any design pattern where I was able to, I'd never inject any concrete implementation, ...
user avatar
57 votes
Accepted

Where does refactoring belong in GitFlow branch naming model?

Refactoring work should go in a feature branch. The prefix "feature" is just a word to describe a discrete programming task, you could choose any word you like, any branch from development is either ...
user avatar
  • 64.9k
56 votes

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

I would happily start the project over. You're a student, and you're still learning. This puts you in a very different position than the question you linked to. You have no professional ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible