Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Is cleaner always better?

Name: Anonymous 2009-07-22 10:22

Well, I make a student in web development, and my professor insists on using the longer version of things when writing PHP simply because he is used to it. Now, I have kind of gotten used to writing redundant code.

Here's an example of Me vs. My Professor:

Him


   if(!get_magic_quotes_gpc()) {
    $username = addslashes($username);
   }

   $q = "select password from users where username = '$username'";
   $result = mysql_query($q,$conn);
   if(!$result || (mysql_numrows($result) < 1)){
      return 1; //Indicates username failure
   }


Me


    !get_magic_quotes_gpc() ? $username = addslashes($username) : TRUE;

    $query = "SELECT password FROM users WHERE username = '$username'";
    $result = mysql_query($query,$connection);
   
    !$result || (mysql_num_rows($result) < 1) ? '1' : '2';


Which way do you prefer writing it in?

Note

If you see any errors, please feel free to correct them.

Name: Anonymous 2009-07-23 4:31

>>1
get_magic_quotes_gpc()
If your professor uses magic quotes tell him to go and get fucked, you are both as bad as each other.
$username = addslashes($username)
addslashes is not secure for database querying. Enjoy your injection.
SELECT password FROM users
DO NOT fold password comparison out to the CGI. There should never be a reason to retrieve a password from a database, for any reason, in any form.
mysql_query
Don't use libmysql, mysqli was created for a reason.
return 1; //Indicates username failure
True indicates failure? No No No.

It's quite clear you don't have a solid grounding in basic Computer Science concepts and are just trying to wing your way through it. I strongly recommend you go back to basics and read a great introductory text, Structure and Interpretation of Computer Programs by Gerald J. Sussman, Harold Abelson with Julie Sussman. The text can conveniently be accessed online free of charge at the location I have included below. Good luck with your future studies!

http://mitpress.mit.edu/sicp/

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List