Questions tagged [clean-code]

The term "clean code" is used to describe computer programming code that is concise, easy to understand, and expresses the programmer's intent clearly. Questions with this tag relate to the process of writing clean code, or refactoring old "dirty" code to be clean code.

While the idea of clean code has existed for many years, the term started to become popular in 2008 when Robert Cecil Martin (Uncle Bob) published his book Clean Code: a Handbook of Agile Software Craftsmanship.

Some of the primary ideas behind include, but are not limited to:

  • Ensuring that code has useful unit tests in place. This helps guarantee that code does what it says it does.

  • Keeping code units (classes, functions, et al) short and concise. A block of code should do one thing and do it well.

  • Eliminate frivolous comments. Code should be self-documenting through judicious selection of names for code elements (classes, functions, variables, et al).

  • Refactor early, refactor often. Just like a manuscript goes through many revisions, code should not be written once. As new code is added, old code needs to adapt as new structures take form and code is reused.

509 questions
219
votes
16 answers

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

I have read a book called Clean Code by Robert C. Martin. In this book I've seen many methods to clean up code like writing small functions, choosing names carefully, etc. It seems by far the most interesting book about clean code I've read.…
184
votes
10 answers

Why is Clean Code suggesting avoiding protected variables?

Clean Code suggests avoiding protected variables in the "Vertical Distance" section of the "Formatting" chapter: Concepts that are closely related should be kept vertically close to each other. Clearly this rule doesn't work for concepts that…
Matsemann
  • 1,918
  • 3
  • 16
  • 21
172
votes
23 answers

Programming cleanly when writing scientific code

I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code. My code is primarily "scripting" type stuff - things to test mathematical functions, or to simulate something - "scientific…
Auden Young
  • 1,637
  • 3
  • 11
  • 21
138
votes
17 answers

How do I know how reusable my methods should be?

I am minding my own business at home and my wife comes to me and says Honey.. Can you print all the Day Light Savings around the world for 2018 in the console? I need to check something. And I am super happy because that was what I had been…
Koray Tugay
  • 1,565
  • 3
  • 12
  • 18
125
votes
16 answers

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 used anywhere or only referenced in unit tests…
Simon Hutton
  • 311
  • 2
  • 3
  • 6
124
votes
20 answers

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

You've found some code that looks superfluous, and the compiler doesn't notice that. What do you do to be sure (or as close to sure as you can) that deleting this code won't cause regression. Two ideas spring to mind. "Simply" use deduction based…
Bradley Thomas
  • 5,090
  • 6
  • 17
  • 26
110
votes
13 answers

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

Moderator note This question has already had seventeen answers posted to it. Before you post a new answer, please read the existing answers and make sure your viewpoint isn't already adequately covered. I've been following some of the practices…
Pablo Gonzalez
  • 843
  • 2
  • 7
  • 8
104
votes
9 answers

Why should I use dependency injection?

I am having a hard time looking for resources on why I should use dependency injection. Most of the resources that I see explains that it just passes an instance of an object to another instance of an object, but why? Is this just for cleaner…
Daniel
  • 1,045
  • 2
  • 7
  • 5
103
votes
14 answers

At what point is brevity no longer a virtue?

A recent bug fix required me to go over code written by other team members, where I found this (it's C#): return (decimal)CostIn > 0 && CostOut > 0 ? (((decimal)CostOut - (decimal)CostIn) / (decimal)CostOut) * 100 : 0; Now, allowing there's a good…
Bob Tway
  • 3,606
  • 3
  • 21
  • 26
101
votes
9 answers

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

Is it an anti-pattern or code smell to put "general use" functions (examples below) into a catch-all file named "helpers" or "utils"? It's a pattern I've seen quite a lot in the JavaScript world, which I've also seen with other languages. By…
old greg
  • 909
  • 2
  • 4
  • 6
90
votes
10 answers

Clean Code comments vs class documentation

I'm having some discussions with my new colleagues regarding commenting. We both like Clean Code, and I'm perfectly fine with the fact that inline code comments should be avoided and that class and methods names should be used to express what they…
Bjorn
  • 1,073
  • 1
  • 8
  • 8
89
votes
23 answers

Is fewer lines of code always better?

Which of these programming styles is better? var result = methodOne(methodTwo(a, methodThree(b)), c, d); or var result3 = methodThree(b); var result2 = methodTwo(a, result3); var result = methodOne(result2, c, d);
Mike Bryant
  • 379
  • 1
  • 6
  • 10
85
votes
9 answers

When to use / not use syntactic sugar

Currently I am working on a school project written in C#. Some teammates just started in C# and some are already familiar with C#. Today I had a discussion on whether to use syntactic sugar like this in our code: private _someClass; public SomeClass…
Gertjan Brouwer
  • 1,021
  • 1
  • 7
  • 10
81
votes
9 answers

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

I'm still inexperienced to write high quality code, so I read books addressing the issue such as Clean Code by Robert C. Martin, and keep checking code of well-known libraries to improve my skills. Although many open source libraries have been…
80
votes
8 answers

Is modifying an incoming parameter an antipattern?

I am programming in Java, and I always make converters sort of like this: public OtherObject MyObject2OtherObject(MyObject mo){ ... Do the conversion return otherObject; } At the new workplace the pattern is: public void…
1
2 3
33 34