Developer Stories: How to screw up If-then-else

Developer Stories: How to screw up If-then-else

Since it's been a couple of days since I actually talked about code (the last time was arrays, if you are keeping track ) ... today I thought I'd talk about if-then.

There was an old musical, Guys and Dolls, in which the character’s father gave him this advice:

“Then this guy is going to offer to bet you that he can make the jack of spades jump out of this brand-new deck of cards and squirt cider in your ear. But, son, do not accept this bet, because as sure as you stand there, you're going to wind up with an ear full of cider.” - Skye Masterson

What does this have to do with If - then statements? I am glad you asked me that. If this has not happened to you, it will.

  • You code a statement saying if X occurs then do this thing, else do this other thing .
  • You are absolutely sure that you want to do one thing when X and always the other one thing when not X.
  • You are wrong.

Let’s say you do this, for a class to identify students who are not passing by outputting them to a dataset that you will then print, so you can intervene.

 if points  > 70 and point ≤ 100 then output pass   ;
        else output fail ;        

Looks good, right?

What if someone gets extra credit and has a grade of 105% ? Then, they fail?

You can fix that by doing

If points  > 70 then output pass ;
     else output fail ;        

What if there is a data entry error because some doofus entered five or 5 points for the students score instead of 5 and the grade comes up as? NaN or -1?

What if the same doofus entered 500 instead of 5 for another student and now there score is 560?

My recommendation for many circumstances is to do an extra else and be specific, something like this.

 If points ? > 70 and points  =< 100 then output pass?;
             else if points => 0 and points =< 70 then output fail ;
             else output error_file ;         

First of all, I used less than or equal 70 instead of 71 because the grade could be 70.3. Second, I have it output anything unexpected to another file. I can look in my log and if that file is empty then I'm good to go. You aren't really expecting this error to happen, so you can use the same file for all of your if- then statements.

You do not always have to have a third possibility, but you should consider it

If I set the value of x at the beginning of the program to be 0, and X is automatically incremented by x++ every time the user clicks on a correct answer, then I can be pretty sure x is going to be 0 or an integer greater than 0.

If I think the probability of exceptions is very small and the probability of missing them is not something great like failing a student who should have passed, then I might skip the extra else statement.

HOWEVER, even if you don’t do have an else statement for exceptions, when you have an error in your program that you can’t figure out, one of the places you can look is to see whether there is a possibility your coding of the if - then statement had not considered?

Prediction: this will happen to you and then you will remember I told you so.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了