- 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
Heh. Forum software strikes again.
Admin
Yeah - we all know they should have used Visual Basic!
Admin
Don't you know it's heretical to question the genius of Java's exception scheme? Real programmers accurately foresee all the exceptional scenarios that may occur ahead of time and have a magical catch block that will make everything alright.
Admin
How would one properly use crack? Singletons, I get, but crack?
Admin
Exactly. That is why so many examples (in texts and otherwise) say that the error checking was omitted for clarity. The real programmers simply fill in the blanks. Easy, peasy.
Sincerely,
Gene Wirchenko
Admin
The bug - sans observer, is doomed to fly singleton, from the factory to the oncoming windshield, where upon it's last sense in this world is static, only to be decorated by the fleeting instance of wiper-blades, and it's absence in the factory registry. Where upon a new iteration of the bug flies from the factory...without scope...
Admin
Seriously... masklinn, you need to have a Narcissism checkup once in a while...
Admin
People don't get hurt using crap software--unless they put their fist through the CRT in a fit of rage.
Admin
ANSI-C.
Admin
Yeah. It goes something like this:
Admin
Admin
Java. If I have the choice, it's servlets + a template engine.
Admin
They are called "Expections" (as opposed to Expectations), and the name was coined here, after several wtfs where exception handling was used as the only exit from a loop.
You recognise them by the fact that every programmer within kilometres start puking.
Admin
There is a licence available, only it's not called "licence" but a "degree in Computer Science" or "degree in Software Engineering"...
Offcourse a degree does not guarantee that one does not make stupid things, but at least the chances are smaller than with some self-educated guy who just walked in from the street and proclaimed to be a professional programmer.
Admin
Actually, it DOES help design, if you stop and think rather than continue to code like you always have. If you feel that you are being overwhelmed with the number of exceptions thrown, that is a big warning sign that you have designed your program wrong. A lot of oetherwise really smart people get this wrong in my opinion, Bruce Eckel for instance, who in early editions of thinking in java had lots of example code wrapped in a big try/catch which then ignored the exception... like an earlier poster said, an equivalent of "on error resume".
For example: When writing a system that did a lot of reading/writing to files, I found I had IO exceptions everywhere, which made the code messy, recovery difficult, and showed a lot of low level error messages to the end user. I realised the design was flawed, and when I refactored it so that it was more modularised (a logging subsystem, a db subsystem, etc that were separated much cleaner) things became much simpler. I could wrap all the different IOexceptions the subsystems could get and rethrew them as an exception that was appropriate for the subsystem, say, loggingexception. Then it is up to the user of the subsystem (myself, in this case), to decide what the app as a whole should do with this problem (warn user, but continue). On the other hand, if the db subsystem got an IOexception, there was no use continuing as that was the whole purpose of the app, so I called some generic shutdown routines in the "main" controller class. I did NOT throw a Runtime exception.
In my opinion, having the checked exception is what has facilitated the creation of the huge number of high quality open source projects for Java.
</wink>
Admin
It didn't say that ASP was a programming language....
and nor are HTML or ASP.NET programing languages, so whats the problem?
/H
Admin
Nor is AJAX a programming language...
/H
Admin
Err... doesn't take much of an imaginative effort for me - you've just pretty much described what we have to support...
>
Imagine if you will, the next attempt at dealing with the singleton databaseexception. A flyweight-cache so as to share common message instances, a factory to create the right style of database exception, coupled with the option to choose one's factory at runtime via a prototype, and then the database exceptions could use a template pattern instead of messages, an observer pattern to decouple logging the databaseexceptions, and a swarm of decorators to allow these exceptions to wrap on top of each other.
Admin
Maybe I'm dense (and I'm not a professional java programmer) but what is the problem with a singleton exception - and how is this related to the duplicate key - surely the DB wasn't asked to insert the object's reference?
I'm sure you guys will tell me just how dense I am!
Admin
If two errors occur at the same time, the second thread will overwrite the error message of the first, and you get misleading error logs.
Admin
Hey I'd prefer funny discussions instead of know-it-alls trying to make logical posts about illogical code.
Admin
There's also the problem that a thread that had no problems has access to a correctly created exception + error message that it has never had.
Sooooo, what does this mean? This means that if you forget to inicialize the error message before throwing the exception, it doesn't mind, because it may already been inicializated with a completely unrelated message by some other thread at some point when that other thread needed to throw his own DataNotFoundException.
Admin
Try Resin Server. I always use the Open Source version, which has no proxy caching nor load balancing but works fine for single-server setups.
Admin
From the book : Practical Common Lisp
http://www.gigamonkeys.com/book/
As has been said many times, and variously attributed to Donald Knuth, C.A.R. Hoare, and Edsger Dijkstra, premature optimization is the root of all evil.
Knuth has used the saying several times in publications, including in his 1974 ACM Turing Award paper, "Computer Programming as an Art," and in his paper "Structured Programs with goto Statements." In his paper "The Errors of TeX," he attributes the saying to C.A.R. Hoare. And Hoare, in an 2004 e-mail to Hans Genwitz of phobia.com, said he didn't remember the origin of the saying but that he might have attributed it to Dijkstra.
Admin
Singleton is a GoF pattern...
And yes, it's often abused badly (as are most patterns).
Worst pattern abuse I've come across was an application designed by someone who LOVED multitier (layered) applications and factories.
It was a 3 layer application (typical MVC, dataaccess, business logic, display), nothing wrong with that (except that the reason given, decoupling the DB from the business logic and display was senseless as it was a monolithic platform, each component being set in stone by corporate (multinational) dictate).
Then it got funny, each layer was itself divided into 3 layers (communication with the layer below, communication with the layer above, and the actual application logic).
On the interface between EACH two layers there were abstract factories on either end to create the objects passed between them, as well as the objects receiving those objects to process them. Of course there were also factories to create the factories.
So to display a single database record when a button was clicked you had to pass down through 9 application layers, creating 18 objects (factories and factory-factories) to create 9 DTOs, and then do the same again on the way back.
So a single record retrieval would just to pass through the application architecture be creating 54 objects, plus whatever was needed inside the layers themselves to handle the request (business logic, display logic, database logic).
Suffice to say the application was terribly slow, massively large (despite offering relatively little real functionality), and very hard to maintain because of all the bypasses that had over time been created to prevent having to go through all that for time critical operations (like printing a 100 page report which when going through the architecture would take 72 hours, but going direct would be done in maybe 10 minutes).
Admin
Imho you usually expect that a database connection could break down occasionally but you don't expect them to break all the time. Now, these are the cases that should be called exceptions. If this happens you want to pinpoint the cause, the location and the time scale of the problem and emit a fitting user-friendly error message..
Seems fairly simple to me.
But maintaining software has taught me (at least) one lesson: if something can be done the simple way, the developer will stick to the most roundabout solution.
---
int param = new Integer( paramStr ).parseInt( paramStr );
Admin
I bet it was done in Java, using at least 5 different Apache frameworks.
Admin
I have a theory that when developers write code like this they emulate those funny bird species where the male tries to attract the female by a really weird and idiotistic dance. The weirder the dance the more your chances to mate.
Admin
Wrong guess, it was done in C++ using no frameworks whatsoever.
It did use copious code generation by Rational Rose though, which added even more headaches as it had a tendency to silently removed code it hadn't itself generated even when placed in the "safe" areas it indicates for such.
Admin
Talk about the tail wagging the dog. So forced declarations in Java are good because they made you realize how poorly modularized your initial design was? Sorry, but you should have had modular design, regardless of exception handling. Are you saying that if you tried to code in c# you wouldn't write good, modular code because you weren't forced to handle or declare exceptions at the method level?
Admin
Why don't you submit the code, maybe it'll get used on the main page ;)
Admin
Oh man, I thought for most of the post you were being serious. Thanks for including the tip-off.
Admin
This method can be usefull if carefully applied within a narrow context, e.g. input validation.
Instead of a relatively verbose input validation
you only have to write
and the surrounding framework cares for the rest. (validateNotEmpty throws a ValidationException if validation fails, thus breaking out of processRequest)
Admin
ARRGGH!!!! Its that dreaded singleton! I HATE SINGLETONS! HATE HATE HATE!
It wouldn't be so bad, but I estimate that half of our crashes in production code are caused by singleton where there was no need for them. (And all in classes created by a chief programmer who thankfully is no longer here)
P.S. Did I mention I hate singleton?
Admin
My favorite pattern is Argyle....
Admin
Bah, we all know plaid is superior.
Admin
wtf. Yes real programmers accurately see forsee checked exceptions (the compiler helps). There is nothing magical about using a catch block to log the error and let the user know something happened.
Admin
Yes I have, it was evil. So am I.
Admin
Did this someone go on to found Interface21? :D
Admin
I agree, a degree/certificate or license does not gaurantee any common sense or misuse of knowledge gained during the process of getting the piece of paper. Every profession has it's 'crack heads' and they, like us, most likely spend part of their work day looking at examples of these 'wtfery'.
I disagree that a choice of a certain type of platform (which always seems to flame up) to produce a usable product would enhance the end user's experience. At one time the choices ranged in degrees of Assembly and I'm sure you could still write applications in assembly but you would likely be crazy by the time you were done.
Admin
I think you were joking, but I have to say this anyway. The only people I see making fun of Spring are those that just can't comprehend it. Generally speaking, they are also junior or junior/mid level programmers who think they are seniors. A pretty common occurence.
Anyway, if you don't think Spring is great, you should look at it again. It brings things to the Java platform that have been lacking for years and that make life so much easier. And the core concepts are just great!
Onto another topic....I know this forum and its participants lean heavily towards the .NET/MS side of things, but if you haven't already you have to check out Rails. Seriously, it is that good.
BTW, just so there is no confusion. I have done Java/J2EE programming for the past 8 years, moved to .NET 2005 about 3 months ago and I don't think either can hold a candle to the productivity I get out of Rails.
Admin
Int32.TryParse calls Number.TryParseInt32 which calls a dozen other functions. Convert.ToInt32 follows the same path in .NET 2.0, but a totally different path in .NET 1.1. TryParse didn't exist on many of the object types in .NET 1.0 and .NET 1.1. I'm trying to find the processor savings by calling this function chain with tramp data versus executing a stack trace and creating an exception.
For that matter, exceptions may be misnamed but I do believe they can aid in code readability. They don't help with code readability or maintainability if they are overused, but any aspect of programming (in any language) can cause chaos in that manner.
Yes, this code makes sense:
if(Int32.TryParse(input, out result) == true)
is clean and can be read and still make sense. But what about...
if(CustomLibrary.PositionObject(x, y, z) == false)
False? False what? What does false mean? Well, let's make it return 0 for normal operations and a negative number for error states!
if(CustomLibrary.PositionObject(x, y, z))
Well that makes sense, at least. It's readable, if you're used to the shorthand boolean expressions.
What about error checking though? We just completely forgot about error checking! We better add that in.
int errorCode = CustomLibrary.PositionObject(x, y, z);
switch(errorCode)
{
case -1: // File not found error.
HandleFileNotFoundError();
break;
case -2: // Resource unavailable.
HandleResourceUnavailableError();
break;
...
default: // No error. Celebrate!
HandleUserInput();
break;
}
Oh, eh, that's fantastic. After every line of code we do a complete error check. That makes for REALLY readable code.
So rather than concentrate on writing code that first and foremost does its job - you end up writing code that tries (and will fail!) to take into account every possible error that might occur. Whereas with exceptions you - yes, pick and choose - pick and choose the errors you want to recover from at your level.
try
{
CustomLibrary.PositionObject(x, y, z);
HandleUserInput();
}
catch(FileNotFoundException ex)
{
// Handle error.
}
catch(ResourceUnavailableException ex)
{
// Handle error.
}
The processing code is written and can be read over without attempting to mentally discard the error handling code. The exceptions come at the end, resembling a goto (and I think this is most programmers objection to it, despite the fact that sensible goto use is advocated in Code Complete, 2nd Ed), but the exceptions at the end allow you to centralize your error processing.
Exceptions propagate even, so if you throw a HolyShitTheSystemIsOnFireException and don't handle it until your main program routine, there's nothing you have to do to support that behavior. You don't end up with...
int Routine1()
{
int errorResult = CustomLibrary.PositionObject(x, y, z);
if(errorResult < 0)
{
return errorResult;
}
}
int Routine()
{
int errorResult = Routine1();
if(errorResult < 0)
{
return errorResult;
}
}
The programmer actively declares what exceptions they can handle.
Flow control: I don't like using exceptions for flow control but they do provide a great deal more meaning than integer or boolean result codes. You could create an enumeration I suppose for every function and have the function return one of the enumerated values to make it clear exactly what occurred -- but this seems to be a waste of time to me.
As for the supposed cost of exceptions there are tons of articles and research pointing both ways on the supposed exceptional cost. "The True Cost of Exceptions" (http://www.developerfusion.co.uk/show/5250/) is one such article that tries to debunk the "exceptions are expensive" myth, with enough details to satisfy most folks. In "Writing Managed Code" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/fastmanagedcode.asp) by Jan Gray, the Microsoft CLR Performance Team, she leaves testing exception handling as an exercise to the reader. If exceptions were a huge performance impact would they not have been covered in this paper?
Finally, CodeProject has an excellent article entitled "Performance Implications of Exceptions in .NET" (http://www.codeproject.com/dotnet/ExceptionPerformance.asp). The author goes into detail as to what he measured, how he measured it, and provides a fairly substantial analysis that the footprint of exceptions is minimal as compared to the rest of the architecture.
The primary objection I've seen to exceptions is that of performance - and I don't believe they are the problem in those cases. If you're objecting to exceptions use as flow control devices then you need to step back and reconsider: when you call a function, you should expect it to succeed. If you expect it to fail, why not pretest the data? If the function fails it has encountered an exception in its ability to process data and it reports that exception to you. Hell, I'd rather say they aid in flow control rather than defeat it because they allow the flow to be distinctly defined.
But, whatever, I'm sure this will be torn to pieces by the zealots who hang out here.
(Edit: nice captcha. Postal. *snicker*)
Admin
How right you were. :)
Have you ever considered not using stupid ifs? IE if ( x ) as opposed to if ( x == true) or if ( !x ) as opposed to if ( x == false ).
That really bugs the hell out of me.
Admin
I also detest:
if ( a == b ) then x = true else x = false;
when
x = a == b;
is so much simpler.
Admin
I have a theory on how this happened.
- The programmer was advised that in a system that uses exceptions, an "out of memory" exception should be statically allocated on startup, so that if it occurs you don't run into the problem of failing to allocate memory for your error message.
- The programmer made this instance global.
- The programmer then learned that "all globals are evil, use singletons instead".
- Then, somehow, it morphed from just the "out of memory" exceptions to all exceptions.
(Of course, if your singleton dynamically allocates the string for the error field, you'd actually be back to start for the "out of memory" condition.)
Am I giving too much credit?
Admin
Anyone ever wrote a program that explicitely throws a "out of memory" exception?
Admin
Another which says what I think
Admin
Really? I've found it's quite the opposite.
Admin
On the contrary, Argyle lends itself to far more efficient development processes, and performs twice as well as plaid in core benchmarks.
Admin
Two people interview for the same position and they both look about the same in terms of experience and skill. Money is not a problem. One has a degree (from a real school, not Devry, Joe Blow CC or ITT Tech or something) and one does not. Who do you hire? I would hire the one with a degree from a real school 10 out of 10 times. This at least shows some sort of commitment and investment (time and money).
A degree does not guarantee that the person will be good, and not having a degree does not mean they will be bad (one of the better programmers I have known does not have a degree), but generally speaking, self-educated people take shortcuts and are not forced to do the hard things in many cases.
To take a different route: would you let a doctor operate on you if they were self taught? Would hire a lawyer that didn't goto law school?
This phenomenon where we allow accountants to become programmers just because they want to without any formal education is what gives us some truly crappy projects. Think back to the .com days when everyone was becoming a developer overnight because the money was there....it only hurt the field as a whole.