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

Pages: 1-

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-22 10:25

The professors code is better. It does not try to use quips and beginner witticisms/syntactic sugar to impress the maintainer. If follows convention and is easier to maintain.

Name: Anonymous 2009-07-22 10:27

>>1
Ignoring that this is a PHP thread: using if is clearer.
I prefer using "predicate?true:false" only when the value is not thrown away.

Name: Anonymous 2009-07-22 10:27

>>2 here

If you do not want to introduce the chance of false appeal to supposed authority you should ask "Here is code from two people of the same age. Which is better?."

Name: Anonymous 2009-07-22 10:31

>>4
The second one is cockier and more annoying to read. The first one feels like the author knows what they're doing, even though I prefer the less conservative use of whitespace in the second one.

Name: OP 2009-07-22 10:35

Thanks guys. I will try to figure out a way to get the best of both worlds.

Name: Anonymous 2009-07-22 11:08

I noticed an error. You are using MySQL.

Name: Anonymous 2009-07-22 11:24

>>7

I see what you did there.

Name: Anonymous 2009-07-22 11:33

? :
This should never have been brought into existence.

Name: Anonymous 2009-07-22 11:49

>>9

Why is that?

Name: Anonymous 2009-07-22 12:07

>>9
Ternary operators are the shit, bro.
Though you can replace a?b:c by a and b or c.

Name: Anonymous 2009-07-22 12:11

>>11
Yeah, that's what I'm saying, ternary ?: is shit.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-22 12:25

ternary operators are very useful: there places in the code where they come handy like:

 printf("The buffer is allocated %u bytes with %s",bufbytes,is_set?"content_str":gen(new_content));
--instead of
if(is_set){
 printf("The buffer is allocated %u bytes with content_str",bufbytes);
}else{  printf("The buffer is allocated %u bytes with %s",bufbytes,gen(new_content));}



______________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
When despotism has established itself for ages in a country, as in France, it is not in the person of the king only that it resides. It has the appearance of being so in show, and in nominal authority; but it is not so in practice and in fact. It has its standard everywhere. Every office and department has its despotism, founded upon custom and usage. Every place has its Bastille, and every Bastille its despot. The original hereditary despotism resident in the person of the king, divides and sub-divides itself into a thousand shapes and forms, till at last the whole of it is acted by deputation. This was the case in France; and against this species of despotism, proceeding on through an endless labyrinth of office till the source of it is scarcely perceptible, there is no mode of redress. It strengthens itself by assuming the appearance of duty, and tyrannises under the pretence of obeying.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-22 12:25

ternery oopereturs ere-a fery useffool: zeere-a pleces in zee cude-a vhere-a zeey cume-a hundy leeke-a:

 preentff("Zee booffffer is elluceted %u bytes veet %s",booffbytes,is_set?"cuntent_str":gee(noo_cuntent));
--insteed ooff
iff(is_set){
 preentff("Zee booffffer is elluceted %u bytes veet cuntent_str",booffbytes);
}ilse-a{  preentff("Zee booffffer is elluceted %u bytes veet %s",booffbytes,gee(noo_cuntent));}



______________________________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
Vhee desputeesm hes istebleeshed itselff fur eges in a cuoontry, es in Frunce-a, it is nut in zee persun ooff zee keeng oonly thet it reseedes. Bork Bork Bork! It hes zee eppeerunce-a ooff beeeng su in shoo, und in numeenel oothureety; boot it is nut su in precteece-a und in fect. Bork Bork Bork! It hes its stunderd iferyvhere-a. Bork Bork Bork! Ifery ooffffeece-a und depertment hes its desputeesm, fuoonded upun coostum und usege-a. Bork Bork Bork! Ifery plece-a hes its Besteelle-a, und ifery Besteelle-a its desput. Bork Bork Bork! Zee ooreeginel heredeetery desputeesm reseedent in zee persun ooff zee keeng, deefides und soob-deefides itselff intu a thuoosund shepes und furms, teell et lest zee vhule-a ooff it is ected by depooteshun. Bork Bork Bork! Thees ves zee cese-a in Frunce-a; und egeeenst thees speceees ooff desputeesm, pruceedeeng oon thruoogh un indless lebyreent ooff ooffffeece-a teell zee suoorce-a ooff it is scercely percepteeble-a, zeere-a is nu mude-a ooff redress. Bork Bork Bork! It strengzeens itselff by essoomeeng zee eppeerunce-a ooff dooty, und tyrunneeses under zee pretence-a ooff oobeyeeng.

Name: Anonymous 2009-07-22 12:28

>>13
IOW, it is useful in your shit language. Sorry, don't use a shit language.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-22 12:31

>>15 What is your ``non-shit' language? I'd like to take a closer look.

_______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
In hatred as in love, we grow like the thing we brood upon. What we loathe, we graft into our very soul.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-22 12:31

>>15 Vhet is yuoor ``nun-sheet' lungooege-a? Bork Bork Bork! I'd leeke-a tu teke-a a cluser luuk. Bork Bork Bork!

_______________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
In hetred es in lufe-a, ve-a groo leeke-a zee theeng ve-a bruud upun. Bork Bork Bork! Vhet ve-a luezee, ve-a grefft intu oooor fery suool.

Name: Anonymous 2009-07-22 12:34

>>16
I don't believe you would care to take a look, given your posting here. Troll someone else.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-22 12:43

>>18 I'm curious to see what you compare my "shit language" to. Your post >>15 seems condescending and biased, without mentioning details.



___________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Stop having subversive thoughts.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-22 12:43

>>18 I'm cooreeuoos tu see-a vhet yuoo cumpere-a my "sheet lungooege-a" tu. Bork Bork Bork! Yuoor pust >>15 seems cundescendeeng und beeesed, veethuoot menshuneeng deteeels. Bork Bork Bork!



___________________________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
Stup hefeeng soobferseefe-a thuooghts.

Name: Anonymous 2009-07-22 15:05

>>19
The problem is that the language (pointlessly) creates two things, statements and expressions, pathologically requiring ?: in some cases. The semantics of ?: is conceptually if-then-else but because of the syntactic problems created from the separation of statements and expressions, this additional operator is required to make "concise" statements.

Summary:
I want an if-then-else construct here, but I can't use if-then-else. Hurrrr, I know, I'll create a new operator!

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-22 15:09

>>21
 ternary operator is in fact more universal then IF/ELSE and i prefer to use it if the task requires heavy use of IF's
Its also more concise and can be used everywhere as short form of if-then-else.

_______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Compiler: A program which produces executables.
Runtime:A program which loads another program as top execution layer
Virtual machine:A program which executes bytecode
Interpreter:A Virtual Machine which runs scripts
JIT:A program which runs scripts and temporary compiles them into memory
Oh no! A young masked interrupt, holding up the bus.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-22 15:10

>>21
 ternery ooperetur is in fect mure-a uneefersel zeen IF/ILSE und i preffer tu use-a it iff zee tesk reqooures heefy use-a ooff IF's
Its elsu mure-a cunceese-a und cun be-a used iferyvhere-a es shurt furm ooff iff-zeen-ilse-a. Bork Bork Bork!

_______________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
Cumpeeler: A prugrem vheech prudooces ixecootebles. Bork Bork Bork!
Roonteeme-a:A prugrem vheech lueds unuzeer prugrem es tup ixecooshun leyer
Furtooel mecheene-a:A prugrem vheech ixecootes bytecude-a
Interpreter:A Furtooel Mecheene-a vheech roons screepts
JIT:A prugrem vheech roons screepts und tempurery cumpeeles zeem intu memury
Ooh nu! Bork Bork Bork! A yuoong mesked interroopt, huldeeng up zee boos.

Name: Anonymous 2009-07-22 15:18

>>1
: TRUE;
NO THANK YOU

Name: Anonymous 2009-07-22 15:24

>>9
Don't even talk about proper conditionals like that.

Fun fact: as far as I can tell, John McCarthy is indirectly responsible for this thing's presence in C and many other languages, since he got it added to Algol.

Name: Anonymous 2009-07-22 16:51

>>22
ternary operator is in fact more universal then IF/ELSE
Semantically they are the same. The problem is languages that make a distinction between statements and expressions. There need only be if-then-else.

(+ 5 (if (idiot? FV) 5 0)) => 10

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-22 17:00

"Semantically they are the same."
Except you can't use if(){}else{} in many places where choices can be made.



______________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Compiler: A program which produces executables.
Runtime:A program which loads another program as top execution layer
Virtual machine:A program which executes bytecode
Interpreter:A Virtual Machine which runs scripts
JIT:A program which runs scripts and temporary compiles them into memory
It is indeed a precious gift to understand the forces that guide oneself.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-22 17:00

"Semunteecelly zeey ere-a zee seme-a."
Ixcept yuoo cun't use-a iff(){}ilse-a{} in muny pleces vhere-a chueeces cun be-a mede-a. Bork Bork Bork!



______________________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
Cumpeeler: A prugrem vheech prudooces ixecootebles. Bork Bork Bork!
Roonteeme-a:A prugrem vheech lueds unuzeer prugrem es tup ixecooshun leyer
Furtooel mecheene-a:A prugrem vheech ixecootes bytecude-a
Interpreter:A Furtooel Mecheene-a vheech roons screepts
JIT:A prugrem vheech roons screepts und tempurery cumpeeles zeem intu memury
It is indeed a preceeuoos geefft tu understund zee furces thet gooeede-a ooneselff.

Name: Anonymous 2009-07-22 17:17

>>26
You mean that there need only be the ternary conditional.

Name: Anonymous 2009-07-22 20:04

The better a programmer you are the easier your code is to read.
Your professor's code is exponentially better. You, OP, are on the way to becoming as horrible at programming as that annoying faggot who shall not be named.

Name: Anonymous 2009-07-22 20:30

>>27
... In your shit language.

Name: Anonymous 2009-07-22 20:41

>>29
OK.

Name: Anonymous 2009-07-23 1:32

You're both not using provider-parameterized queries, so you're both equally as retarded.

Name: Anonymous 2009-07-23 3:09

>>27
I don't think you understand what semantic means...

Name: Anonymous 2009-07-23 3:16

Your last line doesn't return 1 nor continue, it's different from his code. You can't use a ternary there unless you want to return 2 as well:

return !$result || (mysql_num_rows($result) < 1) ? '1' : '2';

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/

Name: Anonymous 2009-07-23 5:30

>>36
-1, informative

Name: Anonymous 2009-07-23 8:28

addslashes is not secure for database querying.
>>36 is right u need 2 use mysql_real_escape_sql_query_string_2 2 encode ur db commands otherwise u may b hacked :)

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