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.

518 questions
125
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…
47
votes
8 answers

Why isn't encoding the names of arguments in function names more common?

In Clean Code the author gives an example of assertExpectedEqualsActual(expected, actual) vs assertEquals(expected, actual) with the former claimed to be more clear because it removes the need to remember where the arguments go and the potential…
43
votes
14 answers

Is there any benefit to obsession with making code "look pretty"?

Sometimes I spend ridiculous amounts of time (hours) agonizing over making code "look pretty". I mean making things look symmetrical. I will actually rapidly scroll through an entire class to see if anything jumps out as not looking "pretty" or…
TaylorOtwell
  • 2,667
15
votes
6 answers

When to use else in conditions?

When to use else in conditions? 1) a) long int multiplyNumbers(int n) { if (n >= 1) { return n*multiplyNumbers(n-1); } else { return 1; } } or b) long int multiplyNumbers(int n) { if (n >= 1) { return…
zohub
  • 161
10
votes
1 answer

Trouble grasping what clean code looks like in real life

I am currently reading and working through "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin. The author talks about how a function should do one thing only, and thus be relatively short. Specifically Martin writes: This…
1west
  • 111
7
votes
2 answers

How to solve "train wreck" properties problem that violates Law Of Demeter?

I've been reading about law of Demeter and I would like to know how to solve this traversing model properties problem that I see a lot on Objective-C. I know that there is a similar question but in this case I'm not calling a method from the last…
6
votes
3 answers

Clean Code: A function with many arguments should become an object? (Martin's Clean Code)

I came across reading a blogpost (this one on medium.com) about some principles of writing clean code and referring to a book by Robert C. Martin. Let me cite a particular paragraph from the blogpost: A function shouldn’t have more than 3…
5
votes
7 answers

Is it a good idea to split a constructor in multiple functions?

Here is the work flow of a class of my program: Server class instanciation -> Creating socket -> Binding socket to addr:port -> Listening -> Handling clients Should I put all that stuff in the constructor or in separated private functions ?
Nurrl
  • 179
5
votes
3 answers

Private string constants for map keys

Is it good practice to define private constant strings that have the same name as their values? Take the following code for example. public class Example { private static final String FIRST_KEY = "firstKey"; private static final String…
3
votes
2 answers

How to define what fields to check for equality?

I have an odd conceptual question, It's not about a specific incident, just a general best-practices approach. I have asked myself on occasion when defining java Equal methods, what makes two objects equal. The simple approach is to check rather…
dsollen
  • 1,143
2
votes
4 answers

Try/Catch or test parameters

Possible Duplicate: Arguments for or against using Try/Catch as logical operators Efficient try / catch block usage? I was recently on a job interview and I was given a task to write simple method in C# to calculate when the trains meet. The…
Ondra
  • 121
  • 3
1
vote
1 answer

How to declare a lot of variables under line length restrictions?

I need a good approach to declare a lot of variables over a context. For exmaple in kubeflow, I need declare a pipeline with a lot components in order to do that, I use the following code: component_1_op = comp.load_component( …
Tlaloc-ES
  • 387
  • 1
  • 12
1
vote
1 answer

Clean Code: two types of arguments

I read Robert Martin's Clean Code: Function chapter. In this chapter, the best number of arguments of function is single argument. So, it is good to abstract that argument and function should be separated if it have various roles. In practice, I…
undefined
  • 131
1
vote
4 answers

How to make sure clean code well implemented?

I'm a project manager in a small team of developers (consisting 3 team leaders and 10 programmers) We're currently trying to enforce clean code for our team , we're having small frequent seminars discussing uncle bob's clean code book. Long story…