for those of you purists that don't like break, continue and goto
-
i mean, i agree that it shouldn't, but it causes control flow changes and can be used that way. but like i said, i agree. Just because you can do something, doesn't mean you should.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
I've seen MS themselves doing it. Sometimes when external code that you don't have control over lacks certain functionality, it might be your only choice. (been there, done that. X| )
Wrong is evil and must be defeated. - Jeff Ello
-
I've seen MS themselves doing it. Sometimes when external code that you don't have control over lacks certain functionality, it might be your only choice. (been there, done that. X| )
Wrong is evil and must be defeated. - Jeff Ello
right? that's why i *never* throw! kidding! but in seriousness I take great pains to prevent the users of my code from having to catch exceptions on failure if they don't want them. Like I'll provide TryXXXX to complement XXXX Pck: Code Roundup and Quick Start Guide[^] In PCK I have an elaborate error handling system whereby it finds errors and continues processing only (optionally) throwing an exception at the end with ALL the errors it encountered. Otherwise they're reported as "messages" with different severity/errorlevels probably the most elaborate exception handling i've had to do in managed code.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
why do this?
for(int i = 0;i
instead offor(int i = 0;i
hengh?? why you still use break?
:laugh:
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
Depending on the length of the array, just let it loop :D I rarely use break or continue (break slightly more often, but probably only a handful in my entire career). I've never used goto, except to mock you I think :D I'd use FirstOrDefault here (which probably uses a break, I don't know). This kind of functionality is embedded in .NET, so why would I write my own? But if I had to pick, I'd choose the break, which far more clearly communicates intent than i=arr.Length;.
honey the codewitch wrote:
} // braces copyright Sander Rossel
So proud of my honeycode the witch :D
Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
Depending on the length of the array, just let it loop :D I rarely use break or continue (break slightly more often, but probably only a handful in my entire career). I've never used goto, except to mock you I think :D I'd use FirstOrDefault here (which probably uses a break, I don't know). This kind of functionality is embedded in .NET, so why would I write my own? But if I had to pick, I'd choose the break, which far more clearly communicates intent than i=arr.Length;.
honey the codewitch wrote:
} // braces copyright Sander Rossel
So proud of my honeycode the witch :D
Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
if code is hard to write, it should be hard to read. :laugh: i'm a sucker for symmetry.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
why do this?
for(int i = 0;i
instead offor(int i = 0;i
hengh?? why you still use break?
:laugh:
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
At times, the purpose of the search might be just to find the index of the valueToFind. Break keeps the index safe?
(JS)
var arr =[0,1,2,3,4,5];
var valueToFind = 3;for(i=0;i
right, but in the OP i limited
i
to the loop scope, but yeah.When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
why do this?
for(int i = 0;i
instead offor(int i = 0;i
hengh?? why you still use break?
:laugh:
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
I prefer break statements. It is not suitable for big projects it might be confusing. I try my level best to write clean code!
-
why do this?
for(int i = 0;i
instead offor(int i = 0;i
hengh?? why you still use break?
:laugh:
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
Because the second fragment duplicates knowledge of the loop's termination condition. You have to remember to adjust it in two places. The first fragment is therefore more robust.
Software Zen:
delete this;
-
Because the second fragment duplicates knowledge of the loop's termination condition. You have to remember to adjust it in two places. The first fragment is therefore more robust.
Software Zen:
delete this;
yep. although there are cases where I'll modify i inside the loop for other reasons. Like if I have to add or remove items while enumerating (it happens with complicated algos)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
why do this?
for(int i = 0;i
instead offor(int i = 0;i
hengh?? why you still use break?
:laugh:
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
It was sample code. Normally you'd account for that when you put actual code in there, but without putting anything in there, it's not for me to know where the actual break should happen. However, it was sort of written with the idea that it would follow the main loop logic, kind of like the break example would. Of course the control flow is slightly different, but it's not irreconcilably different. if you need something to go after the conditional check, you'd wrap whatever went after the break in the one example in an else block instead.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
It was sample code. Normally you'd account for that when you put actual code in there, but without putting anything in there, it's not for me to know where the actual break should happen. However, it was sort of written with the idea that it would follow the main loop logic, kind of like the break example would. Of course the control flow is slightly different, but it's not irreconcilably different. if you need something to go after the conditional check, you'd wrap whatever went after the break in the one example in an else block instead.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
Yeas it the simpliest possible 5 lines code. In real life you would probably have another couple of hundreds lines of similar mess entagled there, with some poor soul wondering why is this piece of crap executing when it was not supposed to.
:laugh: it's funny cuz it's true. For the record, I'm not endorsing the method i described, I'm simply being facetious about it. I think it's silly. A break statement is much clearer, which was kind of the point of my OP. Sometimes you need break. Or a continue. Or even a goto (which i can give a solid use case for - in this case making the code MORE readable)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
:laugh: it's funny cuz it's true. For the record, I'm not endorsing the method i described, I'm simply being facetious about it. I think it's silly. A break statement is much clearer, which was kind of the point of my OP. Sometimes you need break. Or a continue. Or even a goto (which i can give a solid use case for - in this case making the code MORE readable)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
Alright, here a piece of advice: never exit conditionally a for cycle. if you need to do that, use while or do until.
i think it depends on the situation for example
public int IndexOf(T item)
{
var ic = Count;
var i = 0;
for(;iThat's very clear even without comments
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
i think it depends on the situation for example
public int IndexOf(T item)
{
var ic = Count;
var i = 0;
for(;iThat's very clear even without comments
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
i mean yeah. That's a little less clear to me though. I avoid do loops usually because the conditional can get confusing if you're used to regular for loops. It takes me a second to work out what's going on, like when exactly the condition exits. It's not big deal, it's just my preference. I think my code is clearer.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
i mean yeah. That's a little less clear to me though. I avoid do loops usually because the conditional can get confusing if you're used to regular for loops. It takes me a second to work out what's going on, like when exactly the condition exits. It's not big deal, it's just my preference. I think my code is clearer.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
It is simpler and I actually use pretty much exactly the same code when it is that simple. When you have more than a page of lines within that loop a sudden exit is not what I would do. And a sudden conditional exit with break or return might be even ok. Putting the i to the max and then exit in that manner is something I would never do.
-
It is simpler and I actually use pretty much exactly the same code when it is that simple. When you have more than a page of lines within that loop a sudden exit is not what I would do. And a sudden conditional exit with break or return might be even ok. Putting the i to the max and then exit in that manner is something I would never do.
well to each their own. probably it also has to do with my C++ background which influences a lot of my code. Not that C++ has different flow constructs, it's just that a) i learned these habits a long time ago and the industry changes b) C++ development is a different animal, and control flow is all over the place generally and for loops are used for almost everything except while(true) although some people go for(;;). It's not uncommon even to do stuff like
for(current=firstNode;null!=current;current=current.nextNode) if(current->key==key) break;
to traverse a linked list for example.When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
-
yep. although there are cases where I'll modify i inside the loop for other reasons. Like if I have to add or remove items while enumerating (it happens with complicated algos)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
That makes sense, especially given the type of algorithms you deal with in parsers and data structures. In the course of developing several large, complex applications, I've learned that having pieces of code that must stay in sync logically or follow the same algorithm is a failure point. Refactoring can help if i makes sense to move things into a method, and then have each location invoke the method. The hard part there can be figuring out a name for the thing: "
CheckToSeeIfMessageNeededAtThreadExit
" is ugly :-D.Software Zen:
delete this;