Do it only once please
-
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-DThe loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:
while (true) // loop 1 time, exit at bottom { if (set (ifrFragment)) { result = true; ifrTerm = ifrFragment; break; } if (errorCode) break; if (keyword (TokenSubtype::Not) && term (ifrFragment)) { result = true; ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment; break; } if (errorCode) break; // more lines snipped break; }
But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.
-
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-DIf it ain't broke...
-
The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:
while (true) // loop 1 time, exit at bottom { if (set (ifrFragment)) { result = true; ifrTerm = ifrFragment; break; } if (errorCode) break; if (keyword (TokenSubtype::Not) && term (ifrFragment)) { result = true; ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment; break; } if (errorCode) break; // more lines snipped break; }
But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.
lol. Haven't seen that done in a long time. We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
-
lol. Haven't seen that done in a long time. We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
Robert Surtees wrote:
We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
Robert, I use goto's for exactly the same reason.
if (keyword (TokenSubtype::Group)) { if (!group\_label (groupName)) { errorMessage = myName + ": Missing GROUP label\\n" + errorMessage; errorCode = DL\_ERROR; **goto exit\_;** } if (!separator (TokenSubtype::Colon)) { errorMessage = myName + ": Missing colon following GROUP label"; errorCode = DL\_ERROR; **goto exit\_;** }
// etc...
-
The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:
while (true) // loop 1 time, exit at bottom { if (set (ifrFragment)) { result = true; ifrTerm = ifrFragment; break; } if (errorCode) break; if (keyword (TokenSubtype::Not) && term (ifrFragment)) { result = true; ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment; break; } if (errorCode) break; // more lines snipped break; }
But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.
But then why not
do
{
...
} while ( false ) ; -
But then why not
do
{
...
} while ( false ) ;Your way is better.
-
The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:
while (true) // loop 1 time, exit at bottom { if (set (ifrFragment)) { result = true; ifrTerm = ifrFragment; break; } if (errorCode) break; if (keyword (TokenSubtype::Not) && term (ifrFragment)) { result = true; ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment; break; } if (errorCode) break; // more lines snipped break; }
But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.
heh. of course that's just a coding-standards-compliant way of writing:
if (set (ifrFragment)) { result = true; ifrTerm = ifrFragment; goto done; } if (errorCode) goto done; if (keyword (TokenSubtype::Not) && term (ifrFragment)) { result = true; ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment; goto done; } if (errorCode) goto done; // more lines snipped
done:
:laugh:
-
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-DYou're sure you're not mistaking that "one" in lnIndice <= 1 for an "ell"? Marc
-
Robert Surtees wrote:
We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
Robert, I use goto's for exactly the same reason.
if (keyword (TokenSubtype::Group)) { if (!group\_label (groupName)) { errorMessage = myName + ": Missing GROUP label\\n" + errorMessage; errorCode = DL\_ERROR; **goto exit\_;** } if (!separator (TokenSubtype::Colon)) { errorMessage = myName + ": Missing colon following GROUP label"; errorCode = DL\_ERROR; **goto exit\_;** }
// etc...
I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...
-
I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...
-
Robert Surtees wrote:
We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
Robert, I use goto's for exactly the same reason.
if (keyword (TokenSubtype::Group)) { if (!group\_label (groupName)) { errorMessage = myName + ": Missing GROUP label\\n" + errorMessage; errorCode = DL\_ERROR; **goto exit\_;** } if (!separator (TokenSubtype::Colon)) { errorMessage = myName + ": Missing colon following GROUP label"; errorCode = DL\_ERROR; **goto exit\_;** }
// etc...
Tony Wesley wrote:
I use goto's
It's OK, so do I, in C#! :)
xacc.ide
IronScheme a R5RS-compliant Scheme on the DLR
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach." -
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-DThat's what I call extendable coding. ;P
-
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-DFINALLY!!!! My first entry on a coding horror! Is this a sign I am starting to learn, possibly, but even in goood old fashioned ms basic circa 1981 I would have spotted this load of old codswallop! So pleased to say that I would never have done this!:-D
------------------------------------ Happy Primes Lead to Happy Memories. Don't Google FGI
-
I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...
chmod2222 wrote:
I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...
-
chmod2222 wrote:
I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...
I aint affraid to use it bubba joe :) I just don't see the need for it... At least in C#...
-
FINALLY!!!! My first entry on a coding horror! Is this a sign I am starting to learn, possibly, but even in goood old fashioned ms basic circa 1981 I would have spotted this load of old codswallop! So pleased to say that I would never have done this!:-D
------------------------------------ Happy Primes Lead to Happy Memories. Don't Google FGI
-
Today we had to edit very old code and we ran into this great piece of code
for (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } }
We found it very funny :-D -
I aint affraid to use it bubba joe :) I just don't see the need for it... At least in C#...
C#? Sounds like a language for quiche eaters. :) Seriously, there are times when nested if/else constructs get unweildy. In some cases, albeit rarely, I'd rather use a goto. I only use them to transfer control either to a error handling section or a single exit point.
-
Robert Surtees wrote:
We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.
Robert, I use goto's for exactly the same reason.
if (keyword (TokenSubtype::Group)) { if (!group\_label (groupName)) { errorMessage = myName + ": Missing GROUP label\\n" + errorMessage; errorCode = DL\_ERROR; **goto exit\_;** } if (!separator (TokenSubtype::Colon)) { errorMessage = myName + ": Missing colon following GROUP label"; errorCode = DL\_ERROR; **goto exit\_;** }
// etc...
-
So what happens to your "goto" cleanup routine when an exception is thrown? I'll give you a hint -- it doesn't get called.
CurtD wrote:
So what happens to your "goto" cleanup routine when an exception is thrown? I'll give you a hint -- it doesn't get called.
The goto doesn't figure into that. Just above it in the thread is another snippet of my code that doesn't use a goto. Same issue. What happens to the code after the while loop when an exception is thrown? It doesn't get executed. Once could argue that the problem is with exception. As I pointed out elsewhere, Joel Spolsky argues that exceptions [...] create an abrupt jump from one point of code to another. In fact they are significantly worse than goto's: (emphasis added) I don't quite agree with Joel. But there was a time, when I was debugging an app that another programmer had written, that I understood that completely. It was spaghetti exception handling. I never knew where I had come from. I'm not saying you shouldn't use exceptions. But they can be abused as badly as gotos.