DEV Community

Discussion on: What makes for readable code?

Collapse
 
pinotattari profile image
Riccardo Bernardini

I am on the side of readable names. In my opinion (and experience) if you use good names you can avoid most of comments in the implementation.

Sometimes I collect a piece of code in a procedure that is called just once, only to provide a "descriptive name" to that piece of code. Of course, it is not efficient because of the stack manipulation at call/return time, but if efficiency is not a strong issue (and often nowadays it isn't), the gain in readibility is worth the inefficiency.

Of course, coherent and good formatting helps a lot, but nowadays you can outsource that to the IDE.

About comments: I usually do not put comments in the implementation, comments that should explain what the code does. If you choose the right names and your language is fairly high.level (so you do not have to mangle with pointers at every line of code), the code is often self-esplicative.

I usually use comments to document stuff that (i) is not immediately clear from the code and (ii) it is pretty stable (so it does not change often when the code changes). Most of my comments are put in the spec file of a package (.h for C people) to explain (i) what is the objective of the package, why it is there and (ii) what is the duty of every procedure declared there.

Collapse
 
fennecdjay profile image
Jérémie Astor

Sometimes I collect a piece of code in a procedure that is called just once, only to provide a "descriptive name" to that piece of code. Of course, it is not efficient

It depends on your compiler: in C, if you write said function as static inline, the compiler might insert it in your code (it is said to be as fast as macros).

Collapse
 
pinotattari profile image
Riccardo Bernardini

You're right. I think that many compilers could do that autonomously during the optimization phase. If I remember correctly gcc does this unless you specify an option.

Collapse
 
pclundaahl profile image
Patrick Charles-Lundaahl

+1 on the avoiding comments. I remember hearing somewhere that most comments are lies.