You'll be glad to know! [modified]
-
I'm not as much of a hater of goto's as most people, but without goto's I'd write it as:
for (int i = max; i >= min; i--)
{
bool removePoint = false;
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)
{
removePoint = true;
}
else
{
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)
removePoint = true;
}if (removePoint) { points.RemoveAt(i); if (points.Count < 3) return null; }
}
Either way, it's pretty clear. Yours doesn't require the extra allocation/initialization/assignment of the flag, so it's slightly more efficient, which could be important if you're in a huge loop.
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
Glad to see that not everyone give in the anti goto dogma, an actually look at the code! :-D And yeah, I have been unconvinced by all the gymnastic to get rid of the goto so far. Seems much easier, shorter, etc... with it! I'm not really sure why I made this post in fact.. but I think I learn something about communication! :)
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.
Ouch. I recommend:
for (...) {
avar = calculation();
if (avar == someValue) {
localVar.UpdateNicely();
}
else {
avar2 = someOtherCalculation();
if (avar2 == someValue2) {
localVar.UpdateNicely();
}
}
}If
avar
andavar2
are purely local to thefor
, you could reduce this to:for (...) {
if ((calculation() == someValue) || (someOtherCalculation() == someValue2)) {
localVar.UpdateNicely();
}
}/ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Ouch. I recommend:
for (...) {
avar = calculation();
if (avar == someValue) {
localVar.UpdateNicely();
}
else {
avar2 = someOtherCalculation();
if (avar2 == someValue2) {
localVar.UpdateNicely();
}
}
}If
avar
andavar2
are purely local to thefor
, you could reduce this to:for (...) {
if ((calculation() == someValue) || (someOtherCalculation() == someValue2)) {
localVar.UpdateNicely();
}
}/ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Real code here[^]. you could only do your first version. which I find more cumbersome than the goto version because: - it has more bracket and line of code - some code is repeated (localvar.UpdateNicel() contains, in fact, a conditional return, hence it can't be put into a function) In fact the version with goto is the best or equals to all other solution in term of - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less)
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.
-
ok, I guess this one will work and is readable! :thumbsup: but I still prefer my way! :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.
Super Lloyd wrote:
but I still prefer my way
Of course you do, it is yours after all. Be prepared to defend a law suit some years down the track when a support dev has a heart attack when they see it. At which point the thing will be refactored to something sensible. And yes I'm one of those who will go to extreme lengths to eliminate goto.
Never underestimate the power of human stupidity RAH
-
Maybe you an answer this other question. Somewhere I posted my real code and somewhere some posted the nested if version. Personally I found my version way simpler and maintainable. It has less line, less bracket, less variable a lower cyclomatic complexity. Now where does this "absolutely no goto" religion comes from? I just can't bring myself to prefer the nested if extra variable more lines of code version.... :omg: Anyway, I can see this is going no where. I did learn something, it's pointless to post against people belief, nothing came out of it! I haven't change my opinion, nor did they! and I'm upset!
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.
If you don't like brackets, refactor the nested loop into another method. The way I see it you just WANT to use goto's even though you know that 'most other people' (TM) would prefer the equally valid version without goto's. Personally it upsets me that people use language features for no good reason. 'I don't like brackets' is not a good reason at all. Code with a goto is always harder to decipher than code without.
-
Glad to see that not everyone give in the anti goto dogma, an actually look at the code! :-D And yeah, I have been unconvinced by all the gymnastic to get rid of the goto so far. Seems much easier, shorter, etc... with it! I'm not really sure why I made this post in fact.. but I think I learn something about communication! :)
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.
Right on. If you start using nested gotos, or didn't label them nicely, I'd have to hire JSOP to come after you. :-D Remember, it's all good code, until you have to maintain it. Double :-D
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
-
If you don't like brackets, refactor the nested loop into another method. The way I see it you just WANT to use goto's even though you know that 'most other people' (TM) would prefer the equally valid version without goto's. Personally it upsets me that people use language features for no good reason. 'I don't like brackets' is not a good reason at all. Code with a goto is always harder to decipher than code without.
Basically you don't want goto, why do I need to do as you like? No compelling reason so far!
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.
-
If you don't like brackets, refactor the nested loop into another method. The way I see it you just WANT to use goto's even though you know that 'most other people' (TM) would prefer the equally valid version without goto's. Personally it upsets me that people use language features for no good reason. 'I don't like brackets' is not a good reason at all. Code with a goto is always harder to decipher than code without.
In fact, I noticed you gave an argument: "is harder to decipher" It's a good argument, it just happen I find the goto version more readable! There is nothing much to say...
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.
-
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
Super Lloyd wrote:
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)
Um, setting arbitrary rules to suit your purpose only shows that you've used goto for arbitrary purposes. Cheers, Drew.
-
Super Lloyd wrote:
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)
Um, setting arbitrary rules to suit your purpose only shows that you've used goto for arbitrary purposes. Cheers, Drew.
mm.. I though i a good idea because people were showing other code which they preferred but I found more confusing. So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!
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'm not as much of a hater of goto's as most people, but without goto's I'd write it as:
for (int i = max; i >= min; i--)
{
bool removePoint = false;
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)
{
removePoint = true;
}
else
{
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)
removePoint = true;
}if (removePoint) { points.RemoveAt(i); if (points.Count < 3) return null; }
}
Either way, it's pretty clear. Yours doesn't require the extra allocation/initialization/assignment of the flag, so it's slightly more efficient, which could be important if you're in a huge loop.
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
-
Now just get rid of that mid-loop return and we'll all be happy! (any arbitrary break in flow is the same as a goto in my book). Cheers, Drew.
Yeah, but kinda depends on what's after the loop though. Could replace with a
break;
but then you might end up having to do anotherif (points.Count < 3)
Sometimes getting the hell out of dodge is the best option. Depends, it always just depends. As long as it's clear what's going on, right? 1001 ways to skin a cat (although I would never do that, unless I was beyond hunger, or it was a Kobe cat!) :)
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
-
mm.. I though i a good idea because people were showing other code which they preferred but I found more confusing. So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!
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.
Super Lloyd wrote:
So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!
Of course you code to a standard that you prefer. Everyone does. What others are trying to explain to you is that there are other ways that, once you get used to them, lead to a lot fewer problems with bugs and software maintenance. Structure in your code is one of those ways; gotos and returns from within loops are two things that dimish the structure of your program and make it very hard to understand by people who didn't write the code in the first place. Adding conditionals with well named variables is an easy way to get rid of gotos and gives you additional contextual clues that help you and others understand what your program is doing and why, even if you're looking at it years from now. Cheers, Drew.
-
Super Lloyd wrote:
So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!
Of course you code to a standard that you prefer. Everyone does. What others are trying to explain to you is that there are other ways that, once you get used to them, lead to a lot fewer problems with bugs and software maintenance. Structure in your code is one of those ways; gotos and returns from within loops are two things that dimish the structure of your program and make it very hard to understand by people who didn't write the code in the first place. Adding conditionals with well named variables is an easy way to get rid of gotos and gives you additional contextual clues that help you and others understand what your program is doing and why, even if you're looking at it years from now. Cheers, Drew.
My reply is that I should not have reply to all this post, this is going nowhere! Particularly all those alternative are nothing new, so it's mostly boring... Just for the record I write an average of 1 goto a year. I'm happy with it and last I look back at 10 years old code with goto, I still find it much easier to understand this way!
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.
-
Yeah, but kinda depends on what's after the loop though. Could replace with a
break;
but then you might end up having to do anotherif (points.Count < 3)
Sometimes getting the hell out of dodge is the best option. Depends, it always just depends. As long as it's clear what's going on, right? 1001 ways to skin a cat (although I would never do that, unless I was beyond hunger, or it was a Kobe cat!) :)
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
Steve Echols wrote:
Could replace with a break;
Ug, I hate 'breaks' too. I guess I've never seen a good reason to break the flow of code with any of those things. I'd just add another variable and throw it in with the conditional in the for loop and then check it again after the loop if necessary (like you say - depends on what else is going on). Adds more code, but the conditional variable at least makes it obvious why the loop is being aborted, and the check for it goes with the loop construct where it should be anyway (all the reasons for stopping the loop are in one place).
var result=null;
bool enoughPoints=true;
for (int i = max; i >= min && enoughPoints; i--)
{
bool removePoint = false;
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)
{
removePoint = true;
}
else
{
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)
{
removePoint = true;
}
}
if (removePoint)
{
points.RemoveAt(i);
if (points.Count < 3)
{
enoughPoints=false;
}
}
}
if(enoughPoints)
{
...some other code that may affect 'result'
}
return result;Cheers, Drew.
-
My reply is that I should not have reply to all this post, this is going nowhere! Particularly all those alternative are nothing new, so it's mostly boring... Just for the record I write an average of 1 goto a year. I'm happy with it and last I look back at 10 years old code with goto, I still find it much easier to understand this way!
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.
-
Super Lloyd wrote:
My reply is that I should not have reply to all this post, this is going nowhere!
Heh, then why are you replying? Cheers, Drew.
Oops, I mean "my problem"! But... you're right! :laugh: (I'm still learning) .....
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.
-
Steve Echols wrote:
Could replace with a break;
Ug, I hate 'breaks' too. I guess I've never seen a good reason to break the flow of code with any of those things. I'd just add another variable and throw it in with the conditional in the for loop and then check it again after the loop if necessary (like you say - depends on what else is going on). Adds more code, but the conditional variable at least makes it obvious why the loop is being aborted, and the check for it goes with the loop construct where it should be anyway (all the reasons for stopping the loop are in one place).
var result=null;
bool enoughPoints=true;
for (int i = max; i >= min && enoughPoints; i--)
{
bool removePoint = false;
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)
{
removePoint = true;
}
else
{
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)
{
removePoint = true;
}
}
if (removePoint)
{
points.RemoveAt(i);
if (points.Count < 3)
{
enoughPoints=false;
}
}
}
if(enoughPoints)
{
...some other code that may affect 'result'
}
return result;Cheers, Drew.
Depends :) As long as there's nothing after the if (removePoints) block that relies on having enough points, that works for me! Just gotta structure everything right, to minimize checks. Dang depends! Starting to feel like I'm about to have a coding accident now! :-D
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
-
Depends :) As long as there's nothing after the if (removePoints) block that relies on having enough points, that works for me! Just gotta structure everything right, to minimize checks. Dang depends! Starting to feel like I'm about to have a coding accident now! :-D
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.
-
:thumbsup: Great minds think alike!
- S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.