29

Robert C. Martin offers in the fist chapter of his book 'Clean Code' several definitions of 'clean code' from differen well known software experts. How do you define clean code?

durron597
  • 31,968
  • 17
  • 99
  • 158
crauscher
  • 6,528
  • 14
  • 59
  • 85

8 Answers8

48
  • Easy to understand.
  • Easy to modify.
  • Easy to test.
  • Works correctly (Kent Beck's suggestion - very right).

These are the things that are important to me.

Ree
  • 6,061
  • 11
  • 48
  • 50
  • 9
    It should also work correctly. Otherwise this seems like a fine definition to me. – Kent Beck Jun 05 '09 at 07:29
  • 1
    Good point, I added it to the answer. – Ree Jun 05 '09 at 07:45
  • You should aim to extend your code rather than modify. The open close principle of SOLID is no joke, you will make much better code, and much faster adjustments when you get a change request. – Morten Bork Apr 11 '22 at 21:00
19

Code I'm not afraid to modify.

g andrieu
  • 2,081
  • 3
  • 14
  • 10
14

Code that doesn't require any comments to be easily understood.

Jonathan van de Veen
  • 1,016
  • 13
  • 27
  • I would say minimal use of comments, rather than none at all: (1) it is not impossible to have a counter-intuitive algorithm; a comment can be used to make the user aware (2) comments can be used to break down code to logical sections so the reader immediately has an overview. – N.Vegeta Jan 05 '18 at 16:49
  • 1
    Thank you for this comment. As to breaking down your code into logical sections with comments, I strongly disagree. If you feel the need to break up your code, use methods with correct names. As to counterintuitive algorithms, if you do the above, I doubt you need comments to describe what is going on. If you still need to, documenting your solution is probably more helpful then just adding some comments to your code. – Jonathan van de Veen Feb 08 '18 at 11:11
  • I agree that the code should not need any comments to make "what it does" clear. But when it comes to "why it does that", well worded comments can be useful - even necessary. – keremispirli May 31 '18 at 11:44
  • @keremispirli: Don't get me wrong. I do write comments in source code when something is not self-explanatory. The question is, how do you define clean code? I think if you need comments to clear up why your code is written the way it is, then there is still room for improvement. The question then becomes, is it worth it to do so? A lot of times that question will be answered with no, because it doesn't add value for the user, nor does it improve maintainability enough to justify the effort. – Jonathan van de Veen Jun 01 '18 at 08:39
3

Code which reads as close to a human language as possible. I mean it on all the levels: from syntax used, naming convention and alignment all the way to algorithms used, quality of comments and complexity of distribution of code between modules.

Simplest example for naming convention:

if (filename.contains("blah"))

versus

if (S_OK == strFN.find(0, "blah"))

Part of it depends on the environment/APIs used, but most of it is of course the responsibility of the developer

Rom
  • 4,129
  • 23
  • 18
2

Point-free Haskell code. (Not really, though.)

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
1

Reusable code is also important. So not only important is the quality of the code, but where do you put. Example, business logic into a Controller is a useless code

paul
  • 12,873
  • 23
  • 91
  • 153
1

Code in which the different modules or classes have clearly defined contracts, is a good start.

gnud
  • 77,584
  • 5
  • 64
  • 78
1

Code which doesn't break in multiple places when you make a single, seemingly insignificant change. It is also easy to follow the control path of the program.

Jesse
  • 11
  • 3