Writing Clean Code : Part 2

vbvbbvbvbvbvbvbbvbv.png

We’ve seen some shitty coding styles in the first part. Lets fix the damage in this part.

How to fix those bad codes. Please go to this link to see those example of some bad codes. 

1. Specify the type of exception that your code throws and respond accordingly. Here is a solution to its bad code.

try {
   /* open file */
}
catch(FileNotFoundException e) {
  Log.e("File Open", e.getMessage());
}

try {
   /* read file content */
}
catch (IOException e) {
  Log.e("File Read", e.getMessage());
}

 try {
     /* close the file */ 
} 
catch (IOException e) {
   Log.e("File Close", e.getMessage());
}

2. Just use for loop in such cases.

for (int i=0; i<MAX_VALUE; i++){
    //some operation on the array;
}

3. If you don’t need any parameter to initiate the class then you can just setup some static method of that class and call them. For eg:

You have class A

class A{
    ...
    void function()
    {

    } 

}

If you declare the methods with the static modifier , i.e

public static void function() { /* ... */ }

Then you can call them without creating instances:

A.function();

4. If you have multiple condition then you can just perform OR or AND operation

if (condition1 || condition2 || condition3 || condition4){
  //Do something
}

Or

if (condition1 && condition2 && condition3 && condition4){
  //Do something
}

5. In such cases you can just declare the variable only when its necessary.

6. If you have some expensive method to call then you can just store its value in a variable and use it anytime you want. 

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

 

7. You can just call next method from that method. Here is a fix for that problem. 

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

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

  void DoThing3() {
	// Do "thing 3"
	//do nothing - exit
  }

8. Use values from string.xml

9. Just test if the collection is null or empty. Call the size of the collection gives nullPointerException if it is null. And you might want to avoid that.

if(list == null || list.isEmpty()){
  //Do something
}

10. Using nested blocks for two or three cases at maximum is mandatory but using it for too many cases takes a lot of time. In such cases you can use Switch method.

switch(x){
    case 1:
        // do Something
        break;

    case 2:
        // do Something
        break;
    
    case 3:
        // do Something
        break;
    
    case 4:
        // do Something
        break;
    
    case 5:
        // do Something
        break;
    
    case 6:
        // do Something
        break;
}

So those are some pretty simple solutions to previously discussed example of bad codes. In the next part I will describe some approaches of writing clean code.

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