Vendor's Code...



  • Digging through some nasty inherited code, I come across numerous instances of WTF.

    And not WTF because the business processes are WTF, but because it seems like the vendor tried to choose the most complex route when addressing even the most simplest requirements.

    I'll be posting some snippets here often, because there's no short supply of WTF here. But here's the line of code that inspired me to start the topic.

    string query = string.Format("%{0}%", string.Empty))
    

    It doesn't get modified elsewhere, and is used in a where clause. But then, why bother having the clause?!

    I'm just going to fix this silently, but this is just the kind of code that makes me weary of this profession and life in general...


  • Banned

    What does the % do here?


  • Discourse touched me in a no-no place

    @Gaska said:

    What does the % do here?

    SQL wildcard at a wildguess. (i.e. it's not part of the .Format parser, but the SQL one that query ends up in.)



  • What does something like "% %" would do to the results of a query?



  • PJH is right. So the result is that the query object's value is merely "%%"

    So why then not just initialize it as:

    string query = "%%";


  • @Eldelshell said:

    What does something like "% %" would do to the results of a query?

    That would yield results where the field has a space anywhere in the value.

    Further digging reveals that the intent was more or less like a Is Not Null clause. But remember, these people don't ever do anything in a simple manner.



  • @AgentDenton said:

    So why then not just initialize it as:

    string query = "%%";

    That's not enterpriseyTM enough.

    They're preparing for something like

    string query = string.Format("%{0}%", string.Concat(string.Empty, string.Empty, "", string.Empty, "  ".Trim()));


  • Probably that string's inserted into a SQL "LIKE" statement. WHERE blah LIKE "%poo%".

    But since the string is empty, the LIKE just matches every single row. Hopefully whatever DB you use is smart enough to optimize-out the compares at least.


  • Banned

    I love how you never read earlier posts before replying, which makes you a frequent victim of Hanzo.



  • @AgentDenton said:

    most complex route when addressing even the most simplest

    I see you've followed suit.



  • @rc4 said:

    I see you've followed suit.

    Touché 😄



  • Here's another little anti-pattern I've noticed. They must have adopted some kind of reverse paid-by-the-line strategy, because some of the code stretches horizontally to two or three times the width of my screen (at a resolution of 1600x900).

    No it's not minified, single line stuff. It's stuff like this (from a proper .cs file):

    if (request.parameters.ContainsKey("recordid")) { if (Guid.TryParse((string)request.parameters["recordid"], out id)) { recordId = id; } }
    

    It's like they actively like to make code hard to read, since this pattern extends to Linq queries as well...



  • That's a matter of taste and practice. Someone that is used to that single-line style would understand it better than a multi-line form.

    (I'm not sure whether I'm playing Devil's advocate or not)

    Btw, the if-within-if pattern actually makes sense in languages without short-circuit logical and, like VBA6.


Log in to reply