- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Errr..this is actually true, exception handling is evil, slow down the execution drastically. I tend to avoid try-catch blocks whenever I can and use my own logic with ifs and return values.
Admin
I look forward to seeing your code on this site.
Admin
This makes you a blight on our profession.
Admin
please, you must submit some code. I just have to see this.
Admin
What is this "spreadsheet" you speak of?
Admin
Me, or exceptionless-boy?
Admin
that would be 'exceptionless-boy'.
Admin
Don't worry; I'm sure someone will be posting it right here sometime soon.
Admin
He needs to lay off a lot more than just cough syrup! [:|]
Admin
Err, what, you've never seen a piece of code that looks like this:
if ( pSomePointer == NULL )
return FALSE;
??
Or would you all rather prefer
try
{
pSomePointer->DoSomething();
}
catch
{
return FALSE;
}
?? This makes me say WTF??
Besides, go to my site and download KT and try to crash it if you can. And good luck.. :-\
Admin
Great, so your code doesn't differentiate between error states and normal return codes. I'm sure that's useful.
If this is how you think exceptions are supposed to be used, it's no wonder you are so confused.
What site?
Admin
You don't seem to have understood the point. Your code is pretty pointless either way, but what we would like to see is more like this:
if ( pSomePointer == null )
throw new IllegalArgumentException("Paramterer XY must not be null.");
However, it depends on what the method does. If all it does is check the parameters for validity and return true or false, then that's OK, although in that case you'd probably want more information about which parameter was invalid in what way. On the other hand, if the method is supposed to DO something with the parameters and can't do that right if the parameter is null, then a return code is bad style, because people tend to call the methode and expect it to work without checking the return value. For a more in-depth explanation, look here.
Admin
That was just a stupid example for the love of everything. My site is http://sprdsoft.cmar-net.org , program is Kewl Tool..
Look guys, I'm just saying that it's easier to check for errors with some custom error checking system rather than using try-catch block which DO slow down the execution. However, I still use standard exception handling when I need to.
This is my article on codeproject.com and this is the coding style I use nearly always, using both my exception handling logic and built in..
http://www.codeproject.com/internet/siv_version.asp
And why the hell do you attack me without asking any further details about my statement??
Admin
You must be new here. All we do is attack attack ATTACK!
Admin
Depends on what you think is easier: putting a switch statement after every method call to test for every possible error code imaginable (and still miss some) or put in a try - catch in a few strategic locations and catch every error.
I'm not sure about C++ but in Java, try - catch blocks do not slow down execution in an appreciable way. It's only when they are thrown that they have any cost. In some languages like Python, the cost of exceptions is built into every method call whether you use a try catch or not.
I work with code written by people who apparently thought exceptions were just a pain in the ass. So when something goes wrong, it doesn't error out, it inserts bad data into the DB and executes in other indeterminite ways. This costs a lot, and not nano-seconds. tens and even hundreds of thousands of dollars can be lost.
Admin
attack! LOL :D
Anyways, I agree, it all depends on the situation, but for example, the project I'm on at my job is completely jammed with try-catch blocks (in C#), our web app is so damn slow because of it, since postback occurs every once in a while and a billion of exceptions get thrown out of space.. Sadly, I was one year too late to stop that 'tradition'..
My co-workers tend to write code like this:
try
{
m_someVar = ViewState["something"].ToString();
}
catch
{
m_someVar = "whatever";
}
which is quite dumb if you ask me, I prefer
if ( ViewState["something"] != null )
m_someVar = ViewState["something"].ToString();
else
m_someVar = "whatever";
so I ask you, if you run this code for ten thousand times, which one will be faster?? :)
Admin
This is a complete misuse of exceptions IMO. And not only is it slower, it's completely silly. There are cases where there's no better alternative and some Python types argue it's an acceptable pattern. There's a thread all about this subject called 'Array of Hope'.
Admin
Hmmm... I'd prefer
string vs(string strKey, string strDefault) {
return ViewState[strKey] == null ? strDefault : ViewState[strKey].ToString();
}
m_someVar = vs("something", "whatever"); // ah, my aching fingers
Admin
<FONT face="Courier New" size=2>bwah ha ha. it's actually easier to make a non-crashing program than you think. for example, i just told my
turing machine to read characters until the end of the tape.</FONT>
<FONT face="Courier New" size=2>h4x0rv("LOL i p4553d it teh INF tape!??!?") ;</FONT>
<FONT face="Courier New" size=2>i ran it a mobius tape, and although it is stuck in an infinite loop, the turing machine is not allocating any resources so it's crash free.</FONT>
<FONT face="Courier New" size=2>totally radical, dude! keanu reeves in "my own private system monitor"</FONT>
Admin
"Kewl tool" huh? People using "kewl" is a WTF in itself.
Admin
<FONT face="Courier New" size=2>my suspicions are confirmed.</FONT>
<FONT face="Courier New" size=2>while i sympathize with your situation, what you're reasoning is the equivalent of strapping hedgehogs to your forearms prior to entering the thunderdome.</FONT>
Admin
That's irrelevant. The first is a horrible abuse of exceptions. But it has NOTHING to do with exceptions vs. return codes. You seem to have been traumatized by exceptions misused for control flow and come to the totally wrong conclusion that they should not be used at all. The great thing about exceptions is that, when used correctly, they make the code so much more readable because you can handle the exceptions for large blocks of code with several possibly exception-throwing statements in one place at the end of the code block rather than mixing it with the code. Better yet, you can handle the exceptions several levels upwards in the method call stack.
As a rule of thumb, exceptions should NOT be used for a case that is expected, for something that happens in a particular place and needs a particular reaction specific to that. Exceptions are good for more general things like "if anything goes wrong while reading or parsing the input file, show error message XYZ and return to main screen".
Admin
what do you mean he should be exported to india? we dont need stupid programmers in india...stupid american programmers should stay in stupid america.
Admin
Apu, we don't expect him to work there! We expect him to be a beggar.
Admin
"I'm not sure about C++ but in Java, try - catch blocks do not slow down execution in an appreciable way. It's only when they are thrown that they have any cost." I am sure about C++; when you can still turn exception handling, you will see performance decreases from turning it on (cf. Scott Meyers, Lippman). But almost anyone says he needs the performance boost he gets from avoiding exceptions is probably lying.
Admin
<font size="3">what's your point?? did ANYONE get this??</font></font>
besides, if it's easier to make a non-crashing program, maybe you should tell that to microsoft heh..
??
Admin
Agreed, I never claimed anything of this to be false, but I still believe that exception handling is over-used, well, at least where I live :-) Sorry for the misunderstanding IF all of you thought that I think exception handling is evil incarnation and should never be used, because that's not what I meant to say..
Admin
Or a satanist.
Admin
<FONT face="Courier New" size=2>0. it's become clear you should keep your knowledge of programming under a fig leaf. otherwise, you'll have to be cast into the accumulator, squashed by 0x01, the loneliest integer.</FONT>
<FONT face="Courier New" size=2>1. is this 1995? are you listening to green day? do you read slashdot? have you installed slackware? do you like the font "Comic Sans"?</FONT>
<FONT face="Courier New" size=2>2. do us all a favor and get back to shooting at the walls of heartbreak, bang bang.</FONT>
Admin
Same as you, my dear friend, should hide your brains, for they obviously match those of George W. Bush, a conclusion I am foced to make due to the fact that you haven't laid down a single proof of your argument and just kept insulting with things irrelevant for this discussion. Good luck with the real life.
Admin
Aw c'mon, it's obvious he was burnt out and bored! I found it pretty silly myself, a good laugh [H]
Admin
What's wrong with India?
Admin
<FONT face="Courier New" size=2>what argument? the hell are you talking about? </FONT>
Admin
if not (JED read Delphi online help on good programming practices) then
JED can't code
else
JED still can't code!
JED - GO SELL LIFE INSURANCE! Your bull.... will fool someone!
Admin
Jed, Dude!
You forgot:
- MightBeTrue
- GuessIfItsTrue
Admin
</font></font>
<font face="Courier New" size="2"><font size="3">I did. I suggest you read a book about computation and automata? Sipser's Introduction to the Theory of Computation is a good read.
I reckon NP-completeness will also not ring a bell?
</font></font>
How much of a Unix guy I am I must honestly say that Windows XP is hardly giving me problems at all. In over 2 years I have not encountered a single blue screen. The only software that has been breaking on me was third party software.
Microsoft bashing, how lovely it might seem to people, is getting old if it is not based on facts.
Admin
Sorry, but this is a generalisation.
Goto does have its functions (ever tried writing compilers/parsers where skipping to a label in the same function makes a lot of sense?), but the way how it is handled is what determines if it is evil or well-used.
The same can be said for 80% or so of the C++ code out there in the wild. A lot of people that have no clue whatsoever how to properly use I/O streams or templates.
Don't blame the language or its functionality for the programmer's malpractice.
The good programmers, at least in my opinion, are the ones that adapt to the situation and not the ones that believe everything is cast in stone and should remain like that forever.
Admin
Yes- and this could be generalized to a small framework so that doing this for various objects didn't require a class for each one. I'd be inclined to pull the offset/field name data out into a separate file, in an actual implementation. Data driven programming is often a big win- it's unfortunate that few mainstream languages offer good facilities for data driven code. It's nice that Java has added reflection, but I still find it quite painful for things like this. A good macro system, and/or convenient closures (inner classes fail the convenience part) and higher order functions would make things a lot easier. Limited expressiveness seems to have been one of Java's original design goals though, and reading the code on this site makes me understand that approach.
The longer I program, the more I rely on "comfort level" as a guide(I guess this is similar to what XP people mean by "Code Smells"). Long functions make me uncomfortable. So does mechanical repetition of the same construct. When my level of discomfort reaches a certain point, I stop and look for a more comfortable approach- which generally means some sort of abstraction (after all, the only problem that can't be solved by another layer of abstraction is too many layers of abstraction).
Admin
I am soooo busted [:'(]
I do try to prevent myself from falling into this trap, but sometimes I have the equivalent of a little bitty script thing I need to write, and if I end up doing it in C# it's just not cool what I end up doing.
/duck
Admin
Happy Jed day everyone!
Jed, you are an artist.
Admin
This is a slightly inaccurate definition of those two functions. They return the previous and next values for any ordinal type; e.g., Succ('a') is 'b'. They also work for enumerations and sets, if I remember my Delphi correctly. This is important because Pascal is very strongly typed, so you can't just add 1 to a char as you can in C.
There are also Inc(x) and Dec(x) methods that add and subtract in-place, which works on all ordinal types as well as pointers.
--Kutulu
Admin
He was probably explaining it to himself.
Admin
<FONT face=Tahoma>AAAAAHHHH!!! now I'll forever be tormented every 29th of August...
I should not have ventured further by knowing what is Jed day...
</FONT>
Admin
I'm sure you are a ******** who's never even dealt with an indian before...
moderator' note: censored
Admin
This thread, I guess, shows why if someone makes a joke on a forum full of programmers like Slashdot, there's always someone who takes you seriously.
About the only WTF here is "Why was he slacking off making jokes". It's obvious from the comments, the code, and everything else that these were deliberately written to be funny, not to be serious implementations of anything necessary. Why was he fired? Probably because he spent too much time on this kind of thing and not enough time on actual programming.
And what's with the criticism of his opinions on exceptions? Are gotos - which is what thrown exceptions are a form of and as such is a cause for debate - suddenly non-controvertial? I don't know many programmers that don't at least have misgivings about the technology, even if they, personally, like them. If he was in an environment in which exceptions are the convention and kept refusing to throw them, then that's one thing, but if this is the case, why not mention that?
He looks to me like a talented, if opinionated, programmer, who choose to incorporate his fun in his work rather than browse the web all day.
Admin
And finally here's Jed himself...
Yeah, a form... in this vein what about loops? Did you ever ask yourself why do we ever need loops (for, while etc)? Completely unnecessary, aren't they? For example:
for(int i=0; i<10; i++) {
bla bla bla
}
is about the same as :
int i=0;
loop_begin:
if (i>=10) goto loop_end;
bla bla bla
i++;
goto loop_begin;
loop_end:
<sarcasm>It makes it really clear what you are trying to do isn't it?</sarcasm>
Admin
I call bullshit here. Not just on you, brazzy, but all the other would be idiots who have posted similar "avoiding exceptions is always bad" type bollocks.
The example you replied to may have to run a thousand times on a gui application in a single hypothetical thread. The try/catch example is going to slow the program down to a crawl and seriously effect the user experience/client satisfaction. Your solution is going to be eveny slower, and what, pop up a thousand error messages? You take the first, more efficient code, and then throw a useless bloody exception to be caught and dealt with somewhere else. See, it all comes down to the context. Do you want users of your UI to have to click "Okay" on a thousand "Error: Paramterer XY must not be null" dialogs?
There are times when we need to do a lot of work very quickly with a lot of possibilities. We don't know for sure what is set, and we don't care. We want to do what we can on as many as possible quickly.
Remember when you're having your little prick compare fest, that an exception is a tool that was created for a purpose and while it is extremely useful, it is not the only way to deal with the unexpected, nor is it always the best way.
Remember also that mine will always be bigger.
BTW, regarding the OP, this guy is great! Obviously some CS grads reading his code had too much sense of humour squeezed out of them in the sausage machine. I'd give him a job for sure, might need a bit of micro management, but he can't be as bad as most CS grads, they actually believe it's a science. I reckon he could write an OS to rival Vista!
Long live JED!
Admin
Admin
For more flexibility you can build a string scanning API. For example you can make a nice set of string scanning classes with a flexible API like the following:
Then parsing the entire string is just a simple matter of
The really neat thing is that if you do it this way the whole StringScanner construct is itself just a static data structure. You can tell it to store itself in an XML configuration file and load it on the fly when you need to parse your string; ; the stringScanner initialization code you see above will be handled transparently by an XML object persistence library.. So it's just a few lines of code to do all the scanning and the string format is flexibly configurable.
(captcha is 'sscanf')
Admin