01.Blogs :
RobMiles  
Programming, gadgets and life as a lecturer in a UK university.

Exam Question 2

Well done folks. Yes, the breaks were missing, which is an interesting foible of C, C++ and Java. I'd used this in a good way:

      case 'a':
      case 'A':


This makes sure that the code will work for upper case and lower case command letters. However, it also means that the program continues when you don't want it to:

      case 'A':
            AddEntry();

      case 'd':
      case 'D':
             DeleteEntry();


Once the program has performed the AddEntry it carries on and does the delete, which is not what you want. You have to explicitly end the case clause:

      case 'A':
            AddEntry();
            break;

      case 'd':
      case 'D':
             DeleteEntry();
             break;

 
C# will not let you do this, which is nice. In C# you have to explicitly chain the cases together. Of courese, I'd probably convert the command to upper case before the switch if I was really writing this code. It makes the code smaller, faster and clearer.

The missing break is one of the most common typo based programming errors. Two others that I've seen are:

      for ( count = 0 ; count < limit ; count++ ) ;
      {
           // something in here which happens when you dont expect it
      }

and

      if ( x = 5 ) {
          // at least Java won't let you do this one
      }


Beginning programmers think that their work is done when the program compiles. Ha! If you like a good read and want to know about this kind of thing you should read Code Complete. In fact, if you haven't read this book I don't think you are a programmer at all!

And so to question 2. A couple of classes which won't compile. They are in the right source files, so what is going on here?

public class Person {
      String name ;
      public Person ( String inName ) {
            name = inName ;
      }
   }

public class SecurityGuard extends Person{
      int securityLevel;
      public SecurityGuard ( int inSecurityLevel) {
            securityLevel = inSecurityLevel ;
      }
}

I've just discovered that Steve McConnell is writing another editiion of Code Complete. Wahay! Incidently, he has also written some of the best books ever on project management. Take a look at Rapid Development if you want to find out how to really do it!

posted on Tuesday, May 25, 2004 8:17 AM by RobMiles

# @ Wednesday, May 26, 2004 11:28 AM

I kinda like the Java/C++ way of doing since fall through on selects statements can still be usefull to the programmer. Perhaps just a warning would be better. Any questions from the 2nd year exam?! :)

AndyS


 
03.UPDATE CALENDAR :
<May 2004>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR