You'll be glad to know! [modified]
-
Ok, maybe I should say what I did, so maybe an anti-goto purist can give me tips on how to write goto-less nice code in such circumstances... (or become less anti goto) so here we go, that's what I wrote (veil your eyes, code in the lounge!)
for(...)
{
avar = calculation()
if(avar == someValue)
goto doStuff;
avar2 = someOtherCalculation();
if(avar2 == someValue2)
goto doStuff;
continue;
doStuff:;
localVar.UpdateNicely();
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
How about
for(...)
{
avar = calculation()
if(avar != someValue)
{
avar2 = someOtherCalculation();
if(avar2 != someValue2)
continue;
}
localVar.UpdateNicely();
}The point is basically that
if (something)
goto somewhere
more_stuff;
somewhere:is equivalent to
if (!something)
more_stuff;and you've just got two of them nested. Cheers from one who thinks coding standards are generally a good thing but do not represent ultimate authority. Peter
Software rusts. Simon Stephenson, ca 1994.
-
You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.
for (...) {
if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
}Readability is the key concern here. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
well, I wanted to use some simple pseudo code, but the statement "var1 = calculation()" is really a multi inner variable multiline statement with a more complex statement. I guess I can replace it with a private static variable with heaps of ref variable just or the sake of writing such statement as you sugest, but I won't!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
I thought about it and... despite my initial inflamatory intention I'm thinking we could both gain by showing you what I did, who knows one of 2 thing might even happen!! 1. maybe you'll think this is a good goto! 2. maybe you'll give me good work around so, here you go (veil your eyes, code in the lounge) please remove the goto in the nice fashion or hold your peace forever!
for(...)
{
avar = calculation()
if(avar == someValue)
goto doStuff;
avar2 = someOtherCalculation();
if(avar2 == someValue2)
goto doStuff;
continue;
doStuff:;
localVar.UpdateNicely();
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
No excuse!
for(...)
{
if(calc() == v || calc2() == v2)
{
localVar.UpdateNicely();
}
} -
You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.
for (...) {
if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
}Readability is the key concern here. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Dang, you beat me.
-
How about
for(...)
{
avar = calculation()
if(avar != someValue)
{
avar2 = someOtherCalculation();
if(avar2 != someValue2)
continue;
}
localVar.UpdateNicely();
}The point is basically that
if (something)
goto somewhere
more_stuff;
somewhere:is equivalent to
if (!something)
more_stuff;and you've just got two of them nested. Cheers from one who thinks coding standards are generally a good thing but do not represent ultimate authority. Peter
Software rusts. Simon Stephenson, ca 1994.
Indeed that will do! :thumbsup: :) However.... I do prefer goto to multiple nested statement! ;P
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Dang, you beat me.
the advantage of a keyboard with a very short cable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Indeed that will do! :thumbsup: :) However.... I do prefer goto to multiple nested statement! ;P
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.
for (...) {
if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
}Readability is the key concern here. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
there is the matter of avoiding goto, but this is no big deal, the real challenge is to do it in aesthetic and readable way! Well, I guess it's a subjective matter... But just to give you better chance, here is the real code. I think goto is the most elegant / beautiful solution. I invite you to show me the beauty of avoiding goto! (Although.. being a subjective thing... well....) Here we go:
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if test above fail
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
modified on Thursday, September 9, 2010 8:31 PM
-
Yesterday I wrote no less than 2 gotos in my C# code! [Edit2] This guy[^] found a satisfying refactoring! One which doesn't tax my (limited brain) memory with nested block, additional variable, additional test, increased cyclomatic complexity! (In fact the cyclomatic complexity is reduced by 1, I think) Well done! :thumbsup: :cool: :-D [EDIT] For your information the real code is below. So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less) -- code --
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
modified on Friday, September 10, 2010 10:53 AM
The only acceptable place I've found for a goto is within a switch block. After all, the "labels" are already defined.
-
Yesterday I wrote no less than 2 gotos in my C# code! [Edit2] This guy[^] found a satisfying refactoring! One which doesn't tax my (limited brain) memory with nested block, additional variable, additional test, increased cyclomatic complexity! (In fact the cyclomatic complexity is reduced by 1, I think) Well done! :thumbsup: :cool: :-D [EDIT] For your information the real code is below. So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less) -- code --
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
modified on Friday, September 10, 2010 10:53 AM
Until I did some refactoring today I had a method with three return statements! :omg: (They were to avoid a goto to abort a nested loop.)
-
well, I wanted to use some simple pseudo code, but the statement "var1 = calculation()" is really a multi inner variable multiline statement with a more complex statement. I guess I can replace it with a private static variable with heaps of ref variable just or the sake of writing such statement as you sugest, but I won't!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Well, the complexity of
avar1 = someCalculation();
isn't relevant, you can do it upfront anyway. The complexity ofavar2 = someOtherCalculation();
is what matters. If it can't be put in the middle of the if clause, I'd use a boolean flag, like so:for (...) {
bool updateWanted=false;
if (calculation()==someValue) updateWanted=true;
else if (someOtherCalculation()==someValue2) updateWanted=true;
if (updateWanted) localVar.UpdateNicely();
}and now you can easily expand line 4 (and 3) as required. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Thursday, September 9, 2010 8:52 PM
-
Until I did some refactoring today I had a method with three return statements! :omg: (They were to avoid a goto to abort a nested loop.)
PIEBALDconsult wrote:
avoid a goto to abort a nested loop
I usually set some bool, break, then conditionally break depending on the value in the bool. Slows things down a bit, so a goto might be more acceptable in this case if speed is of significant importance and the inner loop does not have many iterations.
-
it's like writing poetry, just keep scrapping the redundant bits until you start to like the result. Except you have to adhere to the original intentions... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Well, the complexity of
avar1 = someCalculation();
isn't relevant, you can do it upfront anyway. The complexity ofavar2 = someOtherCalculation();
is what matters. If it can't be put in the middle of the if clause, I'd use a boolean flag, like so:for (...) {
bool updateWanted=false;
if (calculation()==someValue) updateWanted=true;
else if (someOtherCalculation()==someValue2) updateWanted=true;
if (updateWanted) localVar.UpdateNicely();
}and now you can easily expand line 4 (and 3) as required. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Thursday, September 9, 2010 8:52 PM
see the code below! it can't be done upfront, as the first if will avoid as divide by zero in the second if! just for your benefit I post the code below again! Anyway, yes it can be done, but I found my way more readable... I guess it's too subjective for debate.. but I invite you to show me the new version:
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Thanks! Glad someone agree! I think I was looking for a beating... but I have a hard time handling it! :laugh:
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Indeed that will do! :thumbsup: :) However.... I do prefer goto to multiple nested statement! ;P
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Two comments. 1. Loops are only nested too deep when you need to indent each level by less than [insert favourite small number here, mine's 4] spaces to fit it on the screen. ;P The construct
if (!test)
goto next;
do_stuff;
next:is actually what a compiler* will emit if you feed it
if (test)
do_stuff;Ironic, huh? But maybe that's why we deal in the input to compilers and leave their output unseen. * for a typical target instruction set
Software rusts. Simon Stephenson, ca 1994.
-
Until I did some refactoring today I had a method with three return statements! :omg: (They were to avoid a goto to abort a nested loop.)
Ho my god! That Is Bad! :laugh:
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
there is the matter of avoiding goto, but this is no big deal, the real challenge is to do it in aesthetic and readable way! Well, I guess it's a subjective matter... But just to give you better chance, here is the real code. I think goto is the most elegant / beautiful solution. I invite you to show me the beauty of avoiding goto! (Although.. being a subjective thing... well....) Here we go:
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if test above fail
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
modified on Thursday, September 9, 2010 8:31 PM
If you really don't want to put some of that logic into functions, this is one way to go:
for(...)
{
bool doRemove = false;
var v1 = a();
var v2 = b();
if(v1.a() || v2.a())
{
doRemove = true;
}
else
{
v1 = c();
v2 = d();
var z = e();
if(e(z, v1, v2))
{
doRemove = true;
}
}
if(doRemove)
{
RemovePoint();
}
}I put in some placeholders where I didn't feel like typing.
-
Two comments. 1. Loops are only nested too deep when you need to indent each level by less than [insert favourite small number here, mine's 4] spaces to fit it on the screen. ;P The construct
if (!test)
goto next;
do_stuff;
next:is actually what a compiler* will emit if you feed it
if (test)
do_stuff;Ironic, huh? But maybe that's why we deal in the input to compilers and leave their output unseen. * for a typical target instruction set
Software rusts. Simon Stephenson, ca 1994.
mmh... so you mean your way is better because it will compile to my way?!? ;P anyway, at this stage it's a matter of taste, your solution is elegant too! but too many bracket confuses me and the compiler in my brain! (as opposed to the one on the hard drive ;) )
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
see the code below! it can't be done upfront, as the first if will avoid as divide by zero in the second if! just for your benefit I post the code below again! Anyway, yes it can be done, but I found my way more readable... I guess it's too subjective for debate.. but I invite you to show me the new version:
for (int i = max; i >= min; i--)
{
var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
goto RemovePoint;
v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
v2 = v2.Normalize();
var z = v1 ^ v2;
if (Math.Abs(z) <= minsin && v1 * v2 < 0)
goto RemovePoint;
continue;
RemovePoint: ;
points.RemoveAt(i);
if (points.Count < 3)
return null;
}A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
I already gave my preferred structure here[^], and I think we've had enough code in the Lounge for this week... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.