Clean Codetech

Clean Code 2.0

Meaningful names

The importance of good names (variables, classes, functions ..) is often overlooked. Code is read 10 times more than it is written. Names have a great impact on understanding what the code does. With good names it takes less time to get to know or maintain the code. The context of the code should be explicit from the code itself.

Key takeaways:

  • Names of similar thing should alphabetically sort together
  • Avoid using noise words like variable or string. Eg lastNameString or countVariable
  • Make sure your names are easy to read and pronounce.
  • Sometimes it is a good idea to create a variable just for the readability of the code. This is useful when having a piece of code with lots of numbers where it is not easy to understand where the numbers come from. For simplicities sake here is an easy example just to demonstrate what I mean.
for(int i = 0; i<7; i++){

for(int i = 0; i < NUMBER_OF_DAYS_IN_A_WEEK; i ++){
  • Avoid relying on the intellectual capabilities of the programmer. Though you may want to brag about how smart you are, it is a bad idea to use one letter variables of which others have to remember what they stand for.
  • A class name should not be a verb
  • Accessors, mutators and predicates should start with resp. get, set and is.
  • Use static factory methods with names that describe the arguments when constructors are overloaded
  • Choose clear names over entertaining names (joking is only funny in the short term)
  • Use one word per concept. When some methods start with get, make sure that all methods starting with get conceptually doe the same thing. Example: You have methods like fetchItem and fetchPerson that retreive an item or person from the database. Don’t create a method fetchAddress which asks the user for input about his address.
  • Don’t be afraid of other people’s reaction when you want to rename something. The possible discussion will probably lead to more alignment along the way

Leave a Reply

Your email address will not be published.