• r (unregistered)

    That IF FALSE THEN TRUE ELSE FALSE is a classic that must get trotted out all the time.

    Should be covered in basic programming class.

  • malfist (cs)

    If you teach someone BASIC, you cripple their ability to program.

  • Alcari (unregistered)

    I'm guilty of the second WTF a lot. In my defense, I was young and foolish.

  • Jonatan (unregistered) in reply to malfist
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

  • ammoQ (cs)

    The SUBString thingy looks a bit like a workaround. The way Oracle works, substr() in a SQL statement is processed by the SQL engine, while substr() in a PL/SQL statement is processed by the PL/SQL engine. Of course both should return exactly the same result for given input parameters; but if a bug in the PL/SQL engine caused incorrect results on some input parameters, transfering the work to the SQL engine as shown in the example might help. (Not that I know of any bug that would make this kind of function necessary)

  • real_aardvark (cs) in reply to Jonatan
    Jonatan:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

    Really?

    I can see it now: "C# -- A Better QBasic..."

  • Andy Goth (unregistered)

    Argh, I hate "x == TRUE" tests, since they don't match the language's definition of truth. If TRUE is #define'd or enum'ed (or global variable'd, heh) to be 1, but x is the product of something other than the logical operators, then this code may very well consider x to be false. (Heh, if the code then tests "x == FALSE", it may decide that x is neither true nor false, so that leaves... FILE_NOT_FOUND?)

    Examples include x being a pointer (which is true if not NULL), a number (true if not zero), or the product of bitwise operations (true if at least one bit is set).

    For this test to work as intended, the bizarre locution "!!x == TRUE" must be used so that x is "canonicalized". But somebody who knows about that trick also knows not to compare against TRUE.

    Lastly, the expression "x == TRUE" is redundant; just say "x" and be done with it!

  • Just Some Guy (unregistered)

    I'm wondering if the SUBString predecessor actually replaced a 200-line function that barely worked but was called from 1,000 places with a much simpler migration function. At least, that's what I'm hoping.

  • Macxdmg (unregistered)

    just to make sure I understand this correctly, he re-wrote SUBSTR with SUBString, and used SUBSTR in the equasion? WTF for re-writing it if he's not going to use his version.

  • Andy Goth (unregistered) in reply to Andy Goth
    Andy Goth:
    (or global variable'd, heh)
    By the way, I'm serious about the global variable thing. I once saw code that had a global variable called "success". Functions would return "success" if they succeeded or "!success" on failure. The calling functions would compare against "success" to test for success. However, at least one function got it wrong, instead opting to assign 1 or 0 to the global variable "success" to indicate its status. I think you can picture the ramifications of such a function failing. :^) ("If at first you don't succeed, redefine success.")
  • misha (cs) in reply to Jonatan
    Jonatan:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

    Agreed. I started out in QBasic too. The advantage is that after a while, you start to realise the limitations of the language yourself, because you try to do stuff and it's harder than it should be. I didn't know I needed pointers 'cos I'd never heard of a pointer, but I knew I wanted to be able to put a reference to one variable in another. And you don't truly understand the importance of structured programming until you've written some truly tangled spaghetti code yourself :)

    So you learn lots about how not to do it. And then go buy a book on a real language.

  • FredSaw (cs)

    Here's a "not" function that I cooked up back in my very early days:

      return CBool(CInt(bFlag) * -1)

  • misha (cs) in reply to Andy Goth
    Andy Goth:
    For this test to work as intended, the bizarre locution "!!x == TRUE" must be used so that x is "canonicalized".

    Surely "(bool)x" would be considerably more readable and do the same thing?

  • Phill (unregistered) in reply to real_aardvark

    I used BASIC when I was around 7 as well. Writing some horrible 2000 line monsters littered with GOTOs has given me a real appreciation for a well-designed, loosely coupled architecture. Far from destroying my programming ability, it showed me a way of thinking logically.

    I frequently come across people who obviously haven't been thought boolean logic. They get freaked out when you write things like while(!isRunning) and have empty if statements with their code in the else cause they don't understand it. As for the Oracle thing, that could have been caused by a funky implementation of SUBSTR written and taken out and a decision made not to change the existing code that pointed to the function.

    It's still a WTF though.

  • John (unregistered) in reply to ammoQ

    The other possible (valid) reason for the SUBString function is if the application once supported multiple databases. In that case, you sometimes have to wrap SQL functions in one database so you can use a common function name across all the databases.

    Still, not what I would have called the function (too much a possibility of a name conflict with another DBMS vendor) nor how I would have coded it.

  • Bob (unregistered) in reply to malfist
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    Bollocks. BASIC programmers learn to program in a nice linear manner and understand the whole input, process, output concept. It's the best possible way to start learning to program. All of the recruits we've had recently get far too clever with pointers or spaghetti objects or some fucking framework <spits> and end up tripping over themselves due to a lack of basic skills.

  • Reuben (unregistered)

    I like the fact SUBString does the same as SUBSTR, except for substrings longer then 100 characters where the output is truncated.

  • Andy Goth (unregistered) in reply to misha
    misha:
    Surely "(bool)x" would be considerably more readable and do the same thing [as !!x]?
    Yes, if you're using C++ and descendents. However I usually encounter this kind of garbage when using C, which has no bool type. But thanks, I'll keep that in mind the next time I'm using C++.
  • Dark Shikari (cs) in reply to Andy Goth
    Andy Goth:
    misha:
    Surely "(bool)x" would be considerably more readable and do the same thing [as !!x]?
    Yes, if you're using C++ and descendents. However I usually encounter this kind of garbage when using C, which has no bool type. But thanks, I'll keep that in mind the next time I'm using C++.
    Or you can do what the code I'm working on does--#define BOOLIFY(x) !!x

    :)

  • HackyKid (unregistered) in reply to Dark Shikari
    Dark Shikari:
    Or you can do what the code I'm working on does--#define BOOLIFY(x) !!x :)

    is the BOOLIFY(2+2)==3 intended? :-p

  • Mr. Shiny & New (unregistered)

    SUBString looks to me like a platform independence wrapper. If you want your SQL to work "unmodified" on different databases, you might need to wrap certain functions so that your implementation can work the same on all platforms. On your "canonical" platform the wrapper just calls the base implementation but on other platforms you might have to do something different. Not necessarily a WTF.

    My fav abuse of stored procs though is a pattern we used to see where I work: a stored proc whose only job is to do a SELECT and return a cursor. Why have the proc at all?

  • notbloodylikely (unregistered) in reply to Mr. Shiny & New
    Mr. Shiny & New:
    My fav abuse of stored procs though is a pattern we used to see where I work: a stored proc whose only job is to do a SELECT and return a cursor. Why have the proc at all?

    A few reasons to do this come to mind.

    For one, you avoid having SQL code in your application - which in turn prevents application developers from actually writing SQL, which from a DBA's perspective is a "good thing"(TM)

    Another reason may be that you've settled on a particular architecture were all database calls are going to made in a unified and consistent manner, and that method is to call a stored procedure - whether you are doing a simple insert, update, or delete; or whether you are doing something more complicated. That way you application framework only has to handle one method for communicating with the database.

  • bling (unregistered) in reply to Mr. Shiny & New
    Mr. Shiny & New:
    SUBString looks to me like a platform independence wrapper. If you want your SQL to work "unmodified" on different databases, you might need to wrap certain functions so that your implementation can work the same on all platforms. On your "canonical" platform the wrapper just calls the base implementation but on other platforms you might have to do something different. Not necessarily a WTF.

    This.

  • poochner (cs)

    Being a bit out of touch in the Oracle way, might the SUBString code have to do with getting substr processed in a different character set?

  • Jim (unregistered)

    Enough about the booleans - what's that PRIVATE macro all about?

  • Stone (unregistered) in reply to Andy Goth
    Andy Goth:
    misha:
    Surely "(bool)x" would be considerably more readable and do the same thing [as !!x]?
    Yes, if you're using C++ and descendents. However I usually encounter this kind of garbage when using C, which has no bool type. But thanks, I'll keep that in mind the next time I'm using C++.
    If you're using C from the 1980s, sure. C has had a bool type since '99.
  • Theo (unregistered)

    Well I often did (and still does) the following:

    protected boolean someComplexNameIndicatingFunctionalCheck { return true; }

    because this method is used in a particular implementation (subclass) and a peer class could return false, or a dynamic value.

  • Synonymous Awkward (unregistered) in reply to malfist
    malfist:
    If you teach someone BASIC, you cripple their ability to program.
    I see quoting Dijkstra still hasn't gone out of style (or paraphrasing, in this case).
  • Clément (unregistered)

    The "toggle" function can be quite useful if it is used as a function pointer

  • ais523 (unregistered) in reply to misha
    misha:
    Jonatan:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

    Agreed. I started out in QBasic too. The advantage is that after a while, you start to realise the limitations of the language yourself, because you try to do stuff and it's harder than it should be. I didn't know I needed pointers 'cos I'd never heard of a pointer, but I knew I wanted to be able to put a reference to one variable in another. And you don't truly understand the importance of structured programming until you've written some truly tangled spaghetti code yourself :)

    So you learn lots about how not to do it. And then go buy a book on a real language.

    Worse, I did much of my early programming on a computer with no languages installed but QBasic, and deciding I needed pointers, I actually implemented them using the low-level memory functions it provides. It took me a while, because this was before I understood floating-point numbers were used by default (it's just 'a number', after all) and spent ages trying to read them bytewise before I came to the conclusion of copying a floating-point number I only had a pointer to (strictly speaking, segment and offset for) byte-by-byte to a local floating-point variable using low-level memory routines, and then returning a copy of the local variable. Certainly not the way to program in any language.

    (A couple of years later, I discovered Pascal and then C, both of which are much better at pointer manipulation.)

  • SF (unregistered)

    Nobody seems to have commented, on the Oracle case, on the point that, even if you need a wrapper, the "SELECT ... FROM DUAL" and the context switch that is implied are pointless ... The developer could have directly returned substr(...). Run it in a loop, you'll feel the difference.

  • operagost (cs) in reply to real_aardvark
    real_aardvark:
    Jonatan:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

    Really?

    I can see it now: "C# -- A Better QBasic..."

    Automobiles: a Better Bicycle Lingerie: a Better Underoos

    Please! Get off the high horse.

  • Puckdropper (unregistered)

    Qbasic isn't exactly BASIC. It is, but it isn't. Using QB, you can learn a lot about functional programming and things like parameter passing and writing functions (ahem: procedures--functions return a single value).

    Now, real BASIC is what Dijkstra was taking about. There are no functions, only gosub. Gotos were the way to get around your code, not subroutine calls. "Information Hiding"? What's that?

  • Andy Goth (unregistered) in reply to Stone
    Stone:
    C has had a bool type since '99.
    That's news to me! Thanks. Looks like it's made available by <stdbool.h>, which I had never heard of before. I guess that means I'm not using C99. I'll probably start sneaking bool into new code now.
  • real_aardvark (cs) in reply to operagost
    operagost:
    real_aardvark:
    Really?

    I can see it now: "C# -- A Better QBasic..."

    Automobiles: a Better Bicycle Lingerie: a Better Underoos

    Please! Get off the high horse.

    What high horse? I quite like C#.

    I just find my mind boggling at the concept of going directly from QBasic to C# ... OOP just isn't that trivial to pick up (to say nothing of the .NET framework).

    The parallel is, of course, with "C++: a better C." Not that that's very believable, either.

  • I Know PL/SQL (unregistered) in reply to Reuben
    Reuben:
    I like the fact SUBString does the same as SUBSTR, except for substrings longer then 100 characters where the output is truncated.

    I think that you will find that this condition will raise an ORA-06502: PL/SQL: numeric or value error, not truncate the string

  • Grant (unregistered)

    Oracle treats values from tables somewhat differently from the values of local variables. Perhaps this was just to force it to be part of a rowset so that it can be processed in a particular way (in conjunction with something which did not play nice with a local variable.)

    I have had lots of fun trying to figure out when to put the colon or no in PL/SQL, as well as making it play nice with local variables. Some things just don't work with local, pass, or bind variables, and need to be table fields.

  • Brady Kelly (unregistered) in reply to Andy Goth
    Andy Goth:
    Lastly, the expression "x == TRUE" is redundant; just say "x" and be done with it!

    Allow me to say, "That's the real WTF!" Somebody actually feeling it necessary to elucidate on the infamous "x == TRUE" redundancy. What do you take us for?

  • Andrew (unregistered) in reply to Andy Goth

    I'm confused. When would someone who feels the need to use explicit (x==TRUE) (x!=0) (x!=NULL) comparisons be likely to use the x==TRUE test for something that isn't achieved by using logical operators?

  • misha (cs) in reply to real_aardvark
    real_aardvark:
    operagost:
    real_aardvark:
    Really?

    I can see it now: "C# -- A Better QBasic..."

    Automobiles: a Better Bicycle Lingerie: a Better Underoos

    Please! Get off the high horse.

    What high horse? I quite like C#.

    I just find my mind boggling at the concept of going directly from QBasic to C# ... OOP just isn't that trivial to pick up (to say nothing of the .NET framework).

    The parallel is, of course, with "C++: a better C." Not that that's very believable, either.

    I went straight from QB to C++, I don't think it is really any harder than doing C++ from scratch. If you are by inclination or ability a half-decent programmer, learning any language isn't going to make you a worse programmer, any more than driving a crappy car would make you a worse driver. [disclaimer: I can't drive so I don't know if that last assertion is really true]

  • She Ra (unregistered) in reply to Andy Goth
    Andy Goth:
    Andy Goth:
    (or global variable'd, heh)
    By the way, I'm serious about the global variable thing. I once saw code that had a global variable called "success". Functions would return "success" if they succeeded or "!success" on failure. The calling functions would compare against "success" to test for success. However, at least one function got it wrong, instead opting to assign 1 or 0 to the global variable "success" to indicate its status. I think you can picture the ramifications of such a function failing. :^) ("If at first you don't succeed, redefine success.")

    I work with code that does this!!!

  • Jno (unregistered) in reply to Andy Goth
    Comment held for moderation.
  • Ubersoldat (unregistered) in reply to Jonatan
    Jonatan:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    I can't fully agree on that. Sure it is a limited language but also a good way to start when learning to program. At least in my case when I first started doodling in QBasic was when I was 6 ;). Converting from QBasic to C# was not so hard.

    I find Alicia helps more little children on how to code.

    Alicia + KungFu = Total Destruction (ain't inspired today)

  • Pecos Bill (cs) in reply to Mr. Shiny & New
    Mr. Shiny & New:
    SUBString looks to me like a platform independence wrapper.

    If that's the case, it still suffers from the flaws mentioned elsewhere: a potentially conflicting name, truncates the output, implied case sensitivity where there might not be any, and appears inefficient for Ork where it can be platform specific. I'd do an assignment: result := SUBSTR(inputStr,startPos,endPos);

    And, should it support substr(inputStr,-3); ? If not, it might benefit from some bounds checking.

  • Guy (unregistered) in reply to notbloodylikely

    I would also add that if all access to the database is via stored procedures then you can secure the data using security roles so the application only sees the data that it's required too.

    Important if you have both a "web facing" and "internal administration" views to the data.

    And - developers can often develop horrendous SQL code and embed it into the application (like the SELECT :var FROM dual; example above). It least it lets the DBA have a chance of optimising the sql.

    I am not a robot: DOOM

  • rumpelstiltskin (unregistered) in reply to Puckdropper

    [quote user="Puckdropper"]Qbasic isn't exactly BASIC. It is, but it isn't. Using QB, you can learn a lot about functional programming and things like parameter passing and writing functions (ahem: procedures--functions return a single value). quote]

    What could you possibly learn about functional programming from QBasic?

  • GrandmasterB (unregistered)

    I dont know enough about Oracle's innards to comment on the first one. But the second one isnt necessarily a WTF - it needs more context. For ex, there may be more functionality intended for that function... or there may have been additional code there at one point and they didnt want to rock the boat and go back and change every call to the function.

    The "if (x == true/false)" thing is a matter of style - I think its more readable than "if (x)" or "if (!x)" in some cases.

    captcha: gygax (whom I've personally met - great guy!)

  • GrandmasterB (unregistered) in reply to Bob
    Bob:
    malfist:
    If you teach someone BASIC, you cripple their ability to program.

    Bollocks. BASIC programmers learn to program in a nice linear manner and understand the whole input, process, output concept. It's the best possible way to start learning to program. All of the recruits we've had recently get far too clever with pointers or spaghetti objects or some fucking framework <spits> and end up tripping over themselves due to a lack of basic skills.

    What he said.

  • Foosball Girl In My Dreams (cs) in reply to Andy Goth
    Andy Goth:
    ... "!!x == TRUE"
    I am going to start using that one immediately!
  • iMalc (unregistered)

    The most general way of toggling between two values:

    BOOL toggle (BOOL inVal) { return TRUE + FALSE - inVal; }

    Now you can redefine TRUE and FALSE to whatever you like...

Leave a comment on “No Thanks, I Prefer SUBString!”

Log In or post as a guest

Replying to comment #:

« Return to Article