Click here to Skip to main content
15,913,669 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.

 
AnswerRe: Which code you suggest? Pin
BubingaMan25-Jul-13 21:10
BubingaMan25-Jul-13 21:10 
AnswerRe: Which code you suggest? Pin
Simon O'Riordan from UK25-Jul-13 22:26
Simon O'Riordan from UK25-Jul-13 22:26 
GeneralRe: Which code you suggest? Pin
Sentenryu26-Jul-13 1:05
Sentenryu26-Jul-13 1:05 
GeneralRe: Which code you suggest? Pin
Simon O'Riordan from UK26-Jul-13 1:21
Simon O'Riordan from UK26-Jul-13 1:21 
AnswerRe: Which code you suggest? Pin
vonb25-Jul-13 22:41
vonb25-Jul-13 22:41 
AnswerRe: Which code you suggest? Pin
Stefan_Lang26-Jul-13 0:09
Stefan_Lang26-Jul-13 0:09 
GeneralRe: Which code you suggest? Pin
Renzo Ciafardone27-Jul-13 17:00
Renzo Ciafardone27-Jul-13 17:00 
GeneralRe: Which code you suggest? Pin
Stefan_Lang28-Jul-13 22:00
Stefan_Lang28-Jul-13 22:00 
Renzo Ciafardone wrote:
unnecessary variable

YMMV. If the alternative is goto, I choose the variable. If it is 15 layers of nested control statements, I choose the variable. If I know that anyone, including me, may be reading and trying to understand that code next month, I'm using a variable. I'm not saying to always use such a variable - only when it helps keeping the code clean and maintainable. IMO, the variable is sensible and necessary in these cases. (Also, these cases cover pretty much all the code I've worked on over the past 30 years)

Renzo Ciafardone wrote:
you are wasting an assignation and then you are adding an extra comparison for each block that fails plus the one that's actually true

You are underestimating the efficiency of an optimizer: in most cases you won't even notice a difference, as the compiler will optimize away any variable that is only used sporadically.

The only exception is if there are multiple checks at the top nesting layer: then you need to add one condition to each top level check after the one that contains the 'abort condition'. The alternative would be just one check and moving the rest of the code down one nesting level. The former may have a minor impact on performance (but see below), the latter will always adversely affect code complexity, and thereby the likelyhood of bugs and the effort of maintenance. Your choice. In the past, I've tended to the latter. But now that I have to deal with that same code, I've decided - for my own benefit - to use the former.

Renzo Ciafardone wrote:
you got yourself a waste of time well into the millisecond range if not more

I very much doubt that.

More importantly, even if it were true for one in a thousand or even one in ten (meaningful!) applications, don't design and write code under the assumption of the worst possible effect on performance, write under the assumption that you need to maintain and rewrite code often!

If you have an application with an expected lifetime measured in weeks rather than months. If that application is extremely performance-critical. If there is no meaningful numerical processing involved that may cause performance problems. If there is no external database, internet connection, file IO or just any IO involved. If you're using a compiler with a crappy optimizer. If you've profiled your code to show that the performance cost of adding that abort flag as an independent variable is raising your performance just over the threshold of acceptance. Then you have a good reason not to use one.
GeneralRe: Which code you suggest? Pin
Renzo Ciafardone29-Jul-13 14:25
Renzo Ciafardone29-Jul-13 14:25 
GeneralRe: Which code you suggest? Pin
Stefan_Lang29-Jul-13 21:26
Stefan_Lang29-Jul-13 21:26 
GeneralRe: Which code you suggest? Pin
BobJanova29-Jul-13 0:23
BobJanova29-Jul-13 0:23 
GeneralRe: Which code you suggest? Pin
Stefan_Lang29-Jul-13 2:44
Stefan_Lang29-Jul-13 2:44 
AnswerRe: Which code you suggest? Pin
BotReject26-Jul-13 0:47
BotReject26-Jul-13 0:47 
AnswerRe: Which code you suggest? Pin
Kenworth7126-Jul-13 1:04
professionalKenworth7126-Jul-13 1:04 
AnswerRe: Which code you suggest? Pin
nocturns226-Jul-13 3:29
nocturns226-Jul-13 3:29 
AnswerRe: Which code you suggest? Pin
RafagaX26-Jul-13 4:46
professionalRafagaX26-Jul-13 4:46 
AnswerRe: Which code you suggest? Pin
MarkTJohnson26-Jul-13 5:03
professionalMarkTJohnson26-Jul-13 5:03 
GeneralRe: Which code you suggest? Pin
jibalt26-Jul-13 17:28
jibalt26-Jul-13 17:28 
GeneralRe: Which code you suggest? Pin
MarkTJohnson28-Jul-13 14:41
professionalMarkTJohnson28-Jul-13 14:41 
GeneralRe: Which code you suggest? Pin
BobJanova29-Jul-13 0:17
BobJanova29-Jul-13 0:17 
GeneralRe: Which code you suggest? Pin
jibalt19-Aug-13 23:19
jibalt19-Aug-13 23:19 
GeneralRe: Which code you suggest? Pin
MarkTJohnson20-Aug-13 5:05
professionalMarkTJohnson20-Aug-13 5:05 
GeneralMessage Closed Pin
10-Feb-14 13:17
jibalt10-Feb-14 13:17 
GeneralRe: Which code you suggest? Pin
MarkTJohnson11-Feb-14 4:11
professionalMarkTJohnson11-Feb-14 4:11 
GeneralRe: Which code you suggest? Pin
jibalt26-Jul-13 17:30
jibalt26-Jul-13 17:30 

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.