Click here to Skip to main content
15,889,200 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Which code you suggest? Pin
Stefan_Lang26-Jul-13 0:19
Stefan_Lang26-Jul-13 0:19 
GeneralRe: Which code you suggest? Pin
OriginalGriff26-Jul-13 0:26
mveOriginalGriff26-Jul-13 0:26 
GeneralRe: Which code you suggest? Pin
Stefan_Lang26-Jul-13 1:27
Stefan_Lang26-Jul-13 1:27 
GeneralRe: Which code you suggest? Pin
svella26-Jul-13 4:42
svella26-Jul-13 4:42 
QuestionRe: Which code you suggest? Pin
Amir Mohammad Nasrollahi28-Jul-13 1:55
professionalAmir Mohammad Nasrollahi28-Jul-13 1:55 
GeneralRe: Which code you suggest? Pin
Stefan_Lang28-Jul-13 22:21
Stefan_Lang28-Jul-13 22:21 
GeneralRe: Which code you suggest? Pin
svella29-Jul-13 4:02
svella29-Jul-13 4:02 
GeneralRe: Which code you suggest? Pin
Stefan_Lang29-Jul-13 22:48
Stefan_Lang29-Jul-13 22:48 
Ok, lets forget language (old C) specific concerns: Assuming you have exceptions, yes, I agree that you can reasonably handle many cases of premature returns that way.

However, my take on exceptions is that code running as expected shouldn't throw one! Finding an element with specific properties in a container does not warrant throwing an exception, whether or not you need clean-up.

Look at the following code, ignoring language specific elements:
C++
// find first elementwithin tolerance of a given value
// return element ID
int find(Container c, double value, double tol)
{
   int result = INVALID_ID; // some constant that is not a valid element ID
   do_some_intialization();
   for(size_t index = 0; index < c.size(); ++index)
   {
      if (element.distance(value) < tol)
      {
         result = element.ID();
      }
   }
   do_some_clean_up();
   return result;
}

There are various solutions to short-cut the loop once it finds a fitting element:
1. use some command that breaks out of the innermost loop (in C/C++ you can use break)
2. attach the check (result==INVALID_ID) to the loop header, so it quits once you assign a valid ID. (not sure how to do that with for_each in C#, but a standard for loop lets you add an arbitrary number of stop conditions easily)
3. Introduce a flag variable that indicates when you're done searching. As it would pretty much just store the current value of (result==INVALID_ID) in this case, you might as well go with solution 2 above
4. throwing an exception, catching it with try/finally to ensure proper cleaning up

In this example, my suggestion of introducing a flag variable turns out to be unneccesary, as the result variable itself can be used to pretty much the same effect. In my experience, this is often the case, so the effort to use these kind of checks instead of premature returns or gotos is often rather low.

Using an exception would certainly work, but it conflicts with my understanding of what an exception means. Also, if you do catch actual error cases with exceptions within that same code, you need to make sure to not catch the 'good-case-exceptions' as errors!

If you have no problem with that, the more power to you. But I prefer not to go that route.
GeneralRe: Which code you suggest? Pin
svella30-Jul-13 3:57
svella30-Jul-13 3:57 
GeneralRe: Which code you suggest? Pin
PIEBALDconsult30-Jul-13 5:44
mvePIEBALDconsult30-Jul-13 5:44 
GeneralRe: Which code you suggest? Pin
Renzo Ciafardone27-Jul-13 16:49
Renzo Ciafardone27-Jul-13 16:49 
GeneralRe: Which code you suggest? Pin
Fabio Franco26-Jul-13 2:59
professionalFabio Franco26-Jul-13 2:59 
GeneralRe: Which code you suggest? Pin
Klaus-Werner Konrad1-Aug-13 12:32
Klaus-Werner Konrad1-Aug-13 12:32 
GeneralRe: Which code you suggest? Pin
Sentenryu25-Jul-13 8:46
Sentenryu25-Jul-13 8:46 
GeneralRe: Which code you suggest? Pin
ZurdoDev30-Jul-13 5:44
professionalZurdoDev30-Jul-13 5:44 
GeneralRe: Which code you suggest? Pin
PIEBALDconsult30-Jul-13 5:43
mvePIEBALDconsult30-Jul-13 5:43 
GeneralRe: Which code you suggest? Pin
César de Souza26-Jul-13 3:06
professionalCésar de Souza26-Jul-13 3:06 
GeneralRe: Which code you suggest? Pin
Argonia26-Jul-13 3:59
professionalArgonia26-Jul-13 3:59 
GeneralRe: Which code you suggest? Pin
César de Souza26-Jul-13 4:41
professionalCésar de Souza26-Jul-13 4:41 
GeneralRe: Which code you suggest? Pin
Ancandune26-Jul-13 2:19
Ancandune26-Jul-13 2:19 
GeneralRe: Which code you suggest? Pin
ZurdoDev26-Jul-13 2:21
professionalZurdoDev26-Jul-13 2:21 
GeneralRe: Which code you suggest? Pin
Ravi Bhavnani29-Jul-13 11:16
professionalRavi Bhavnani29-Jul-13 11:16 
GeneralRe: Which code you suggest? Pin
ZurdoDev29-Jul-13 14:45
professionalZurdoDev29-Jul-13 14:45 
GeneralRe: Which code you suggest? Pin
Ravi Bhavnani30-Jul-13 5:05
professionalRavi Bhavnani30-Jul-13 5:05 
GeneralRe: Which code you suggest? Pin
ZurdoDev30-Jul-13 5:07
professionalZurdoDev30-Jul-13 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.