Code you’re writing the wrong way and why

Sabbir Mollah
NSUACMSC
Published in
3 min readApr 1, 2018

--

There is a fine line between code optimization and code readability. Often we face the dilemma of which one to sacrifice to keep the other. However, there are a few ways of code optimization that don’t alter the readability of our code.

Here are some such simple optimizations with great impacts.

Decrease it before increasing it

Often we write code without considering the range of the variable types we use. While this doesn’t cause any headaches in the daily programs we write in our University lab classes, this may cause serious errors in development or competitive programming.

Suppose, we have three numbers x, y and z. We have to multiply x with y and then divide the whole thing with z.

A normal approach would be:

result = (x*y)/z ;

Nothing fishy here, right?
Wrong.
When x and y are large numbers, they can easily overflow the data type. So a better way is to do the division first and then do the multiplication.

result = (x/z)*y ;

or

result = (y/z)*x ;

This is an interesting way of optimizing that I’ve learned from a senior at NSU. Always try to reduce an expression by division/subtraction/modulus before increasing the result.

switch-case instead of if-else-if

It is a good idea to convert your if-else-if conditions with switch cases whenever you can. Although, you will be always using if-else-if conditions for comparing values such as x>y, or x≤t.

if( age<10 ){}
else{
}

However, whenever you are testing the equality of a variable with a value, switch case is your way to go.

switch( age ){
case 18:
….
}

Why use switch instead of if-else-if? Well, it is much more elegant, if that doesn’t convince you, then switch case works faster than if else. This is due to the compiler which can optimize the switch statements better. In the case of the if-else-if ladder, the program will have to check each if statement in the hierarchy.

Condition optimization

We see ourselves throwing in nested conditions inside the if-else braces or anywhere else without giving a second thought.
Take a look at this condition for example:

if( getLength()>6 || age>20 ){
//do something
}

It may seem, there is nothing to optimize here. However, we know that a||b is true when either a or b or both is true. So, what if() does is, if the left sife of an or || turns out to be true, it doesn’t check the right part. Now, it is very much probable that age turns out to be greater than 20. Where it is less probable for length to be greater than 6. Also, what if the getLength() has a high time complexity? So, most of the time our program will have to check both sides of the condition, which we could have prevented by writing the code like this:

if( age>20 || getLength()>6 ){
//do something
}

Now, there is a higher chance that our code won’t even execute the getLength() most of the times.

Swap without temp

What is the first step of swapping two numbers? Take a variable named temp, right?

temp = x;
x = y;
y = temp;

Yes, at least that’s how I would do it. And by no means it is a wrong way. But there is another way of doing it when you just can’ t afford to use one more variable.

x = x+y;
y = x-y;
x = x-y;

Though, it isn’ t a very useful way of optimization, it still has an interesting mathematical notion to it.

Functions in loop breaking conditions

for (int i = 0; i < myString.getLength() ; i++) {}

Stare at this innocent looking code. Notice anything ODD?

Say the length of myString is n. So the loop will iterate for n times. But, there is a catch. Every time the loop iterates it has to execute the getLength() function thus increasing the time of execution.

So a better way is,

int size = myString.getLength()
for (int i = 0; i < size ; i++) {
}

Wrapping up!

The optimizations shown here are mostly syntax related. For more complex optimizations you must have a good idea on Data Structure and different Algorithms.

--

--