Wisdom

10 June, 2012

Higher Order Logic

Higher order logic is logic applied on logic. So, what is logic? Logic is a set of:

  1. Conditions: Checking whether something has a given relation to something else.
  2. Loops: Continue doing an action as long as a given condition is true.
  3. Actions: Putting some value in some container.


Example:

int i = 10; /* putting link of two bytes in i, putting 10 in those bytes */
int x; /* putting link of two bytes in x */
/* running a loop and in each iteration putting something in x */
for(int a=0; a<100; a++)
x = x + i;

Console.WriteLine(x); /* putting something on screen */

Higher order logic is applying logic on logic. The above code is a block of logic, it can be part of a function or a complete function itself but still its a block of logic. The following code is high level logic:

if(DateTime.Now == "1st Jan 2015")
/* call the above block five times */
else
/* call the above block one time */
When we call a method, on basis of some condition, or when we call a method multiple times in sequence, we have a high level logic.

Event-handling therefore is high level logic. The event-handler function is logic, the decision of when to call what function is high-level logic.

Multi threaded programming is at a higher level logic than single threaded programming. That not to say that single threaded programming is necessarily first order logic. Most programs, even when single threaded are at levels higher than the first level in logic.

If there is a starting method in program, such as main method in c, and all methods are called from this method, then the program is already in second order logic. The code in non-main methods is first order logic, the code in main method is second order logic. This holds even if there are no conditions or loops in the main method. If five methods are simply called one after another then we still have a logic, this is pipeline logic, a sequence.

If the methods called from main call other methods then we have three order logic.

Its better to never have a lower order logic method calling a higher order logic method. The flow must go in one direction, in terms of order of logic too.

The last level of logic should ideally have static code, code that reacts on its arguments only. This can safely be called from any method, at anytime, with thread-safety and no complications, no side effects.

The higher the level of logic, the less the number of callers of the code should be. Therefore, main method must be callable only from one place, the operating system. The methods called directly from within the main must not call each other, they must be callable only from main. These methods have second order logic. Any third level method can be callable from any second order level method but ideally must never be called from main.

Logging logic is higher-order logic, even if its just logging code then calling of the actual code. Exception handling is higher level logic, if multiple blocks are called.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home