• (cs) in reply to DaveK
    DaveK:
    thg:
    Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.

    Next week: generating Bus Errors to implement inter-process communication.

    You're late. It's been done. For real. In 1976.

    'leet old skool fun with covert channels

    That's ... That's beautiful. They should have sent a poet.

  • (cs) in reply to Daniel
    Daniel:
    If you're using C++, just overload the = operator!

    I assume by that you mean:

    class SomeClass
    {
        public:
        class IntOverride
        {
            private:
            int _MyData;
            public:
            IntOverride& operator=(int value)
            {
                //TODO: Validation, propogation, whatever
                _MyData = value;
                return *this;
            }
            operator int()
            {
                //TODO: Calculate data if necessary
                return _MyData;
            }
        };
        IntOverride MyData;
    };
    

    That's also overloading the cast operator for the getter. It's definitely a possibility. However, you also need to override every OTHER operator= (*=, /=, etc), whereas properties do that automagically. There are other... undesirable consequences, but it is cross compiler.

    However, all of this assumes you never use references or pointers to the data.

  • (cs)

    Forgive me TDWTF God, for I am about to sin. In mitigation, I'm listening to the Dead at the moment, so my perspective may be skewed. But ...

    "Taking advantage of the fact that the BIT data type is not a true Boolean but is in fact the values 1 or 0..."

    Um, yes.

    Addendum (2009-02-23 16:34): Well, that didn't delete too well, did it?

  • (cs) in reply to kastein
    kastein:
    Anonymous:
    Bosshog:
    The goggles, they divide by nothing!

    You sir, are an idiot commenter.

    Captcha: ingenium (a genius who continually submits nothing but comments about goggles?)

    No one gives a Pope's Hat about the captcha you got. Most of us probably wish it was a bannable offense (and yes, I know some smartass is going to say complaining about it is just as bad. No, you don't have to fulfill this prophecy.)
    1. Captcha 2. Complaint 3. ??? 4. Prophet!

  • methinks (unregistered)

    Ah, reminds me of the following ...er, idiom I once saw in Java:

    void veryLongMethod(...)
    {
       try
       {
          // ~400 LOC
          if (someReturnCondition)
          {
             String a = null;
             a.length();
          }   
          // ~300 LOC
       }
       catch(Exception ex) {}
    }
    

    The WTFs here in ascending significance:

    • Writing methods with hundreds of LOC in an OO language
    • Wrapping the whole method body in one try-block ...
    • ... moreover with a generic catch-clause
    • Committing the deadly sin of leaving a generic catch-block empty
    • Not knowing about the 'throw'-statement
    • Not knowing about the 'return'-statement (The Real WTF (C)) ;oP

    Perhaps I should submit that to the side bar some time...

  • (cs)

    Forgive me TDWTF God, for I am about to sin. In mitigation, I'm listening to the Dead at the moment, so my perspective may be skewed. But ...

    "Taking advantage of the fact that the BIT data type is not a true Boolean but is in fact the values 1 or 0..."

    Um, yes. I have splinters from the One True Boolean embedded in my skull. You might have thought that these splinters come from a Wooden Table, but you'd be wrong. They are, in fact, Al-27 and Al-22. Regrettably, Al-22 decays inside 59 milliseconds, and I'm trying to reprogram my brain with Al-28, which has the interesting property that it is stable for longer than the Standard Windows Timeout (ie 2 minutes). I think the tin-foil hat is getting in the way here.

    But, getting back to whatever point there is, I see this as a failing to understand what stored procedures are for.

    Stored procedures are not for computation. In general, they are not for business logic. (Although they're mighty fine for covering the cases where "business logic" corrupts the database.)

    If you need to write code, you write code. If you write code against a data set, then that's fine: write code against a data set. If you need to write code in Java, with its own menagerie of Enterprisey exceptions, then -- that's fine. At least it's only one set of exceptions to deal with. Ditto most other languages at the consuming end of a DB connection.

    Do NOT expect magic to emerge from stored procedures. It won't. They're on the other end of a dixie cup 'n' string, and they have not bathed in the Blood of the Lamb.

    And what has this got to do with the OP?

    Well, nothing, really. I just feel better for letting it all hang out.

    On a completely unrelated matter: if your daughter has just died, may I recommend Franz Schmidt's Fourth Symphony? It may strike a chord.

  • (cs) in reply to Code Dependent
    Code Dependent:
    kastein:
    Anonymous:
    Bosshog:
    The goggles, they divide by nothing!

    You sir, are an idiot commenter.

    Captcha: ingenium (a genius who continually submits nothing but comments about goggles?)

    No one gives a Pope's Hat about the captcha you got. Most of us probably wish it was a bannable offense (and yes, I know some smartass is going to say complaining about it is just as bad. No, you don't have to fulfill this prophecy.)
    1. Captcha 2. Complaint 3. ??? 4. Prophet!
    Jeez, let the kidz have their fun, and go back to your woodwork.

    There are more important things in life, like nibbling witchety grubs. Just sayin'.

  • Jeremy (unregistered) in reply to Charles400

    Ok..

    "Divide this comment by zero." / 0 = File not found.

  • (cs) in reply to JamesQMurphy
    JamesQMurphy^W Paula:
    Just slap me silly and call me Paula.
    You're silly enough. No need for the slap.
  • (cs) in reply to pink_fairy
    pink_fairy:
    There are more important things in life, like nibbling witchety grubs. Just sayin'.
    Nothing's more important than good wordplay. Or bad wordplay, depending on your point of view.
  • (cs) in reply to pink_fairy
    pink_fairy:
    But, getting back to whatever point there is, I see this as a failing to understand what stored procedures are for.
    It's a shame that the rest of your lengthy comment completely failed to improve our understanding of what a stored procedure is for. I was looking forward to receiving enlightenment...
  • Jay (unregistered) in reply to Kermos
    Kermos:
    Actually keeping member variables private and using get/set methods is a pretty damn good idea. Imagine you have a very frequently used class and suddenly due to new requirements you need something special to happen when it's value is changed. What do you prefer to do, make one change to the set method or change each one of your million unrelated functions?

    Oh, we are going to seriously discuss my joke? Well, okay. Sorry if I'm about to write a book here.

    I didn't say that I am opposed to using getters and setters. Yes, they have the advantage you mention of allowing you to implement side effects. I might also note that they let you handle changes to the data type, like if you start out defining something as an int but later decide it needs to be a float in some cases, you could keep the int setter but have it convert the value to float and then call the float setter. Etc.

    My point, though, was that I have heard many people denounce the evils of global data, and then offer as a solution making the data element private and using getters and setters. I reply that this does nothing to solve the original problem.

    Suppose I write a function to calculate square roots. I put it in my Math object and call it Math.root.

    So now in Object A I write:

    // Get square root of 9
    x=Math.root(9);
    

    And in object B I write:

    // Get square root of 64
    x=Math.root(64);
    

    Cool. Then I decide that in Object A I also need to calculate a cube root. No problem, I say. I'll modify my Math object to include a public variable "power" that controls the behavior of the root() function, and is 2 for square roots, 3 for cube roots, etc. It initially defaults to 2.

    So now in A I write:

    // Get square root of 9
    x=Math.root(9);
    // Get cube root of 125
    Math.power=3;
    y=Math.root(125);
    

    I carefully test object A, it all works, and I declare victory. Right?

    Umm, no. If object B is called after object A, then when B executes, whichPower is still equal to 3, so when it executes "Math.root(64)", the answer is not the expected 8, but only 4. Suddenly there is a very mysterious bug.

    The problem, of course, is because creating the global variable was a stupid solution. A much better idea would have been to create a new function that accepts two parameters, the desired power and the starting number. (We might well change the original root(int x) function to now call anyRoot(x, 2). But that fact should be invisible to the caller.)

    We all agree on that, right? That's why global data is evil.

    So suppose I said, "Oh, you're right, global data is bad. So I'll make power private, and add setPower and getPower functions to update it."

    Now object A says:

    // Get square root of 9
    x=Math.root(9);
    // Get cube root of 125
    Math.setPower(3);
    y=Math.root(125);
    

    Did that solve or mitigate the problem in any way? No.

    Getters and setters can encapsulate side effects, etc. But they are not a solution to the problem of global data.

  • ML (unregistered) in reply to Jay
    Jay:
    Oh, we are going to seriously discuss my joke? Well, okay.
    Actually, the joke wasn't funny. It seemed rather random in the context of this article and explaining it ad nauseum didn't improve it any at all.

    Please don't take this as an opportunity to go into a lengthy dissertation on why your joke about "global data" was perfectly in context with an article about "using errors as flow control". Just let it drop

  • brad (unregistered) in reply to Steve

    I think that you forgot: captcha <followed by utterly retarded comment>

  • (cs) in reply to Josh Lefler
    Josh Lefler:
    I am so glad I moved on to Linux and left all things Microsoft behind years ago...

    In C#, you can do...

    #if DEBUG //Do debug stuff #else //Do release stuff #endif

    But have fun with your Linux

  • David (unregistered) in reply to thg
    thg:
    Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.

    Next week: generating Bus Errors to implement inter-process communication.

    ...and using core dumps to serialize objects.

  • Jay (unregistered) in reply to ML
    ML:
    Actually, the joke wasn't funny. It seemed rather random in the context of this article and explaining it ad nauseum didn't improve it any at all.

    Please don't take this as an opportunity to go into a lengthy dissertation on why your joke about "global data" was perfectly in context with an article about "using errors as flow control". Just let it drop

    You didn't think my joke was funny? Oh no, you have deeply wounded my self-esteem. For of course my whole self-image is dependent on whether someone I've never met and who I only know by a screen name appreciates my humor. This makes me cry inside.

  • Yanman.be (unregistered)

    When exceptions become part of your logic, they're not exceptions anymore, are they?

  • Axil (unregistered)

    To Jay:

    ... The problem, of course, is because creating the global variable was a stupid solution.

    Agreed, yes, in this case.

    ... A much better idea would have been to create a new function that accepts two parameters, the desired power and the starting number. (We might well change the original root(int x) function to now call anyRoot(x, 2).

    Or in say, Delphi:

    function SomeRoot(Value: integer; ARoot: integer = 2): integer;

    Which necessitates just one param for a square root which is good enough for a general case if you mostly need square roots.

    ... But that fact should be invisible to the caller. ... We all agree on that, right?

    Ok, this is where I fall off. Leave aside that other languages might not have default parameters.

    Why should this function be invisible to callers? I mean, it's a math unit, and it's only ever going to be called by another routine in a semi-self deterministic way.

    I'm not being antagonistic, I just don't get the invisible bit. Could you help me here?

    And, yes, I know zip about C (insert flavour) stuff, but thanks to this site and other sources, I can at least read and figure out what code is doing. Sometimes. Getting better.

    Opinion: Using exceptions for general flow control is not good.

  • Axil (unregistered)

    Drat.

    That should have been:

    function SomeRoot(Value: integer; ARoot: integer = 2): real;

  • John Muller (unregistered)

    Considering what happens when an exception hits...

    (C#)

    static void Main(string[] args)
            {
                DateTime start = DateTime.Now;
                int q = 0;
                for (int i = 1; i < 10000; i++)
                {
                    /*
                    if (i - i != 0)
                    {
                        //q += i / i;
                        //q += i / (i-i);
                    }
                    */
    
                    /*
                    try
                    {
                        //q += i / i;
                        //q += i / (i-i);
                    }
                    catch
                    {
    
                    }
                    */
    
                }
                Console.WriteLine(q.ToString() + " in " +(DateTime.Now-start).ToString() );
                Console.ReadLine();
    }
    

    Runtime with "q+=i/i" and no checks. 9998 in 00:00:00.0010001

    sometimes the time doubles to .002002, that's probably the resolution of the timer.

    Runtime with "q+=i/i" and an "if" to prevent divide by zero. 9998 in 00:00:00.0050005

    (hardcoded zero gives a compiler error) Runtime with "q+=i/(i-i)" and an "if" to prevent divide by zero. 0 in 00:00:00.0020002

    Actually, those vary over many runs between .002 and .008 seconds, thread scheduling, overhead, etc. dominates... but that hardly matters...

    Runtime with "q+=i/i" and a try/catch for divide by zero. 9998 in 00:00:00.0150015

    This one varied a lot as well, but generally I'd call it 5-10 times slower than using an 'if'

    Runtime with "q+=i/(i-i)" and a try/catch for divide by zero. 0 in 00:00:13.3103309

    Compared to the variations of the above tests, this one is pretty stable at 13 seconds and change. Only about two thousand times slower than pre-checking the input.

  • (cs) in reply to hikari
    hikari:
    BK:
    For some people, exceptions are just another form of GOTO

    I maintain code written by someone who also used them as a break statement.

    I've done that, but only in debugging code.

    At my previous job, we joked about writing a function for this called Punt(). Punt would declare a pointer to a double, set the pointer to NULL, set the memory at that location (null) to PI, then cast the pointer to a pointer to a function and call it. Guaranteed to cause an exception! We never actually wrote this (that I know of).

  • Watson (unregistered) in reply to Quango
    Quango:
    Shame the Distributed Applications Group wasn't called the Distributed Operations Group.

    Your combined operation would be WAGDOG.

    In some English dialects, "DAG" would also work .... appropriately, too. http://www.shetlandsheepinfo.com/FLEECE/terms%20a%20-%20p.htm
  • ysth (unregistered) in reply to ML
    ML:
    Please don't take this as an opportunity to go into a lengthy dissertation on why your joke about "global data" was perfectly in context with an article about "using errors as flow control". Just let it drop
    This, however, is perfectly in context: http://catb.org/jargon/html/story-of-mel.html
  • Sashlik (unregistered) in reply to methinks
    methinks:
    Ah, reminds me of the following ...er, idiom I once saw in Java:
    void veryLongMethod(...)
    {
       try
       {
          // ~400 LOC
          if (someReturnCondition)
          {
             String a = null;
             a.length();
          }   
          // ~300 LOC
       }
       catch(Exception ex) {}
    }
    
    The WTFs here in ascending significance:
    • Writing methods with hundreds of LOC in an OO language
    • Wrapping the whole method body in one try-block ...
    • ... moreover with a generic catch-clause
    • Committing the deadly sin of leaving a generic catch-block empty
    • Not knowing about the 'throw'-statement
    • Not knowing about the 'return'-statement (The Real WTF (C)) ;oP

    Perhaps I should submit that to the side bar some time...

    • Declaring 'a' variable instead of using something like: ((Object)null).hashCode();
  • Jaysan (unregistered) in reply to Comments Welcome
    Comments Welcome:
    Everone missed the real WTF.

    Escaping the ' in the comments.

    Depends on how the code was copied. If it was originally scripted so that their tool used sp_executesql then the proc would've been embedded in a string and need the ' escaped regardless of where they were.

  • Jaysan (unregistered) in reply to methinks
    methinks:
    * Not knowing about the 'return'-statement (The Real WTF (C)) ;oP

    Oh no, you can't use a return statement in the middle of your method because it violates the principal of having only one exit point.

  • Ryan (unregistered)

    Was this an SQL server SP?

    Was is SQL server 2000?

    You'd be doing well to use RAISERROR in SQL server 2000 ;-)

  • Andrew (unregistered) in reply to Quango

    In Australia, WAGDAG is funnier than WAGDOG.

  • (cs) in reply to Bosshog
    Bosshog:
    The goggles, they divide by nothing!

    FTW!

  • (cs) in reply to Steve
    Steve:
    You sir, are my hero!

    I'm not talking to anyone in particular but I thought I'd get it out there since it seems to be expression of the month at the moment and someone's going to say it sooner or later.

    Whilst I'm here, "the googles, they do nothing" and "the REAL WTF is <insert non-WTF here>". Oh, and "frist" (not).

    You forgot the classic: In Soviet Russia, zero divides by YOU!

  • Thiadmer (unregistered) in reply to thg
    thg:
    Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.

    Next week: generating Bus Errors to implement inter-process communication.

    In the days of Windows 3.x and OS/2 1.x (running on the 80286), programs had to switch between "real mode" and "protected mode" every so often. Though the 80286 could switch from real mode to protected mode in a well documented way, it lacked an instruction/method to do the reverse. The solution: force a processor exception, and have the exception handled by code that causes another exception, eventually "triple faulting" the processor and making it do a full reset. Special code in ROM and a signature in a reserved spot in RAM made the processor restart at the position where it left off.

    If memory serves, this was even patented (do people inventing such kludges have no shame?), but I cannot find the patent right now.

  • Noob (unregistered)

    "?etw".reverse.next.upcase

    inhibeo: wizard incantation used to freeze stupidity in it's tracks.

  • (cs) in reply to pink_fairy
    pink_fairy:
    Code Dependent:
    kastein:
    Anonymous:
    Bosshog:
    The goggles, they divide by nothing!

    You sir, are an idiot commenter.

    Captcha: ingenium (a genius who continually submits nothing but comments about goggles?)

    No one gives a Pope's Hat about the captcha you got. Most of us probably wish it was a bannable offense (and yes, I know some smartass is going to say complaining about it is just as bad. No, you don't have to fulfill this prophecy.)
    1. Captcha 2. Complaint 3. ??? 4. Prophet!
    Jeez, let the kidz have their fun, and go back to your woodwork.

    There are more important things in life, like nibbling witchety grubs. Just sayin'.

    [image]

  • BitFlipper (unregistered)

    This would not work for Chuck Norris. He can divide by zero.

  • methinks (unregistered) in reply to Jaysan
    Jaysan:
    methinks:
    * Not knowing about the 'return'-statement (The Real WTF (C)) ;oP

    Oh no, you can't use a return statement in the middle of your method because it violates the principal of having only one exit point.

    Says who? ;o)

    I was always feeling that under certain circumstances the code is clearer if one uses multiple exit points.

    The code above is a prime and pathological example for that - even if there were many exit points, using "return" instead of all of the misused stuff above would've been preferable by far.

    Of course not making the mistake of "hundreds of LOC in one method" in the first place would most probably cater to this problem, leaving only a handful of exit points (or even just one) for most of the methods this behemoth must be broken down into. And only then we could additionally talk about merging these handful of exit points into one.

  • (cs) in reply to JimM
    JimM:
    pink_fairy:
    But, getting back to whatever point there is, I see this as a failing to understand what stored procedures are for.
    It's a shame that the rest of your lengthy comment completely failed to improve our understanding of what a stored procedure is for. I was looking forward to receiving enlightenment...
    Loath though I am to disappoint the multiple JimMs and their numerous subjects out there, I think you may have missed the crucial phrase "I see this as..."

    You can try reading the comment again, on the understanding that it reflects my understanding of what stored procedures are for.

    Alternatively, you may consider:

    (a) only reading comments of less than, say, a coupla hunnert words or (b) a humorous, or bigoted, or possibly even informative, riposte. Only a coupla hunnert words, please. You have a duty to your subjects, damn it.

  • Christoffer (unregistered) in reply to Jim

    And, of course, after the first maintenance round #define TWO_POINT_NINE_FOUR_TWO 1.772

  • Adam Turner (unregistered)

    Chuck Norris can divide by zero.

  • (cs) in reply to Adam Turner
    Adam Turner:
    Chuck Norris can divide by zero.
    But Bruce Schneier would have finished reading the thread before posting!
  • (cs) in reply to DaveK
    DaveK:
    pink_fairy:
    Code Dependent:
    kastein:
    Anonymous:
    Bosshog:
    The goggles, they divide by nothing!

    You sir, are an idiot commenter.

    Captcha: ingenium (a genius who continually submits nothing but comments about goggles?)

    No one gives a Pope's Hat about the captcha you got. Most of us probably wish it was a bannable offense (and yes, I know some smartass is going to say complaining about it is just as bad. No, you don't have to fulfill this prophecy.)
    1. Captcha 2. Complaint 3. ??? 4. Prophet!
    Jeez, let the kidz have their fun, and go back to your woodwork.

    There are more important things in life, like nibbling witchety grubs. Just sayin'.

    [image]
    Gah.

    [image]

  • BK (unregistered) in reply to Jaysan
    Jaysan:
    methinks:
    * Not knowing about the 'return'-statement (The Real WTF (C)) ;oP

    Oh no, you can't use a return statement in the middle of your method because it violates the principal of having only one exit point.

    Of course, single-entry-single-exit does not work in any language with exception support anyway (because exceptions create implicit exit points). Unless programs in said language are written in such a way as to nullify any exceptions, as demonstrated above.

    You can program against the language. After all it is your users, not you, who will feel the pain.

  • (cs) in reply to DaveK
    DaveK:

    Damn, now I need to unregister so I can always post my CAPTCHA. The memes, think of the memes I can have some omniscient, blameless being kill for me! It's like laundering money, except with murder instead.

    DaveK:
    Or Mimes. Either way works. I prefer the former, though.
  • Josh Lefler (unregistered) in reply to Erzengel
    Erzengel:
    Just because you couldn't search the SDK docs and write the following?
    Declare Auto Function IsDebuggerPresent Lib "Kernel32.dll" () as Integer

    The docs state that API was available in Win2K or later. I remember looking around for a "proper" way at the time, so it either didn't exist or I found it and eliminated it as some of our clients were still on Windows 98. I honestly can't remember which was the case now.

    As far as why I moved to Linux: Freedom. I control what my computer does - Microsoft doesn't.

Leave a comment on “Dividing by Zero for Fun and Profit”

Log In or post as a guest

Replying to comment #:

« Return to Article