Writing Clean Code : Part 1

23232.jpg

Writing a code that just works is one thing and writing a clean code is the whole new level. There is a saying “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”, and it makes perfect sense. Good programmer should know how to write a clean code that can be understood by others too, not only himself(or herself, feminist please dont kill me). Code code can be beneficial for a company but bad code can destroy the company. There are countless examples of bad code bringing companies down or making a disaster of an otherwise good product. There is a great example such as space rockets being mislaunched due to a badly transcribed formula (a single line was missing that meant the rocket had to be terminated 293 seconds after launch ultimately costing around 20 million dollars). So the programming style of a programmer can make a huge impact on any place he/she works for. Then whats the delay lets learn to write beautiful and clean code.

Examples of bad code

1. Have no idea what’s the exception for

try {
   /* open file */
}
catch(Exception e) {
  e.printStackTrace();
}

try {
   /* read file content */
}
catch (Exception e) {
  e.printStackTrace();
}

try {
   /* close the file */
}
catch (Exception e) {
  e.printStackTrace();
}

2. Defining the logic using exceptions where a for-loop or any form of loop would be sufficient.

while(i < MAX_VALUE)
{
   try
   {
      while(true)
      {
         array[j] = //some operation on the array;
         j++;  

      }
   }
   catch(Exception e)
   {
      j = 0;
   }
}

3. Some constructor that takes no parameter and creates a new instance of that class. 

public MyClass() {
}

4. If If If If If If If……… Seriously?

if{
 if{
  if{
   if{
    if{
     if{
      if{
       if{
         ....

5. And this

String tmp = null;
// 50 lines that don't touch tmp
tmp = whatever;
// ...

6. Calling an expensive function over and over instead of storing the result, when you know it won’t change.

if (expensiveFunction() > aVar)
    aVar = expensiveFunction();
for (int i=0; i < expensiveFunction(); ++i)
    System.out.println(expensiveFunction());

7. Acute misapprehension of control flow.

void Next(state) {
	switch (state) {
	case INITIAL:
	DoThing1();
	break;
	case THING1:
	DoThing2();
	break;
	case THING2:
	DoThing3();
	break;
	case THING3:
	// do nothing -- exit
	break;
	}
  }

  void DoThing1() {
	// Do "thing 1"
	Next(THING1);
  }

  void DoThing2() {
	// Do "thing 2"
	Next(THING2);
  }

  void DoThing3() {
	// Do "thing 3"
	Next(THING3);
  }

8. Hardcoded String.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello there This is some long ass hardcoded string" />

9. Collection emptiness test using size()

if (List.size()=0){
    //Do something if its null
}

10. Too many nested blocks.

if (){
    //Do something
}else if() {
    // Do something 
}else if() {
    // Do something 
}else if() {
    // Do something 
}else if() {
    // Do something 
}else if() {
    // Do something 
}... Oh come on!

These are some common forms of bad code. In my opinion the bad form of code is the complicated code. If your code is complicated, it cannot be clean. Complicated code is opaque. It’s hard to read, hard to understand and impossible to debug. Clean code requires you to take a complex problem and split it into simpler parts.  The skill of the programmer is not creating complicated solutions to complex problems. The skill of the programmer is to reduce complex problems to simple elements. At the heart of that skill is working-out precisely where to make each cut. The most brilliant programmers just destroy complication with precise targeted reduction. Eventually each individual part is so simple that it is transparent. If those individual parts are also given the right names – and symbol names are chosen with care, the result is code that is elegant, transparent and obvious.

In the next part I will fix above bad codes.

Happy Programming Fellas!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s