Art Of Clean Code

Komal Pal
4 min readMay 3, 2020

--

I always used to wonder the way cursive writing does to handwriting, grammar does to sentences, spacing does to para, then what about coding ?

Coding should also have something with it to get done to look great even to a layman. It’s true that now coding could be easily understood even by a person who don’t code but it completely depends on the coder!!

Moreover, we read code more than we write as if it’s 10:1 times for read to write ratio for a particular code. Thus if code is easily readable then only other developers would like to collaborate which is one of the most important aspect of open source development!!!

Impact in the Real World

All we need to know is everything comes with a cost!!!

  • Maintenance difficulty
  • Backlog Grows, Slows down release
  • Result in poor performance
  • New developers get confused
  • Contract Losses
  • Security Breaches

Why is it important?

  • It saves time : Won’t that be more easy to read a code which is written nice enough to be read easily?
  • It saves money : Easily readable code helps oneself to test and debug easily
  • It teaches newbies : If it’s understood a layman then why won’t a newbie be able to practice it
  • It maintains the learning standards : A well maintained code doesn’t require much of the documentation.
  • Finally, it saves future you!!! : After all , whenever in future you would like to read your code again , you won’t find it a head ache to understand .
“It is not enough for code to work” -Robert C Martin

Important Practices

  1. You are responsible for your own code: Not boss, not customer, just you are for all what you do

2. Use meaningful names: “You should name a variable using same care as you name a new born baby”- Robert C Martin

int days_online; // using underscore
or
int daysOnline; // camel case

3. Write code that expresses intent: Let’s take up one example instead

function diffTime(a,b){return newData(a) -newData(b);}

Here function “diffTime” expresses the same as does in this function.

4. Express yourself in code rather than in comments: Comments are often lies waiting to happen. eg: You added new features and forgot to change previous comments.

5. The Boyscout Rule: “The art of leaving a mess in the code should be as socially unacceptable as littering” -Robert C Martin

6. Single Responsibility Principle : It does one thing, does it well and it does it only. Simply…..

“ When Planning , think big.

When Coding, think Small”

7. Write Tests: Before that code goes to QA ,tester or customer , do test yourself.

Just ever heard of TDD(Test Driven Development)

First Law: You may not write production code until you have written a failing test.

Second Law: You may not write more of a unit test than is sufficient to fail, and not compiling is failing

Third Law: You may not write more production code than is sufficient to pass the currently failing test.

Types of Test

  1. Functional (Checks for functional i.e. business or corporate behavior)

a) Unit Test

b) Integration Test

c) Acceptance Test

2. Non functional(It will just check overall code as doesn’t change what functionality changes)

a) Load Test

b) Performance Test

c) Security Test

d) Localization Test

8. Work in short cycle: Code in incremental and iterative manner

“ When to use iterative development? You should use iterative development only on projects that you want to succeed”- Martin Fowler

9. Independent Architecture :

  • Reusable Code or Techniques
  • Pluggable use-cases in framework
  • Frameworks are tools, not an architecture that comfirmed to.

10. Formatting : Either use space or tab not both for spacing

“If you can’t make it good, at least make it look good”- Bill Gates

11. Error Handling: “Error Handling is the most important , but if it obscures logic, its wrong”-Robert C. Martin

“Any fool can write a code that a computer can understand. Good programmers code that humans can understand”- Martin Fowler

How they can be automated?

  • IDE Configs
  • Linters
  • Prettier
  • Husky
  • Git Hooks
  • npm-audit
  • Github Actions
  • Code Coverage tools
Just don’t bother what this guy does… Now you know much about how to write a clean code

--

--