RSS Feed

The += ++ — Incremental Operators in Java Performance Readability

This is a very basic tip. Most experienced coders will wonder why I have even written this.

Consider this, something that you do often.

i = i +3;

Experience coders will probably express it differently, as below. This is exactly the same.

i += 3;

Why would you want to write your code like this? Some people think it may perform better.I personally would give credit the compiler and doubt whether there is any performance difference. However, when you get used to these type of statements I think readability improves. The readability improves in more complex statements like below.

drawLinePositionX = drawLinePositionX + previousX +marginX + linePaddingX + etc…

The replacement statement would read.. the key is that it is shorter and in my opinion says more about the type of statement it is.

drawLinePositionX += previousX + marginX + linePaddingX + etc…

Because the line is shorter it is easier to read. Also, it helps statements that can run on a number of lines. So readability is key. Much time is spent debugging and developing code and this is often more important than code optimisation. Code optimisation is a topic I want to cover one day.

By the way. I notice some people are not aware of an even more basic statement and that is the “++”.

If instead of the example consider this..

i = i + 1;

Taking the above example this is the same as..

i += 1;

However with single incrementals like this, they are best expressed like this..


Anyway, this may help a new person to Java.




Leave a Reply

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

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

%d bloggers like this: