digital dreamsIn programming, a function or construct is said to be “orthogonal” if its behavior does not depend on the context.  For example consider the equal operator in Visual Basic.  It can be used for comparison or assignment, depending on the context.  Consider “x = 5”.

In the first statement, the context dictates that it is used for comparison and in the latter one, the context dictates that it is used for assignment.

If x = 5 Then
x = 5

Or consider in C++ the following statements:

// #1
CTest x(y);


// #2
CTest x;
x = y;


// #3
CTest x = y;

Which of these three segments are the logical equivalent of each other?  (Two of them match – one does not.)

The answer is that #1 and #3 will both call the copy constructor, while #2 will call the default constructor, followed by the appropriate overload for the operator =.  This can have unintended consequences.  For example, your class may have some sort of reference counting or may manage an unsafe object that needs to be duplicated when you make a copy.  You dutifully create an equal operator, but forget to make a copy constructor.  The result is that the default behavior will be used – a bit-by-bit copy of the data elements of your class – instead of your preferred behavior, which would manage your reference counts and create unique copies of your objects you are managing.

In writing your own code, making functions and objects orthogonal reduces test time and facilitates reuse.  The more that a code segment’s behavior depends on the current state of the program, the more testing is required to validate that it performs correctly under all circumstances.

Efficiency. Code efficiency equates to solid business practices. Solid business practices equate to dollar savings. Dollar savings equate to a well-run business. Let JASE help you get to that level with your business. Contact our team today to understand how we can help your business get to that point.

image source: Flavio Takemoto

Comments are closed.