5 Great Programming Techniques Every Developer Should Know

1 – Encapsulate ifs inside their methods, not outside them:

Bad example:


If (value > 500) {

     applyDiscountForGreaterThan500();

} else if (value > 1000) {

     applyDiscountForGreaterThan1000();

}

Good example:

// encapsulated ifs in the methods 

applyDiscountIfValueIsGreaterThan500(); 

applyDiscountIfValueIsGreaterThan1000();

2 – Stop a moment to think about the name of the package/class/method/variable

YES, names are EXTREMELY important.

With a self-describing name, it is very easy to find out what your code is doing.

For this reason, do not use the first name that pops into your head. Think about the best possible name that describes exactly for what your package, class, method, variable is responsible.

Avoid ambiguous names! If a bug occurs in this functionality, it will be very difficult to find it.

Examples of ambiguous names:

 Double clientValue; // Value of the client, value of what?

 void hide() // What does that hide?

 class Historic {} // Historic of what? Historic for everything? 
  //Where is the cohesion? 

Specific names help A LOT!

3 – Get to know the shortcuts of your IDE

I am going to use Eclipse’s example. It’s very important to use the shortcuts so you can code two or three times faster.

Control + 3 = “Eclipse’s Google” – write “new class” for example.

Control + shift + F = format your code.

Control + 1 = do various actions. For example, invoke a method that does not exist yet and use the shortcut.

Alt + up = moves a line of code to the above line.

Control + shift + R = search any archive you want on your project.

Control + H = search any word you want on your project.

Alt + Shift + R = rename any package, class, method, variable and update all the references.

Configure Eclipse’s formatter. Change it from 80 characters per line to 120. Configure save actions too so you can have actions when you save your code on Eclipse. For example, we can use the command “organize imports” after each save.

There are many other shortcuts, but remember, you need to use them until they get automatic for you. If you forgot to use the shortcut, undo what you did without using the shortcut and then, do the same thing using the shortcut, so you’ll never forget it. For example, if you are going to delete a code line and you forget to use the shortcut, just undo it and use the shortcut control + D.

More sources:

Eclipse Shortcuts

Netbeans Shortcuts

IntelliJ Shortcuts

4 – Encapsulate methods for what you need

Have you noticed a lot of code in your application that does the same thing?

Why not encapsulate a method for this?

For example:

Calendar c = Calendar.getInstance();

c.setTime(new Date());

c.add(Calendar.DATE, +1);

Date userDate = c.getTime();

It would be a lot easier and much clearer to encapsulate all this code in a separated class and method:

Date nextDay = DateUtils.getNextDay();

Consider if need to keep state of your object, if so, instantiate it!

Remember, the most important thing here, is to encapsulate your solution!

5 – Create the culture of Code Review in your company

Even if the company you work for doesn’t use a Code Review tool, ask your friend to take a look at your code.

Each developer has his own experiences. We can learn a lot by sharing them.

Many things that you may not have noticed in a simple Code Review could be uncovered in a peer Code Review. You will see your mistakes and learn from them!

Follow me on Twitter to learn more great programming techniques. I will be sending a new one each week!

For learning new techniques every week, subscribe here on NoBugsProject email list!

Remember, practicing these techniques will make you an even greater developer.

5 Great Programming Techniques Every Developer Should Know

3 thoughts on “5 Great Programming Techniques Every Developer Should Know

  1. John Muczynski says:

    Hi Rafael,

    I saw your discussion of DateUtils to share code that is duplicated.
    Having a class like DateUtils full of static methods is not nearly as helpful as having not-static methods.
    The invokations of static methods cannot easily be refactored to use an object.

    Kind Regards,
    Johnny

    1. Hello John,

      Yeah John, thanks for your feedback, I really appreciate this! I see your point. We get lots of benefits from keeping the state from an object. We must think carefully before deciding what we are going to implement.

      Best regards,
      Rafael Del Nero

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