Readable code

Main concept of Readable code comes from one simple question to us developers.
Where do we spend our time while working with code?

  • Writing : writing the code.
  • Reading :  trying to understand the existing code.
Reading code is about fast and effective communication.
 Its not about clever, crafty, pretty code. Its about fast and effective communication.

While writing the code, think or care about the developer behind you, so that they can read and understand your code easily. 

We like to write (or most of the time read) the code, after we deploy who is going to maintain the code?, sometimes you but most of the times someone other then you. The cost of the re-write is so high so,

"Caring about your craft is a good thing. Caring about the what happens after you deploy your code is beautiful".

So, how do we make our code easy to read?
  • Start some kind of style guide, ie. for a angular developer angular style guide.
  • Meaningful names. ie. when you write a name for functions write a readable names. If you can not figure out what code is doing in 5 seconds, you probably should consider refactoring code.
  • Organizing for Readability ie.  Properties followed by method.
  • Smaller functions are more readable. ie. break things into smaller function
  • Less than 20 lines function. Once you get this mark, may be your code is doing more then one thing, may be consider refactoring.
  • Misspelling ( consider writing consistent Naming and spelling because it matters )
  • Provide clarity through code first. People who maintain code lack the author's context but make your code clear through code not from comment.
  • Self describing code.
  • Replace magic strings with constants.
  • Write dirty code, then clean it => Nobody's code is perfect when you first write it. but, you write dirty code, show somebody or show yourself and then clean it up. You iterate over and over. Put your own code for a day and go back, and realize that how easily read your own code.
Dear developers, writing readable code makes your app easier to maintain and saves you and next developer after you.

Tips:
  • 5 seconds rule.
  • One thing per file
  • Small functions
  • Well thought naming
  • Use comments wisely
  • Craft a style guide
  • Use prettier
Reference :
1. Ready for Readable Code? - John Papa, Ng-conf,  https://www.youtube.com/watch?v=56mETnrByBM 





Comments