Declare less variables

Almost every java programmer had heard this statement “accomplish this task with less number of variables.” I sometimes think, why do we do this. I am not saying create more number of variables than needed. I am saying what does it make a difference if I complete task with two variables instead of one. Specifically, talking about interviews, almost 80% interviewer check how optimized code interviewee can write. I think that is really important in interview.

But this should not become our bad habit in professional software development. I think we should not sacrifice clarity of code for micro optimizations. Nowadays, we are living in time where we have strong servers and enough memory to handle such extra stuff. Only goal is to write maintainable code and that can be achieved by following three steps.

  1. Correctness
  2. Clarity
  3. Code optimization

Sometimes, developer becomes much more eager to write as optimized code as possible. So, code becomes difficult to maintain and sometimes quite confusing for other developers.

Why developers fail to choose readability is difficult to say. Usually, they directly jump to write code without much thinking about readability and maintainability. Nowadays, developers who write readable code are respected everywhere than those who can do much more complex and difficult task but with messy code. It can be possible to cover this in peer to peer code review. However, more then 50% of the probable bugs or convention related issues can be easily solved by tools like SonarQube, FindBugs etc.

Optimization is important but not the first goal of any real world applications. If you can write easily understandable code, optimization would be less tricky. This is what Donald Knuth said,

Premature optimization is the root of all evil.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s