Daniel Sapoundjiev on
  NoIf programming

NoIf programming

How came to that?

I was working on some algorithms for prime number generation and for 2D nesting of polygons and computer was not able to process the tasks I gave to it in acceptable time. So, I start looking for optimizations. One of the first thing I saw in the code was that there were a lot of IF operators there. Some of them were executed billions of times in my algorithms.

So, I remembered about the strategy pattern and about executing methods by pointers to them. Then I made some changes in this direction and it was better.

Where is the IF in the nature?

We often use the word IF when we talk about logic or something in our life. If I do this then that will happen. If this happens then I will do that.

However, nature works differently. In nature there are objects that are moving and every move just reflects the move of the other objects. There is no pure IF here. There is just movement and interaction. Ball moves on the snooker table and hits another ball and the other ball goes in the pocket. And it just happened depending on the movement.

Where is the IF in the machine code?

Conditions first appear in the machine instructions. Here the word 'IF' is not used and conditional jump is used.

Where is the IF in the processors and other chips in the computer

If we take a look in the Intels processor specifications for developers we can see some code at the end of every instructions. This code explains logical work of the processor to performing this instruction. A lot of IF-s are there.

If we check specifications of multiplexers, and other chips that are used to build a computer and other devices we will see some tables for the input and output values. They are also logically explained! If we have this for input then we will have this for output. However, good is here that the word IF is not so often used.

However, even this chips and processors don't work with IF operators inside. They work with direct change inside the material, and this change allows some electrons to pass to the output pins. So, where is the IF operator here?

What is Noif programming?

I thought about this name noif, because the basic idea of this technique is to skip some if operators in methods that are called many times.

Sometimes there are tasks that are very hard for the CPU. Most of the cases the pc is fast enough but sometimes. Sometimes the CPU is very week. And in these cases we need to take care about how much unnecessary work we give to it.

So, if we make the code like the nature works we will skip the IF operator.

Memory and speed in noIF programming

Typically, when code is optimal written there is dependency between speed and memory. More memory, more speed. Less speed, less memory. But in noIF programming we can see that memory usage can be reduced, and the speed is higher. The better speed comes from eliminating the IFs, which is obvious. The less memory usage comes from - Lot of cases are in different classes, so if we don't use them they are not loaded.

Noif in software protection

Most of the software applications can be easily cracked. Because the license condition is just before showing the “Product is not licensed” message. You can attach to process, set breakpoint in the windows API function that shows the message. And than go back to previous instructions and soon you will find the license condition.

However this can not happen if you use the Noif programming. You can change the state(or strategy) when the applications is loaded depending the license information (note, you have one if here!). But later you don’t have ifs, the concrete strategy is executed. When there is no valid license the concrete strategy will show the message. When there is valid license the strategy will execute straight the application code.

The advantages here are. Application is faster and more secure

Let's explain Noif programing with real example.

We have a List class in .net. When we take a look in the code of Add method we will see something like this.
If (First == null) First = value;
But only on the first execution of this method the First is equal to null. When we add 1 million items in this list, only one time enters the code inside. 1 million - 1 times this IF is unnecessary. So one method can be used the first time and this method can be replaced by another for the rest calls.

This example shows how to replace method with another. But we can replace whole object by using the strategy pattern.......

Be aware of

Noif programming can save you a lot of time and make your applications faster. But in some cases you can get "dead by design". Use noif programming for this parts of the application where you will add more and more ifs with time! Not everywhere as a must!

In conclusion

IF operators are very important part of every application. But sometimes developers and software designers don't care about them. Developers use them more than necessary.

Back to main menu