The importance of variable names.



  • The following code was submitted by a third year student as a lab assignment.

    The assignment was intended to expose them to PHP, and to deal with passing form fields via PHP,

    .Ignore the errors.  These aren't CS students, and I can teach logic and correctness.

    What I want to talk about is the importance of variable names to readability.


    This is what the student gave me:

    $HCUNnP = "Student Password";
    $USub = $_GET["Uname"];
    $PSub = $_GET["pword"];
    $Val = "Hello Student";
    $Den = "Username or Password are INCORRECT! Please Try Again!";
    $Res = "You need to come in via this form [LINK]";

    $UnPSub = $USub." ".$PSub;
    if (isset($UnPSub))
    {
    if ($UnPSub == $HCUNnP)
    echo $Val;
    else
    echo $Den;
    }
    else
    echo $Res;



    This is what I gave the student back, BEFORE correcting any logic errors.  I simply substituted variable names.

    $UserName_Password = "Student Password";
    $SubmittedUserName = $_GET["Uname"];
    $SubmittedPassword = $_GET["pword"];
    $Validation = "Hello Student";
    $Denial = "Username or Password are INCORRECT! Please Try Again!";
    $Restricted = "You need to come in via this form [LINK]";

    $temp = $SubmittedUserName." ".$SubmittedPassword;
    if (isset($temp))
    {
    if ($temp == $UserName_Password)
    echo $Validation;
    else
    echo $Denial;
    }
    else
    echo $Restricted;


    Do you see the difference?

    I'll grant that $temp could be better, but I really don't want to call it what it is, as it would make me cry.

    The first is almost incomprehensible.  The second reads like a good description of what the code is supposed to do.



  • What I want to talk about is the use of variables to hold non-repeated literal strings.



  • Yeah, that too, but there were more important issues.

     



  • @mightybaldking said:

    Do you see the difference?

    Yes, the first way is much better.  It reads like a soap opera, here's Val and Den, right now Val and Den are not on speaking terms, suddenly a stranger called Res arrives in the neighbourhood!  Will Val like Res?  Will Den forgive Val?  Tune in next time on TDWTF!

    *And* it's a functional bit of code!  What is your problem?!



  • For what it's worth, back when I was in school, I submitted a program which used variable names like Fred, Ted, Mike, Joe, Sheila, Amanda, and Bill.  I think my subroutine was named Marcus.  I got a perfect score - despite the fact that every other line after the header block was a comment, and the text in the comment simply matched the text in the code line below it.  I believe this was the least legible of any C code I've ever written - it was certainly an attempt at such.

    This was part of an experiment to test the hypothesis that nobody was actually reading the student program submissions.  My classmate's part was even better - complete with pneumonic variable names.  (He got a perfect score, too.)

    In any event, if I were grading the coding submission you gave above, I probably would cry, if it were a repeat performance.  Of course, if it was the student's first assignment, I'd just bring out the red pen... and use it a lot.



  • @tgape said:

    complete with pneumonic variable names
    So names like "alveoli" and "bronchiole"?



  • Professors / TA's never look at your code. They just run your project and grade you on what it does. One of my professors, however, did use some sort of software to analyze the students' code to check whether two projects were too similar, but that's about it.

    Hmm.. this gives me an idea. If I programmed a trojan horse into one of my projects, I don't think anybody would find out!

     



  • @Huf Lungdung said:

    Professors / TA's never look at your code. They just run your project and grade you on what it does

     

    They don't always even do that. When I was at uni, one of our projects was to write a couple of programs to solve the '8 queens puzzle' and the 'knights puzzle' using recursion. The available platform was a BASIC home computer, with on a 2MHz 6502 computer (this was a while ago). You could only run them in a shared computer lab, so there was no way you could run them to completion without being beaten up unless you did lots of extra work to allow partial state to be saved, (in 4 hours either program would only be a couple of percent of the way through). I actually rewrote them in 6809 assembler (still using recursion) and they took about 10 minutes to run to completion. I submitted both versions. 

    There is no way the teacher ran everyone's programs to completion or he'd still be there today, and he couldn't run my assembler submission as he didn't have the right type of computer. I did get full marks though. I suspect I just blinded him with my l337-ness :) . I still wonder what would have happened if I'd submitted a space-invaders program instead...

     

     



  • $temp = $SubmittedUserName." ".$SubmittedPassword;
    if (isset($temp))

    Always returns true.

    e.g.:

    root# php -r '$x= null . " " . null;  var_dump(isset($x));'
    bool(true)



  • $username = "Student"
    $password = "Password";
    
    if (isset($_GET["Uname"]) && isset($_GET["pword"])) {
      if ($_GET["Uname"] == $username && $_GET["pword"] == $password)
        echo "Hello Student";
      else
        echo "Username or Password are INCORRECT! Please Try Again!";
    } else
      echo "You need to come in via this form [LINK]";
    


  • @merreborn said:

    $temp = $SubmittedUserName." ".$SubmittedPassword;
    if (isset($temp))

    Always returns true.

    e.g.:

    root# php -r '$x= null . " " . null;  var_dump(isset($x));'
    bool(true)

     Yes, I discussed that with him after the fact.

     

     



  •  Why was DaveK banned?  That was funny.

     



  • That's his signature.



  • @mightybaldking said:

    Why was DaveK banned?
    I- I can't resist... for the lulz




  • @DOA said:

    @mightybaldking said:

    Why was DaveK banned?
    I- I can't resist... for the lulz


    Heh, I finally got one!


  • @merreborn said:

    $username = "Student"
    $password = "Password";
    

    if (isset($_GET["Uname"]) && isset($_GET["pword"])) {
    if ($_GET["Uname"] == $username && $_GET["pword"] == $password)
    echo "Hello Student";
    else
    echo "Username or Password are INCORRECT! Please Try Again!";
    } else
    echo "You need to come in via this form [LINK]";

     

     

    In keeping with the spirit of the assignment, that should really be:

     

    echo "Hello ".$_Get["Uname"]; 
     
    but the whole thing is contrived anyways. 



  • This code sort-of reminds me of a "Computer Science" (read: basic Java skills) competition in my area. The code samples they provide also use terrible names (like c, it, that, james, and big; and that's not even to obfuscate the code), include useless statements, and don't even follow Java coding conventions. Here's an example:

    What does this code output?
    int c=0;
    for (c=24; c>10; c=c-3)
    {
      c=c-3;
    }
    out.println(c);
    

    (a) 42
    (b) 9
    (c) 6
    (d) c
    (e) There is no output due to a runtime error.



  • @AltSysrq said:

    This code sort-of reminds me of a "Computer Science" (read: basic Java skills) competition in my area. The code samples they provide also use terrible names (like c, it, that, james, and big; and that's not even to obfuscate the code), include useless statements, and don't even follow Java coding conventions. Here's an example:

    What does this code output?

    int c=0;
    for (c=24; c>10; c=c-3)
    {
      c=c-3;
    }
    out.println(c);
    

    (a) 42
    (b) 9
    (c) 6
    (d) c
    (e) There is no output due to a runtime error.

    I know it's e, but don't ask me how i know, I'm not certain myself.


  • @tgape said:

    For what it's worth, back when I was in school, I submitted a program which used variable names like Fred, Ted, Mike, Joe, Sheila, Amanda, and Bill. 
     

    In college, I once had a program in Pascal (ick!) with string variables named things like twine, yarn, etc...

     

    Completely OT:  Why does this forum keep changing the buttons on the articles?  Sometimes there are Reply and Quote buttons right on the articles and sometimes there are Reply, Favorite and Contact buttons.



  • @dwilliss said:

    Completely OT:  Why does this forum keep changing the buttons on the articles?  Sometimes there are Reply and Quote buttons right on the articles and sometimes there are Reply, Favorite and Contact buttons.

    It depends how cool you are.  The more posts you make on teh site, the more buttons you get on your toolbar.  Some of the later buttons are really good, e.g. after "Reply, Favorite, Contact", the next ones you get are "Delete other user's post", "Delete other user", and "Bring me delicious caek". Stop posting for a while, and the number starts to decrement again.  When you get so many buttons that they overflow off the right-hand side and crash your browser, you get banned.


  • We once had a PHP assignment where we would read values from a database and present those in a drop-down. When the teacher wanted to grade these assignments, the database server went down hard and didn't come up again. He went on to grade the assignments anyway.

    When he found a website that still presented the drop-down, that student was asked to enter the office. The student could not comprehend why he had failed the assignment when the drop-down worked!



  • @steenbergh said:

    The student could not comprehend why he had failed the assignment when the drop-down worked!
    Heh, "What do you mean fail?! My assignment even works without the database! I should get extra points!"

    All this talk about student projects reminded me of a project we had in uni. I don't remember exactly what it was about, but it was a website requiring a login written in PHP. Since part of the assignment was about security I had implemented a mechanism to invalidate an IP after so many failed login attempts. The time comes for my presentation, I say my piece and at the end they're supposed to ask me questions, if for no other reason than to make sure I didn't copy-paste the code from somewhere else. So I'm standing there waiting for a question on how my locking mechanism works, ready to wow them with the sheer brilliance of the code, when the examiner goes...

    "I see you've named your variable here $returnValue. How come?"

    "Uhmm... I'm sorry?"

    "Why did you name this variable in this function $returnValue?"

    *scratches head* "Uhmm... because... it holds the value this function returns?"

     "Excellent! Thank you for your presentation!"

    Sad part is I didn't even get a high score because I can write code, but I suck at writting academic papers. Bibliography my ass ...



  •  It's too bad that professors like my Software Engineering professor aren't more common.

     

    He handed out a programming assignment.  I got the thing to run flawlessly and turned it in.  I got it back covered in red with a score of 25%!

     

    I learned quite a bit about writing maintainable code from him...


Log in to reply