Comment On Dividing by Zero for Fun and Profit

When the Web Applications Group and the Distributed Applications Group were merged into one, it should have been a joyous day for Greg Beech. After all, Greg would finally get the chance to see first hand the "Enterprise CMS" system that the Distributed group had been bragging about for so many years. It was built by a team of elite consultants and billed "the quintessential collection of best practices." [expand full text]
« PrevPage 1 | Page 2Next »

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:03 • by anonym (unregistered)
1. 0
2. 1
3. ?
4. profit / first!

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:06 • by Quango
Shame the Distributed Applications Group wasn't called the Distributed Operations Group.

Your combined operation would be WAGDOG.

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:08 • by kastein

I've actually written code kinda like this... but it never made it past testing, was just quick ugly testing code while I was debugging something else. I heavily // TODO: flagged it and removed it before getting much further.

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:08 • by BK (unregistered)
For some people, exceptions are just another form of GOTO

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:10 • by Charles400
Divide this comment by zero.

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:12 • by hikari
245349 in reply to 245346
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.

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:16 • by Anonymous (unregistered)
Ahh, "elite consultants" - where would TDWTF be without them?

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:16 • by RayRay (unregistered)
245351 in reply to 245349
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 thought you were going to stop talking about me behind my back.

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:19 • by Steve (unregistered)
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).

Re: Dividing by Zero for Fun and Profit

2009-02-23 09:30 • by thg (unregistered)
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.

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:08 • by Tryde (unregistered)
245358 in reply to 245349
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 worked with an (obfuscated) library in java which used new Exception() along with try/catch blocks in order to exit loops, switches...

Debugging was great.... :(

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:17 • by Code Dependent
Do you guys have any idea how discouraging it is to search for an image on the internet, copy its URL, post a message and add the proper BBCode tag to display the image, and then come back a few minutes later to discover that it has been deleted? You're abusing your readership when you do that.

So, you corrected your misspelled word. What's wrong with annotating my post with "Fixed! - Alex" or something similar? After all, I don't mind clicking on your ads now and again so you can rake in a few coins.

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:23 • by Pim
245361 in reply to 245352
Steve:
You sir, are my hero!
(..)
Whilst I'm here, "the googles, they do nothing" and "the REAL WTF is <insert non-WTF here>". Oh, and "frist" (not).

Brillant!

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:24 • by Satanicpuppy
Intentionally creating an error isn't a crazy wtf if you use the error handling code to catch it and do the correct thing...That's basically the entire point of a Try->Catch block.

Still, assuming there isn't any great efficiency gain, it's better to do it using standard conditionals.

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:26 • by Thomas (unregistered)
Was using Raiserror or a better filter really that cumbersome? It certainly would have provided a better error. I guess I'm missing what claimed savings the consultants seem to think they got by using this silly solution.

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:37 • by fluffy777 (unregistered)
The real WTF is that it's inefficient too.

let's examine this expression more closely:

(Active = 1 OR 1/(Active+@ActDivisor) IS NULL)

so... if Active is 1 then the expression is true,
otherwise it's 0 and the expression is "1/(Active + @ActDivisor) IS NULL", so... isn't that just "1/@ActDivisor IS NULL", 'cause Active is 0 ... or... um... what?

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:39 • by lolwtf
(1/0)th!

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:42 • by DaveK
245366 in reply to 245356
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

Re: Dividing by Zero for Fun and Profit

2009-02-23 10:47 • by MainCoder (unregistered)
This was just the error reporting. The real question is why this procedure was called all over with both in parameters set to zero.

Re: Dividing by Zero for Fun and Profit

2009-02-23 11:18 • by Sutherlands
245371 in reply to 245364
fluffy777:
The real WTF is that it's inefficient too.

let's examine this expression more closely:

(Active = 1 OR 1/(Active+@ActDivisor) IS NULL)

so... if Active is 1 then the expression is true,
otherwise it's 0 and the expression is "1/(Active + @ActDivisor) IS NULL", so... isn't that just "1/@ActDivisor IS NULL", 'cause Active is 0 ... or... um... what?


So here are the steps:
1) if(Active = 1 or 1/(Active+@ActDivisor) IS NULL)
2) if(Active = 1 or 1/@ActDivisor IS NULL)
3) if(Active = 1 or ActDivisor = 0)

2 conditional jumps!

Re: Dividing by Zero for Fun and Profit

2009-02-23 11:19 • by hikari
245372 in reply to 245351
RayRay:
I thought you were going to stop talking about me behind my back.


Only when the code stops making me curl up in the corner and cry.

Re: Dividing by Zero for Fun and Profit

2009-02-23 11:23 • by JamesQMurphy
245373 in reply to 245356
thg:
Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.


Except this is PL/SQL. Those consultants weren't stuck, they were on crack.

Re: Dividing by Zero for Fun and Profit

2009-02-23 11:31 • by snoofle

try {
if (iHaveSomethingUsefulToSay) {
int x = 1 / 0;
}
return;
} catch (Exception e1) {
try {
if (itsNotFrist) {
int y = 2 / 0;
}
} catch (Exception e2) {
try {
if (iPromiseNotToPostTheCaptcha) {
int z = 3 / 0;
}
} catch (Exception e3) {
postComment("Yada yada yada");
}
}
}

Re: Dividing by Zero for Fun and Profit

2009-02-23 11:34 • by Lunkwill (unregistered)
TRWTF is how the code fails miserably on Active==FILE_NOT_FOUND

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:03 • by monkeyPushButton (unregistered)
245379 in reply to 245352
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).
No, no. YOU sir, are my hero!

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:04 • by Zerbs (unregistered)
245380 in reply to 245373
JamesQMurphy:
thg:
Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.


Except this is PL/SQL. Those consultants weren't stuck, they were on crack.

Actually this is T-SQL from MS Sql Server. T-SQL does have try / catch functinality in 2005 and 2008

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:05 • by Josh Lefler (unregistered)
Reminds me of a trick I used long ago when I coded in VB6. It was often useful to know if one was in the IDE or running as a real executable, so I had a handy IsDebug() method.


Function IsDebug()
On Error GoTo bad:
Debug.print( 1 / 0)
IsDebug = False
Exit Function
bad:
IsDebug = true
End Function
(Coded from memory and haven't done VB work in years)

Basically, Debug.print calls aren't compiled into the executable, so an error will only get thrown in the IDE.

I am so glad I moved on to Linux and left all things Microsoft behind years ago...

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:20 • by ebenblues (unregistered)
245382 in reply to 245342
anonym:
1. 0
2. 1
3. FileNotFound <--
4. profit / first!

Solved at last!!

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:26 • by Erzengel
245383 in reply to 245381
Josh Lefler:
Reminds me of a trick I used long ago when I coded in VB6. It was often useful to know if one was in the IDE or running as a real executable, so I had a handy IsDebug() method.


Function IsDebug()
On Error GoTo bad:
Debug.print( 1 / 0)
IsDebug = False
Exit Function
bad:
IsDebug = true
End Function
(Coded from memory and haven't done VB work in years)

Basically, Debug.print calls aren't compiled into the executable, so an error will only get thrown in the IDE.

I am so glad I moved on to Linux and left all things Microsoft behind years ago...


Uhhh, why? How does your function turn into "Microsoft = Bad"? Just because you couldn't search the SDK docs and write the following?
Declare Auto Function IsDebuggerPresent Lib "Kernel32.dll" () as Integer


(Note: Not sure if that's the correct code, I don't use VB6. IsDebuggerPresent is the Windows SDK exposed method of determining if someone has attached a debugger, though.)

Still, I like the code. Ingenious, for someone who doesn't know how to use documentation. How would you do this in Linux?

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:36 • by hatterson
245384 in reply to 245383
Erzengel:
Josh Lefler:
Reminds me of a trick I used long ago when I coded in VB6. It was often useful to know if one was in the IDE or running as a real executable, so I had a handy IsDebug() method.


Function IsDebug()
On Error GoTo bad:
Debug.print( 1 / 0)
IsDebug = False
Exit Function
bad:
IsDebug = true
End Function
(Coded from memory and haven't done VB work in years)

Basically, Debug.print calls aren't compiled into the executable, so an error will only get thrown in the IDE.

I am so glad I moved on to Linux and left all things Microsoft behind years ago...


Uhhh, why? How does your function turn into "Microsoft = Bad"? Just because you couldn't search the SDK docs and write the following?
Declare Auto Function IsDebuggerPresent Lib "Kernel32.dll" () as Integer


(Note: Not sure if that's the correct code, I don't use VB6. IsDebuggerPresent is the Windows SDK exposed method of determining if someone has attached a debugger, though.)

Still, I like the code. Ingenious, for someone who doesn't know how to use documentation. How would you do this in Linux?


Read the man page....oh wait

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:38 • by JamesQMurphy
245385 in reply to 245380
Zerbs:
JamesQMurphy:
thg:
Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.


Except this is PL/SQL. Those consultants weren't stuck, they were on crack.

Actually this is T-SQL from MS Sql Server. T-SQL does have try / catch functinality in 2005 and 2008


You're right. PL/SQL doesn't have ISNULL (it has NVL). Just slap me silly and call me Paula.

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:53 • by Jay (unregistered)
245386 in reply to 245346
BK:
For some people, exceptions are just another form of GOTO


Ah, but by using an exception to transfer control rather than a GOTO, you conform to the "no GOTOs" rule, and thus what you have written must be a good structured program!

Next lesson: The experts agree that embedded constants in a program are bad because their purpose can be unclear, like "adjustedAmount=amount*2.942;" may leave the reader wondering where the number "2.942" comes from. This problem is easily solved by replacing all constants with a symbolic name. In this example, "final static float AMOUNT_FACTOR=2.942;" and then use the name rather than the hard-coded constant to eliminate the problem!

For advanced students, we will move on to avoiding the pitfalls of global data accessed from a million unrelated functions by declaring the variable private, then create public get and set functions for it and use THOSE from your million unrelated functions. Your data is private, so it meets the "no public data" rule and what you have written must be a properly encapsulated class!

(Note: This is a joke. Please don't take this advice seriously.)

Re: Dividing by Zero for Fun and Profit

2009-02-23 12:58 • by Bosshog (unregistered)
The goggles, they divide by nothing!

Re: Dividing by Zero for Fun and Profit

2009-02-23 13:12 • by Comments Welcome (unregistered)
Everone missed the real WTF.

Escaping the ' in the comments.

Re: Dividing by Zero for Fun and Profit

2009-02-23 13:12 • by Marc B (unregistered)
245389 in reply to 245352
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).


I'm hoping next month's expression will be "shovel-ready", as in "that code was a real pile of shovel-ready dogsh*t". Why should the politicians have all the fun?

Re: Dividing by Zero for Fun and Profit

2009-02-23 13:16 • by Anonymous (unregistered)
245390 in reply to 245387
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?)

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:02 • by kastein
245391 in reply to 245390
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.)

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:12 • by Kermos
245392 in reply to 245386
Jay:

For advanced students, we will move on to avoiding the pitfalls of global data accessed from a million unrelated functions by declaring the variable private, then create public get and set functions for it and use THOSE from your million unrelated functions. Your data is private, so it meets the "no public data" rule and what you have written must be a properly encapsulated class!

(Note: This is a joke. Please don't take this advice seriously.)


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?

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:31 • by Erzengel
245395 in reply to 245392
Kermos:
Jay:

For advanced students, we will move on to avoiding the pitfalls of global data accessed from a million unrelated functions by declaring the variable private, then create public get and set functions for it and use THOSE from your million unrelated functions. Your data is private, so it meets the "no public data" rule and what you have written must be a properly encapsulated class!

(Note: This is a joke. Please don't take this advice seriously.)


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?


Depends on how far removed the unrelated functions are from the class. If they're in the same executable, and I'm using Microsoft's compiler, I can just replace the data with a __declspec(property). For example,

class SomeClass
{
public:
int MyData;
};

becomes

class SomeClass
{
private:
int _MyData;
public:
__declspec(property(get=GetMyData, put=SetMyData)) int MyData;
int GetMyData()
{
return _MyData;
}
void SetMyData(int value)
{
//TODO: validation, propagation, whatever
_MyData = value;
}
};


That would require no more changes to the surrounding functions. Same deal when working in C#, just with language support instead of compiler specific functionality.

HOWEVER, all bets are off if this is a library that is imported or loaded into another executable.

Also, I don't know if GCC has anything similar.

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:42 • by Kermos
245397 in reply to 245395
Erzengel:

Depends on how far removed the unrelated functions are from the class. If they're in the same executable, and I'm using Microsoft's compiler, I can just replace the data with a __declspec(property). For example,

class SomeClass
{
public:
int MyData;
};

becomes

class SomeClass
{
private:
int _MyData;
public:
__declspec(property(get=GetMyData, put=SetMyData)) int MyData;
int GetMyData()
{
return _MyData;
}
void SetMyData(int value)
{
//TODO: validation, propagation, whatever
_MyData = value;
}
};


That would require no more changes to the surrounding functions. Same deal when working in C#, just with language support instead of compiler specific functionality.

HOWEVER, all bets are off if this is a library that is imported or loaded into another executable.

Also, I don't know if GCC has anything similar.


That declspec property thing is neat, I like it. Don't think GCC does, a quick look at it's attribute list doesn't reveal anything like it. In my case though my code needs to compile both under GCC and MS' Compiler so I can't use anything that is compiler specific anyway.

The Eclipse IDE does have support for automatically generating getters / setters, though it still needs a bit of tweaking to really become useful. It currently doesn't support enough customization to make what it generates fit the surrounding coding style and it also doesn't give me the option of making getters return a const reference. By the time I hand-edit what it has generated it generally is still easier to write the methods myself.

It's a new feature though so hopefully Version 6 will see improvements there. :)

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:46 • by ParkinT
245398 in reply to 245346
BK:
For some people, exceptions are just another form of GOTO

Now THAT is funny!!

Re: Dividing by Zero for Fun and Profit

2009-02-23 14:48 • by ParkinT
245399 in reply to 245374
snoofle:


try {
if (iHaveSomethingUsefulToSay) {
int x = 1 / 0;
}
return;
} catch (Exception e1) {
try {
if (itsNotFrist) {
int y = 2 / 0;
}
} catch (Exception e2) {
try {
if (iPromiseNotToPostTheCaptcha) {
int z = 3 / 0;
}
} catch (Exception e3) {
postComment("Yada yada yada");
}
}
}

But THIS is funnier!!

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:22 • by Daniel (unregistered)
245401 in reply to 245395
If you're using C++, just overload the = operator!

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:50 • by Meistert (unregistered)
245405 in reply to 245352
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).


Brillant

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:53 • by Americium (unregistered)
245406 in reply to 245380
Zerbs:
JamesQMurphy:
thg:
Stuck programming in a language that does not offer Exception Handling? .. Use the good-old "divide by zero" trick.


Except this is PL/SQL. Those consultants weren't stuck, they were on crack.

Actually this is T-SQL from MS SQL Server. T-SQL does have try / catch functionality in 2005 and 2008


Oracle PL/SQL supports exceptions. It is a derivative of Ada, which has its own try..catch idiom. Ada has a block structured begin..exception..end instead.

Ada will raise an exception, rather than throw it. Python borrows this term, but still uses try..catch.


begin
raise Some_Error
exception
when Some_Error then
-- Do something
when others then
-- Handle any other exception
end;

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:54 • by pitchingchris
245407 in reply to 245381
Josh Lefler:

I am so glad I moved on to Linux and left all things Microsoft behind years ago...


Its not a microsoft problem, its an idiot problem.

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:56 • by Jim (unregistered)
245408 in reply to 245386
Jay:
BK:
For some people, exceptions are just another form of GOTO


Ah, but by using an exception to transfer control rather than a GOTO, you conform to the "no GOTOs" rule, and thus what you have written must be a good structured program!

Next lesson: The experts agree that embedded constants in a program are bad because their purpose can be unclear, like "adjustedAmount=amount*2.942;" may leave the reader wondering where the number "2.942" comes from. This problem is easily solved by replacing all constants with a symbolic name. In this example, "final static float AMOUNT_FACTOR=2.942;" and then use the name rather than the hard-coded constant to eliminate the problem!

For advanced students, we will move on to avoiding the pitfalls of global data accessed from a million unrelated functions by declaring the variable private, then create public get and set functions for it and use THOSE from your million unrelated functions. Your data is private, so it meets the "no public data" rule and what you have written must be a properly encapsulated class!

(Note: This is a joke. Please don't take this advice seriously.)


Better still:
#define TWO_POINT_NINE_FOUR_TWO 2.942

Re: Dividing by Zero for Fun and Profit

2009-02-23 15:58 • by The Prophet Omar (unregistered)
245410 in reply to 245391
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.)


Complaining about it is twice as worserer....in accordance with the Prophecy

Re: Dividing by Zero for Fun and Profit

2009-02-23 16:01 • by Bob (unregistered)
245411 in reply to 245391
kastein:
No one gives a Pope's Hat about the captcha you got.

If I had a pope's hat handy, I'ld give it to him.

kastein:
No, you don't have to fulfill this prophecy.)

Technically true, but every time someone mentions a captcha there's a certainty that someone will complain about it, and you did feel obliged to fulfill that prophecy, apparantly under the sincere believ that this time your complaint will make the world a better place.

Once you succeed in that, you should probably pop over to the middle east and tell those guys to just get over their differences. And they wouldn't even want a pope's hat.

(Incidentally, my captcha was 'laoreet' but I don't have anything funny to say about it. Sorry.)

Re: Dividing by Zero for Fun and Profit

2009-02-23 16:03 • by Americium (unregistered)
245412 in reply to 245406
Americium:

...
Ada will raise an exception, rather than throw it. Python borrows this term, but uses try..catch.

correction: Python uses try..except. It bastardizes all the exception-handling syntax!

try:
raise Some_Error
except Some_Error: do_something()
except Exception,e : pass Handle any other exception

« PrevPage 1 | Page 2Next »

Add Comment