Skip to main content
281 votes

Prevent deprecated code from compiling after reaching a deadline

This would constitute a feature known as a time bomb. DON'T CREATE TIME BOMBS. Code, no matter how well you structure and document it, will turn into an ill-understood near-mythical black box if it ...
Kilian Foth's user avatar
257 votes
Accepted

When to use / not use syntactic sugar

I disagree with It is hard to read in general especially to "in general". These language features may be hard to read for beginners when they see them the first time, but they were actually added ...
Doc Brown's user avatar
  • 218k
190 votes

How to avoid comments about one line of code for cleanliness

and read that comments are almost always a bad idea for future maintainability And now you are reading that the above is total and absolute BULL____. Use comments. Put in as many as you think are ...
GrandmasterB's user avatar
  • 39.4k
164 votes

Programming cleanly when writing scientific code

This is a pretty common problem for scientists. I've seen it a lot, and it always stems by the fact that programming is something you pick on the side as a tool to do your job. So your scripts are a ...
BgrWorker's user avatar
  • 1,694
164 votes

Clean Code: long names instead of comments

Yes, you understand Clean Code right, but your examples are quite a bit over the top. Here is what you start with: PageReloaderForPagesDisplayingVectorGraphicsThatAreUsedInTheEditorComments ...
Mike Nakis's user avatar
  • 32.7k
159 votes

How do huge open source libraries get maintained while having code far from "clean code" practices?

The principles stated in "Clean Code" are not always generally agreed upon. Most of it is common sense, but some of the author's opinions are rather controversial and not shared by everybody. In ...
JacquesB's user avatar
  • 61.6k
157 votes
Accepted

Is a comment aligned with the element being commented a good practice?

No, such aligned comments are not a good practice. It is not clear that the comments relate to specific positions on the line. It just looks like very wildly formatted code. The comment's indents will ...
amon's user avatar
  • 136k
155 votes

How do you justify more code being written by following clean code practices?

Yes, it's an acceptable byproduct, and the justification is that it is now structured such that you don't have to read most of the code most of the time. Instead of reading a 30-line function every ...
Karl Bielefeldt's user avatar
144 votes

Programming cleanly when writing scientific code

Physicist here. Been there. I would argue that your problem is not about the choice of tools or programming paradigms (unit testing, OOP, whatever). It's about the attitude, the mindset. The fact the ...
Edgar Bonet's user avatar
  • 1,319
132 votes
Accepted

How do you justify more code being written by following clean code practices?

... we are a very small team supporting a relatively large and undocumented code base (that we inherited), so some developers/managers see value in writing less code to get things done so that we have ...
David Arno's user avatar
  • 39.6k
119 votes
Accepted

Functions that simply call another function, bad design choice?

Never forget the Law of Demeter: The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, ...
Flater's user avatar
  • 58.1k
118 votes
Accepted

Clean Code: Functions with few parameters

I don't share your opinion. In my opinion using global variables is a worse practice than more parameters irrespective of the qualities you described. My reasoning is that more parameters may make a ...
Samuel's user avatar
  • 9,237
118 votes
Accepted

How do I know how reusable my methods should be?

It's turtles all the way down. Or abstractions in this case. Good practice coding is something that can be infinitely applied, and at some point you're abstracting for the sake of abstracting, which ...
Flater's user avatar
  • 58.1k
109 votes

How do you safely delete a piece of code that looks like it's never entered?

In my perfect fantasy world where I have 100% unit test coverage I would just delete it, run my unit tests, and when no test turns red, I commit it. But unfortunately I have to wake up every morning ...
Philipp's user avatar
  • 23.5k
108 votes

Why should I use dependency injection?

The advantage is that without dependency injection, your Profile class needs to know how to create a Settings object (violates Single Responsibility Principle) Always creates its Settings object the ...
Michael Borgwardt's user avatar
106 votes
Accepted

Additional line in block vs additional parameter in Clean Code

These guidelines are a compass, not a map. They point you in a sensible direction. But they can't really tell you in absolute terms which solution is “best”. At some point, you need to stop walking ...
amon's user avatar
  • 136k
102 votes

Is putting general-use functions in a "helpers" file an anti-pattern or code smell?

Both Java and C# have the existence proof that this works: the Math class. The danger is the "kitchen drawer" problem, where a class simply gathers new kitchen tools and implements, many of ...
Robert Harvey's user avatar
98 votes

When to use / not use syntactic sugar

Syntactic Sugar is Good™. What is a while loop if not sugar for goto? let me tell you what it is: easier to read and less error prone. Yes, you can do less with while than with goto and conditionals......
Theraot's user avatar
  • 9,241
97 votes
Accepted

Multiple boolean arguments - why is it bad?

The obvious problem To resolve this however, how would one refactor this? Do you create 4 functions now? trainModelWithZerosInitOptimSGD(...) trainModelWithZerosInitOptimAdam(...) ...
Flater's user avatar
  • 58.1k
86 votes

How do you safely delete a piece of code that looks like it's never entered?

There are two halves to this process. The first is confirming that the code is indeed dead. The second is comprehending the costs of being wrong and making sure they are properly mitigated. Many ...
Cort Ammon's user avatar
  • 11.9k
83 votes

Programming cleanly when writing scientific code

Version control is probably going to give you the most bang for your buck. It isn't only for long-term storage, it's great for tracking your short-term experimentation and going back to the last ...
Karl Bielefeldt's user avatar
83 votes
Accepted

How do huge open source libraries get maintained while having code far from "clean code" practices?

Good answer here already, but let me say a word about your butterknife example: though I have no idea what the code does, at a first glance, it does not look really unmaintainable to me. Variables and ...
Doc Brown's user avatar
  • 218k
82 votes

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

Ideally I think you should extract your logic for getting the alert code/number into its own method. So your existing code is reduced all the way down to { addAlert(GetConditionCode()); } and you ...
Steven Eccles's user avatar
78 votes
Accepted

How to avoid comments about one line of code for cleanliness

You answered this yourself in the very first sentence of your question - comments are almost always a bad idea for maintainability. There are times when a quick note in the code is a good thing to ...
Thomas Owens's user avatar
  • 85.2k
75 votes

Is a comment aligned with the element being commented a good practice?

While writing the question, I realized that I can break the lines: (note.tags || []) // get all tags for the line, or empty array .forEach( tag => inc.push( // add the result of include to ...
WoJ's user avatar
  • 1,661
71 votes

Prevent deprecated code from compiling after reaching a deadline

In C# you would use the ObsoleteAttribute in the following manner: In version 1, you ship the feature. A method, class, whatever. In version 2, you ship a better feature intended to replace the ...
Eric Lippert's user avatar
  • 46.5k
70 votes
Accepted

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

The important measurement is complexity of the code, not absolute size. Assuming that the different conditions are really just single function calls, just like the actions are not more complex than ...
cmaster - reinstate monica's user avatar
70 votes

Is it a code smell to set a flag in a loop to use it later?

There's nothing wrong with using a Boolean value for its intended purpose: to record a binary distinction. If I were told to refactor this code, I'd probably put the loop into a method of its own so ...
Kilian Foth's user avatar
69 votes

Clean Code: Functions with few parameters

You should avoid global variables like the plague. I wouldn't put a hard limit to number of arguments (like 3 or 4), but you do want to keep them to a minimum, if possible. Use structs (or objects ...
robert bristow-johnson's user avatar
66 votes
Accepted

Inverting an IF statement

It depends. In your example having the non-inverted if statement is not a problem, because the body of the if is very short. However you usually want to invert the statements when the bodies grow. We ...
Andy's user avatar
  • 10.4k

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