|
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 .
Software Zen: delete this;
|
|
|
|
|
right. Extract Method is one of my favorite refactoring tools
I don't use incredibly long names for private methods. I'll abbreviate something like the above to _CheckMessageThread()
Public members i usually go all out, and give it a really long name if it needs one.
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.
|
|
|
|
|
Yours is BS because it will execute everything after the if.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
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.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
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.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
i think it depends on the situation
for example
public int IndexOf(T item)
{
var ic = Count;
var i = 0;
for(;i<ic;++i)
{
if(Equals(item,this[i]))
return i;
}
return -1;
}
That'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=-1
do i++ until Equals(item,this[i])
return i
modified 20-Oct-19 21:02pm.
|
|
|
|
|
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.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
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.
|
|
|
|
|
A purist should be worried about touching control variables. Hmmm.
Jordan
|
|
|
|
|
break is (of course) superfluous - but why not try something that suggests the intention of your loop?
for(int i = 0;i<arr.Length && arr[i]!=valueToFind;i++)
{
}
... or (because I don't like "empty" constructs) ...
int i = 0;
while (i<arr.Length && arr[i]!=valueToFind)
i++;
|
|
|
|
|
I fixed your mistake, you didn't put some space after semi-colons in the for statement.
Book[] arr = books;
for(int i = 0; i<arr.Length; i++)
{
int books_index = i;
Book book = arr[books_index];
if(book.name == find_book) {
return books_index;
}
else if(book.name != find_book) {
continue;
}
else {
found_flag = false;
}
}
|
|
|
|
|
Why not? What is the net value gain by the alternative you show?
|
|
|
|
|
there is none
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.
|
|
|
|
|
Those two code snippets don't do the same thing. The first doesn't change arr, the second does.
|
|
|
|
|
How does it do that?
If it does, it is a bug
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.
|
|
|
|
|
Purist? Baaah!
Think of this in terms of far-Eastern philosophy, to wit, Yin/Yang[^].
Always the spot of yin in the yang portion, the spot of yang in the yin portion. Neither can exist without the other.
So, continue to use break as they help you goto a better place.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Prince Wang's programmer was coding software. His fingers danced upon the keyboard. The program compiled without and error message, and the program ran like a gentle wind.
"Excellent!" the Prince exclaimed. "Your technique is faultless!"
"Technique?" said the programmer, turning from his terminal, "What I follow is Tao -- beyond all techniques! When I first began to program, I would see before me the whole problem in one mass. After three years, I no longer saw this mass. Instead, I used subroutines. But now I see nothing. My whole being exists in a formless void. My senses are idle. My spirit, free to work without a plan, follows its own instinct. In short, my program writes itself. True, sometimes there are difficult problems. I see them coming, I slow down, I watch silently. Then I change a single line of code and the difficulties vanish like puffs of idle smoke. I then compile the program. I sit still and let the joy of the work fill my being. I close my eyes for a moment and then log off."
Prince Wang said, "Would that all of my programmers were as wise!"
- The Tao of Programming
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 neither one of these loops does anything except waste time.
Was there perhaps a reason you wanted to find out if valueToFind was in arr ? If there was, the first loop is almost right, i points to the matching entry on loop exit. Only problem is, i goes out of scope on loop exit. Sigh. The second loop always has i== arr.Length on loop exit, and i still goes out of scope
|
|
|
|
|
the code to do something is supposed to go in the loop body. i omitted it for the example. sorry i wasn't more clear.
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.
|
|
|
|
|
only use Goto if you like spaghetti code
|
|
|
|
|
i use gotos for generated state machine code so the code will look exactly like the graphs generated by graphviz. it makes the code more understandable.
like this snippet, implementing q1 of the graph:
q1:
if((pc.Current>='0'&& pc.Current<='9')||
(pc.Current>='A'&& pc.Current<='Z')||
(pc.Current=='_')||
(pc.Current>='a'&& pc.Current<='z')) {
sb.Append((char)pc.Current);
pc.Advance();
goto q1;
}
return new System.Collections.Generic.KeyValuePair<System.String,string>("id",sb.ToString());
from (A Regular Expression Engine in C#[^])
there's supposed to be a picture at the link but it's no longer showing up for me. maybe it will for you.
in any case, there's a time and a place for everything.
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.
|
|
|
|