Nested loops
-
I would tend to refactor the loops into smaller methods so that I can follow it easier:
public void IterateOverRoadNetwork(RoadSegments[] segments)
{
foreach (RoadSegment segment in segments)
{
CheckNetworkSpeeds(segment);
}
}
public void CheckNetworkSpeeds(RoadSegment segment)
{
foreach (Vehicle vehicle in segment.Vehicles)
{
CheckForImpossibleRoute(vehicle);
}
}
public void CheckForImpossibleRoute(Vehicle vehicle )
{
foreach (VehicleRestriction restriction in vehicle.Restrictions)
{
//
}
}By doing this, I can name methods for their intent, so I can see what they are trying to do. That's my preferred option.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
If that's in your satnav you must have to drive really slowly for all those nested function calls to complete before you arrive at wherever you wanted it to direct you to.
-
What if you want to iterate over a 3D space plotting each x,y,z in a different colour?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
a) good point. b) never had to do anything like that so don't really care. c) Two: anything else is lunacy and must be stamped out: 2 dimensions is more than enough for anybody!
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
-
a) good point. b) never had to do anything like that so don't really care. c) Two: anything else is lunacy and must be stamped out: 2 dimensions is more than enough for anybody!
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
OK, then, stay where you are. I'll be bringing my steamroller over. Please lay on the ground. Afterwards, you will be two-dimensional.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}Depends
thatraja
FREE Code Conversion VB6 ASP VB.NET C# ASP.NET C++ JAVA PHP DELPHI ColdFusion
HTML Marquee & its alternativesNobody remains a virgin, Life screws everyone :sigh:
-
If that's in your satnav you must have to drive really slowly for all those nested function calls to complete before you arrive at wherever you wanted it to direct you to.
Or, it could be the routine that precalculates all the routes and filters out the impossible to pass roads for different vehicle sizes and types, when you load a road network into a database.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Depends
thatraja
FREE Code Conversion VB6 ASP VB.NET C# ASP.NET C++ JAVA PHP DELPHI ColdFusion
HTML Marquee & its alternativesNobody remains a virgin, Life screws everyone :sigh:
Do you use them? I feel sorry for you... ;P ;P
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
a) good point. b) never had to do anything like that so don't really care. c) Two: anything else is lunacy and must be stamped out: 2 dimensions is more than enough for anybody!
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me
I once worked on a system written in COBOL, an we had to loop through a madly dimensional array - what would today be a collection of collections:
for each country
for each sector
for each segment
for each stock
for each date
do the stuff
loop
loop
loop
loop
loopAll in one lovely single flat COBOL program. Actually, IIRC it was repeated, sometimes different ways round in lots of programs. I don't do COBOL no more.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}Use as many as the algorithm requires. If you need to examine every single point in a three dimensional space, then trying to do it without nesting three deep is messy, inefficient, and wastefull.
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
for (int z = 0; z < 100; z++)
{
...
}
}
}Worse, moving the nesting to methods to "make it look tidier" may well hide the amount of processing that is going on since it is no longer obvious that the single instruction in the middle of the centre loop is being executed 100 * 100 * 100 times, not just 100 times... Having a blanket rule "This is messy!" is wrong, and counter productive.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}Shameel wrote:
I would like to ask what is the level of nested loops that is acceptable in general?
Zero! What's Wrong with the For Loop[^]
-
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}I never go further than 26 levels, because then I've run out of variables to control the loops with. I could of course start using aa, ab, ac...but that gets confusing.
-
I never go further than 26 levels, because then I've run out of variables to control the loops with. I could of course start using aa, ab, ac...but that gets confusing.
I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}Add, as comments to the end of the loops, the containing logic and you can have as many nested as required...
for(int i=0; i<100; i++) {
for(int j=0; j<100; j++) { for(int k=0; k<100; k++) { }//int k=0; k<100 }//int j=0; j<100
}//int i=0; i<100
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von BraunProblem with that is the indentation gets too large and you keep having to scroll to the right.
-
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}As many as required, but no more. Limiting the number of nested loops is like prescribing a length for variable names: "Variable names must be at least six characters and no more than 31 characters in length, must begin with an upper case alphabetic character, may not include an underscore, and must consist of one or more complete English words, signified through use of upper case characters at the beginning of each word". Picking names will be like playing Scrabble...
Software Zen:
delete this;
-
I like to use Z1, Z2, Z3, etc... Then I can have hundreds and thousands of nested loops
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von BraunMmmmmm....Hundreds and Thousands[^]
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Problem with that is the indentation gets too large and you keep having to scroll to the right.
Buy a wider monitor.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Mmmmmm....Hundreds and Thousands[^]
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
mmmmmmmmmmmm, doughnut :-D
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
Follow up on the Variable names thread below, I would like to ask what is the level of nested loops that is acceptable in general? Most people would agree that three levels is acceptable, but I would say stop at two. The third loops gets a bit messy.
for(int i=0; i<100; i++) { //Okay
for(int j=0; j<100; j++) { //Acceptable
for(int k=0; k<100; k++) { //Messy
}
}
}Is this from a wikipedia study:
Shameel wrote:
Most people would agree that three levels is acceptable
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
What if you want to iterate over a 3D space plotting each x,y,z in a different colour?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?
Tom Clement Serena Software, Inc. www.serena.com articles[^]
-
Ok, and what if you want to iterate over points in Hilbert space[^]? Eh?
Tom Clement Serena Software, Inc. www.serena.com articles[^]
You would get HWally to intercept the HPointy Aired HBoss first.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility