DEV Community

Cover image for Best practices to write a readable and maintainable code | Choosing meaningful names

Best practices to write a readable and maintainable code | Choosing meaningful names

Thiago Pacheco on February 14, 2023

They say one of the most difficult tasks as a software developer is naming things but it is also one of the most important practices to have meanin...
Collapse
 
vulcanwm profile image
Medea

this is really helpful!

Collapse
 
andrewharpin profile image
Andrew Harpin

I relate to this so much, I have a bunch of new engineers join the team.

Getting to unlearn the crap they've been taught in their programming classes is hard.

Some useful examples here

Collapse
 
pacheco profile image
Thiago Pacheco

Thank you for the feedback!
Yes, onboarding new engineers is always a challenge so trying to define a styleguide or using some existing one like Google's can be very helpful. I hope the tips here can provide some initial thoughts about that! :)

Collapse
 
rfaco profile image
Roger Faco • Edited

This should be mandatory.
Awesome content.

Thanks, Thiago.

Collapse
 
pacheco profile image
Thiago Pacheco

Thanks Roger!

Collapse
 
colematt profile image
Matthew Cole

Abbreviations may help with making your code smaller, but they make it extra complicated to understand the meaning of the variable in place

This advice is contrary to the Linux kernel coding style, which notes “C is a Spartan language, and so should your naming be. … C programmers do not use cute names like ThisVariableIsATemporaryCounter. A C programmer would call that variable tmp, which is much easier to write, and not the least more difficult to understand. … Local variable names should be short, and to the point. If you have some random integer loop counter, it should probably be called i. Calling it loop_counter is non-productive, if there is no chance of it being mis-understood.”

I understand that a lot of this post basically devolves to “this is my opinion, and these are the reasons for those opinions”. That’s fine. For what it’s worth, I agree with a lot of them! But perhaps the first piece of advice should be “find your project’s style guide, and frequently use a beautifying tool that can be configured to your project’s style guide.”

Unless you are working in a very constrained environment like embedded systems you should consider avoiding the abbreviations.

The length of the variable name has little to no impact on resource consumption in embedded systems since the variable names don’t cross the semantic gap in the compilation phase.