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: Do not trust a computer... Pin
Lutosław12-Dec-08 9:57
Lutosław12-Dec-08 9:57 
GeneralAvoid return statement in the middle - horror or not? Pin
Georgi Atanasov5-Dec-08 12:01
Georgi Atanasov5-Dec-08 12:01 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
Robert.C.Cartaino5-Dec-08 14:32
Robert.C.Cartaino5-Dec-08 14:32 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
John M. Drescher11-Dec-08 8:24
John M. Drescher11-Dec-08 8:24 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult5-Dec-08 15:07
mvePIEBALDconsult5-Dec-08 15:07 
General[Message Deleted] Pin
Robert.C.Cartaino6-Dec-08 5:47
Robert.C.Cartaino6-Dec-08 5:47 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
harold aptroot6-Dec-08 6:48
harold aptroot6-Dec-08 6:48 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Robert.C.Cartaino6-Dec-08 11:52
Robert.C.Cartaino6-Dec-08 11:52 
harold aptroot wrote:
Java also seems to behave differently from what you said:


I think you are right. I was looking at this section[^] of the Java language specification:

15.7.2 Evaluate Operands before Operation<br />
The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? : ) appears to be fully evaluated before any part of the operation itself is performed.


They do explicitly exempt the logical operators. Let's hope that && is not overloaded -- in C++ at least -- then short circuiting does not apply.

Then I was reading this on C++ Sequence Points[^]. It says that "the left operand of the logical AND operator is completely evaluated and all side effects completed before continuing. There is no guarantee that the right operand of the logical AND operator will be evaluated.

Of course, it doesn't say that is guaranteed not to be evaluated, either.

Then it says this: "The controlling expression in a selection (if or switch) statement. The expression is completely evaluated and all side effects completed before the code dependent on the selection is executed."

So, who knows. My conclusion would be that a C/C++ compiler is indeed supposed to do left-right evaluation and short circuiting.

But I still say it's irrelevant. If I have to hunt that hard to get down into the bowels of how a compiler works to determine how my code will execute, I say it's a bad programming practice.

I'll take these guys word for it:
...The moral is that writing code that depends on order of evaluation is a bad programming practice in any language. Naturally, it is necessary to know what things to avoid, but if you don't know how they are done on various machines, you won't be tempted to take advantage of a particular implementation. -- The C Programming Language[^], Kernighan & Ritchie, p. 49.

Enjoy,

Robert C. Cartaino
GeneralRe: Avoid return statement in the middle - horror or not? Pin
harold aptroot6-Dec-08 12:10
harold aptroot6-Dec-08 12:10 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
PIEBALDconsult6-Dec-08 13:44
mvePIEBALDconsult6-Dec-08 13:44 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
Graham Bradshaw6-Dec-08 7:43
Graham Bradshaw6-Dec-08 7:43 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult6-Dec-08 11:16
mvePIEBALDconsult6-Dec-08 11:16 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Mladen Janković9-Dec-08 5:25
Mladen Janković9-Dec-08 5:25 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult9-Dec-08 6:53
mvePIEBALDconsult9-Dec-08 6:53 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
johannesnestler23-Dec-08 1:56
johannesnestler23-Dec-08 1:56 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
ehuysamer5-Dec-08 22:25
ehuysamer5-Dec-08 22:25 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Tristan Rhodes5-Dec-08 23:39
Tristan Rhodes5-Dec-08 23:39 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
cokkiy6-Dec-08 1:00
cokkiy6-Dec-08 1:00 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Robert.C.Cartaino6-Dec-08 6:05
Robert.C.Cartaino6-Dec-08 6:05 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
riced6-Dec-08 8:30
riced6-Dec-08 8:30 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult7-Dec-08 6:15
mvePIEBALDconsult7-Dec-08 6:15 
GeneralRe: Avoid return statement in the middle - horror or not? PinPopular
Michael Dunn7-Dec-08 20:40
sitebuilderMichael Dunn7-Dec-08 20:40 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Member 16746110-Dec-08 1:13
Member 16746110-Dec-08 1:13 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 3:16
mvePIEBALDconsult10-Dec-08 3:16 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Andrew Torrance8-Dec-08 22:23
Andrew Torrance8-Dec-08 22:23 

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.