SELECT nothing



  • I started a new job as a PHP monkey a couple of weeks ago, got tasked with maintaining a complex application the company developed for the main client. ("Why give such responsibility to a new guy?", I don't know either.) It was not as bad as most of the things I've seen here, but it's still a mess... The original developer acknowledges all these as issues, but cannot be bothered to fix them. Though the boss told me to fix all this when I have free time, so it's not all bad.

    No consistent indent, typical pattern of a segment that has no control structures like if/for, so what should be a straight line looks like this (with visualised spaces and tabs):

    Variables with meaningful names like $stalingrad, $shoe, $alonzo_shishkabob, $weeeee , $tralalala , $papa, $mama, $dada, $baba
    Accompanied by comments like // echo "aaaaaaa: $aaaa<br>"; and //echo "why the fuck is this here?";

    Classes named "foo_create_new_bar" redefined with slight variations in five different files, encompassing functionality to create a new bar, edit an existing bar, delete a bar, list all baz contained in a bar, and exchange bars of two separate foos if an unrelated global baz is set

    No version control, even though it's available on the local dev network. Great fun ensued when this ticket was filed: "On the live server, doing bar on an existing foo no longer works. It does work on the dev server. Find the differences in the code and resolve them."
    (Directory tree is five layers deep and about twenty folders wide. Had to FTP it all down to local workstation and diff with the dev copy, then stare at the 2 MB diff file and try to find the problem. No success yet.)

    And the bit that actually made me exclaim "WTF" out loud:

    SELECT fields, 
     more_fields,
     '' as nothing,
     pr_status as zenon,
     0 as fuhrer
    FROM table
    WHERE cond
    

    UNION

    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    1 as fuhrer

    FROM table
    WHERE reversed_cond



  • Sounds very much like some code I got to rewrite back in 2001. Except your guy seems to understand the importance of wrapping lines at some point - potentially, even meaningful ones (although maybe not so much.) The mess I had to work with had many lines over 200 characters long.



  • Sorry to take this OT, but what editor are you using? It looks much nicer than TextWrangler.



  • @teqman said:

    Sorry to take this OT, but what editor are you using? It looks much nicer than TextWrangler.
    I use EditPad Pro, the font in that screenshot is Consolas.



  • @tgape said:

    Sounds very much like some code I got to rewrite back in 2001. Except your guy seems to understand the importance of wrapping lines at some point - potentially, even meaningful ones (although maybe not so much.) The mess I had to work with had many lines over 200 characters long.

     

     

    Manually wrapping lines is just as bad as having 200 character lines in the first place. A manual wrapping that looks good on your screen might not look good on someone else's, and modifying the line breaks the formatting.  Modern text editors have automatic wordwrap for a reason. 

     

    Aside from which, the solution to long lines is not wrapping, it's breaking it down into multiple lines of code.  



  • @SeekerDarksteel said:

    Aside from which, the solution to long lines is not wrapping, it's breaking it down into multiple lines of code.  

    Oh yeah?

    [code]DialogResult answer = MessageBox.Show(this, "This is a long line of code.  Do you want to break it down into multiple lines?", "Go On, Let's See You Try", MessageBoxIcon.Question, MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button2);[/code]

     



  • @upsidedowncreature said:

    Oh yeah?

    <font face="Lucida Console" size="2">DialogResult answer = MessageBox.Show(this, "This is a long line of code.&nbsp; Do you want to break it down into multiple lines?", "Go On, Let's See You Try", MessageBoxIcon.Question, MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button2);</font>

     

    DialogResult answer =
        MessageBox.Show(this,
                        "This is a long line of code. "
                        + "Do you want to break it down into multiple lines?",
                        "Go on, let's see you try.",
                        MessageBoxIcon.Question,
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxDefaultButton.Button2);


  • @upsidedowncreature said:

    @SeekerDarksteel said:

    Aside from which, the solution to long lines is not wrapping, it's breaking it down into multiple lines of code.  

    Oh yeah?

    <font size="2" face="Lucida Console">DialogResult answer = MessageBox.Show(this, "This is a long line of code. Do you want to break it down into multiple lines?", "Go On, Let's See You Try", MessageBoxIcon.Question, MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button2);</font>

     

     

    <font size="2" face="Lucida Console">DialogResult answer = MessageBox.Show(this,</font>

    <font size="2" face="Lucida Console">"This is a long line of code." + </font>

    <font size="2" face="Lucida Console">" Do you want to break it down into multiple lines?", </font>

    <font size="2" face="Lucida Console">"Go On, Let's See You Try",</font>

    <font size="2" face="Lucida Console"> MessageBoxIcon.Question, </font>

    <font size="2" face="Lucida Console">MessageBoxButtons.YesNoCancel, </font>

    <font size="2" face="Lucida Console">MessageBoxDefaultButton.Button2);</font>

     

    Done.



  • @DCoder said:

    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    0 as fuhrer
    FROM table
    WHERE cond

    UNION

    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    1 as fuhrer
    FROM table
    WHERE reversed_cond


    that's not "SELECT nothing" though; it's actually equivalent to

    SELECT fields,
     more_fields,
     '' as nothing,
     pr_status as zenon,
     (!cond)::int as fuhrer
    FROM table


  • @lanzz said:

    @DCoder said:
    SELECT fields, 
     more_fields,
     '' as nothing,
     pr_status as zenon,
     0 as fuhrer
    FROM table
    WHERE cond
    

    UNION

    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    1 as fuhrer
    FROM table
    WHERE reversed_cond

    that's not "SELECT nothing" though; it's actually equivalent to
    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    (!cond)::int as fuhrer
    FROM table

    Actually it isn't, Your version returns records where cond would be null, the union version does not.



  • @teqman said:

    ...nicely formatted MessageBox code...
    Of course that's how I'd write it too but this is using manual linebreaks which SeekerDarkSteel seemed to be implying was bad.


  • @upsidedowncreature said:

    @SeekerDarksteel said:

    Aside from which, the solution to long lines is not wrapping, it's breaking it down into multiple lines of code.  

    Oh yeah?

    <FONT face="Lucida Console" size=2>DialogResult answer = MessageBox.Show(this, "This is a long line of code.&nbsp; Do you want to break it down into multiple lines?", "Go On, Let's See You Try", MessageBoxIcon.Question, MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button2);</FONT>

     

    Long lines are relative.  What constitutes a long line for you?

    The best solution here is a comprimise, build the message in a string builder, along with perhaps even the title, leave the enumerators as is.  This gives you a common length for all similar message boxes regardless of message, hmm sounds like a good candidate for a function for your common message box, reusable code FTW; the extra length on the line no longer really matters because it is done only once, not many times.



  • @KattMan said:

    Long lines are relative.  What constitutes a long line for you?

    Anything you need a second punchcard for.



  • @KattMan said:

    @upsidedowncreature said:

    @SeekerDarksteel said:

    Aside from which, the solution to long lines is not wrapping, it's breaking it down into multiple lines of code.  

    Oh yeah?

    <font size="2" face="Lucida Console">DialogResult answer = MessageBox.Show(this, "This is a long line of code.&nbsp; Do you want to break it down into multiple lines?", "Go On, Let's See You Try", MessageBoxIcon.Question, MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button2);</font>

     

    Long lines are relative.  What constitutes a long line for you?

    The best solution here is a comprimise, build the message in a string builder, along with perhaps even the title, leave the enumerators as is.  This gives you a common length for all similar message boxes regardless of message, hmm sounds like a good candidate for a function for your common message box, reusable code FTW; the extra length on the line no longer really matters because it is done only once, not many times.

     

    Or you could have the message in a resource so that you can properly internationalize your app.



  • @KattMan said:

    Long lines are relative.  What constitutes a long line for you?

    At 1600px of width, my lines fit 170 chars, but I never go there because it's highly annoying to read i.e. maintain.



  • @DCoder said:

    And the bit that actually made me exclaim "WTF" out loud:
    SELECT fields, 
     more_fields,
     '' as nothing,
     pr_status as zenon,
     0 as fuhrer
    FROM table
    WHERE cond
    

    UNION

    SELECT fields,
    more_fields,
    '' as nothing,
    pr_status as zenon,
    1 as fuhrer

    FROM table
    WHERE reversed_cond

    I have something that isn't quite this, but still looks kind of WTFy:

    SELECT field,blah,something_else,'0' as field1, '9' as field6, 'STUPID CONSTANT TEXT' as field11, 'WTF' as field14
    FROM wtf_table

    Notice the constants. This was because my boss wanted to simplify use of constants, so that instead of managing constants in the app, we have to put all those constants in the query. It sounds stupid to me, as all those constants are travelling to the DBMS, and then returning with the results, adding up to the actual data transfer between the DB and the application server. Oh, and multiplied by the rowcount that select generates. But hey, it's "simple"!


Log in to reply