Thursday, July 9, 2009

The Equal Sign and Double-Equal Sign

The use of equal sign and double-equal sign is really an interesting topic in programming.
In computer programming, the assignment operation and the equal-comparison operation are two distinct operations. How to 'represent' these two operations ? The equal sign(s) plays an very important role.
In some older programming languages such as BASIC & COBOL, single equal sign is used for both assignment and equality comparison. For example, here is a COBOL snippet:

  COMPUTE X = (5 + 1) * (5 - 1).   
  IF X = 24
  THEN DISPLAY 'YES, 24'
  ELSE DISPLAY 'NOT 24'
  END-IF.


Pascal, as an language for teaching programming, wants to tell the programmer the difference between the assignment operation and the equal-comparison operation. Pascal uses colon-equal (i.e. :=) as assignment operation and single-equal sign as equality comparison. Here is a Pascal snippet:

  x := (5 + 1) * (5 - 1) ; { Assignment uses := } 
  if( x = 24 )             { Equal comparison uses = }   
  then writeln('Yes, 24');
  else writeln('Not 24') ;


Distinguishing assignment and equal-comparison is of great merit in programming. Many modern programming languages agree with this theory. In C programming lanaguage, however, an alternate approach is used. She uses single-equal sign as assignment operator and double-equal signs as the equality comparison operator. C++ and Java also follow this approach. Here is a C snippet:

  x = (5 + 1) * (5 - 1) ;  /* Assignment uses = */
  if( x == 24 )            /* Equal comparison uses == */    
       printf('Yes, 24');
  else printf('Not 24') ;

Why choose single-equal sign (instead of colon-equal or double-equal or else) as assignment operator ? The answer is for efficiency. It was told that assignment operation is a type of very frequent operation. Using a single character (instead of two or more characters) can gain speed in writing programs. Programmers input fewer keystrokes.

Although efficiency is the advantage, this violate the normal behavior of human beings. Most human beings on Earth learns arithmetics and mathematics at his very early age. When learning arithmetics, the single-equal sign represents EQUAL. Some will learn mathematics for more than 10 years before learning programming. As a result, treating single-equal sign as EQUAL is nearly an intrinsic action of human being. Here is a very simple mathematical equation:

      x + 3 = 9        x plus 3 equals 9    
      x = 9 - 3        x equals 9 minus 3
      x = 6            x equals 6


Imaging that, in the middle of the night, you are woken up by an emergency pager call to debug this program snippet:


  x = (5 + 1) * (5 - 1) ;   
  if( x = 24 )      // The programmer intends for an equal comparsion   
       printf('Yes, 24');
  else printf('Not 24') ;
Even experienced programmer may not spot the bug instantly. This is because they intrinsically treat single-equal sign as EQUAL.

To solve this problem, some suggest to put the constant in the left hand side when doing equality comparison such as this snippet:

  x = (5 + 1) * (5 - 1) ;   
  if( 24 = x )     // The compiler will shouts    
       printf('Yes, 24');
  else printf('Not 24') ;
With this method, the compiler will certainly shouts at you. Then, the programmer can fix the bug as:

  x = (5 + 1) * (5 - 1) ;   
  if( 24 == x )
       printf('Yes, 24');
  else printf('Not 24') ;
But, reading "twenty-four equals x" sounds not so comfortable as reading "x equals twenty-four".

Also, this method cannot be used when comparing two variables like this one:
  x = 5 + 1 ;   
  y = 5 - 1 ;
  z = x     ;
  if( x = y )     // Some compiler will assume an assignment   
       printf('the same') ;   
  else printf('different');   
Even the compiler gives you warning, the programming may overlook, especially when there are thousands of warnings.

In my programming life, I have another method to tackle this problem. I use SPACE. Look at this equation:
      x + 3 = 9   
      x = 9 - 3
      x = 6
Do you notice that there is SPACE in both the LEFT and RIGHT hand side of the equal-sign ? Many human beings will do the same. So, my method is to put a space in BOTH the LHS and RHS when doing equality comparison. When doing assignment operation, I put space ONLY at the RHS of the assignment operator like this:

  x= 5 + 1 ;   
  y= 5 - 1 ;
  z= x     ;                // No space at LHS of =
  if( x == y )              // Spaces at both LHS and RHS of ==   
       printf('the same') ;
  else printf('different');

If you adopt this programming style and make it as a habit, you will feel very odd when seeing this:


  x= 5 + 1 ;
  y= 5 - 1 ;
  z= x     ;
  if( x = y )
       printf('the same') ;   
  else printf('different');
This is because putting spaces at both the left & right side of the SINGLE equal sign is NOT your programming habit. There must be something wrong. You can then get aware that you should use double-equal sign.

Alvin SIU
2009-07-09
Copyright/Licence Information:
All information and coding in this article is offered at no charge for NON-COMMERCIAL PERSONAL USE only.
This blog and the coding is copyright.
Reproduction of this blog and its coding in whole or in part in paper or digitally or in any other forms without the explicit written permission of the author is strictly prohibited.

Disclaimer:
All information in this article is distributed "as is" and is UNSUPPORTED.
NO WARRANTY of any kind is expressed or implied.
You use AT YOUR OWN RISK.
The author will not be liable for any data loss, damages, and loss of profits or any other kind of tangible or intangible loss while using or misusing wholly or partly of the information.

Duplicate Open Current Folder in a New Window

Sometimes after I opened a folder in Win7, I would like to duplicate open the same folder again in another explorer window. Then, I can ope...