Nesting level: 9001
-
This is a good example of even when there are a lot of nesting levels, the code can be formatted so that it is readable and understandable. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
-
Found this in code I wrote a long time ago. Not the stupidest code ever perhaps, but it's just, well, see for yourself.
size = 29;
if (width > 11 || height > 4)
{
size++;
if (width > 13 || height > 5)
{
size++;
if (width > 15 || height > 6)
{
size++;
if (width > 18 || height > 7)
{
size++;
if (width > 19 || height > 8)
{
size++;
if (width > 21 || height > 10)
{
// 7x6 compressed
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}
else
{
// 7x6
Spread(width, 8, 7, xpos, ypos);
}
}
else
{
// 8x8
Spread(width, 9, 9, xpos, ypos);
}
}
else
{
// 8x9
Spread(width, 9, 10, xpos, ypos);
}
}
else
{
// 10x11
Spread(width, 11, 12, xpos, ypos);
}
}
else
{
// 12x13
Spread(width, 13, 14, xpos, ypos);
}
}
else
{
// 14x15
Spread(width, 15, 16, xpos, ypos);
}So what's the alternative?
-
So what's the alternative?
Well I could have done this:
if (width <= 11 && height <= 4)
{
size = 29;
Spread(width, 15, 16, xpos, ypos);
}
else if (width <= 13 && height <= 5)
{
size = 30;
Spread(width, 13, 14, xpos, ypos);
}
else if (width <= 15 && height <= 6)
{
size = 31;
Spread(width, 11, 12, xpos, ypos);
}
else if (width <= 18 && height <= 7)
{
size = 32;
Spread(width, 9, 10, xpos, ypos);
}
else if (width <= 19 && height <= 8)
{
size = 33;
Spread(width, 9, 9, xpos, ypos);
}
else if (width <= 21 && height <= 10)
{
size = 34;
Spread(width, 8, 7, xpos, ypos);
}
else
{
size = 34;
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}Not great either, but still better I would say. At least the width and height requirements are actually grouped together with what should happen if they're met, instead of in a weird inverted way moved as far apart as possible.
-
Found this in code I wrote a long time ago. Not the stupidest code ever perhaps, but it's just, well, see for yourself.
size = 29;
if (width > 11 || height > 4)
{
size++;
if (width > 13 || height > 5)
{
size++;
if (width > 15 || height > 6)
{
size++;
if (width > 18 || height > 7)
{
size++;
if (width > 19 || height > 8)
{
size++;
if (width > 21 || height > 10)
{
// 7x6 compressed
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}
else
{
// 7x6
Spread(width, 8, 7, xpos, ypos);
}
}
else
{
// 8x8
Spread(width, 9, 9, xpos, ypos);
}
}
else
{
// 8x9
Spread(width, 9, 10, xpos, ypos);
}
}
else
{
// 10x11
Spread(width, 11, 12, xpos, ypos);
}
}
else
{
// 12x13
Spread(width, 13, 14, xpos, ypos);
}
}
else
{
// 14x15
Spread(width, 15, 16, xpos, ypos);
}It's over 9,000!!!!!! I assume you have been waiting for that. ;)
-
Found this in code I wrote a long time ago. Not the stupidest code ever perhaps, but it's just, well, see for yourself.
size = 29;
if (width > 11 || height > 4)
{
size++;
if (width > 13 || height > 5)
{
size++;
if (width > 15 || height > 6)
{
size++;
if (width > 18 || height > 7)
{
size++;
if (width > 19 || height > 8)
{
size++;
if (width > 21 || height > 10)
{
// 7x6 compressed
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}
else
{
// 7x6
Spread(width, 8, 7, xpos, ypos);
}
}
else
{
// 8x8
Spread(width, 9, 9, xpos, ypos);
}
}
else
{
// 8x9
Spread(width, 9, 10, xpos, ypos);
}
}
else
{
// 10x11
Spread(width, 11, 12, xpos, ypos);
}
}
else
{
// 12x13
Spread(width, 13, 14, xpos, ypos);
}
}
else
{
// 14x15
Spread(width, 15, 16, xpos, ypos);
}harold aptroot wrote:
code I wrote a long time ago
You dare admit that here? ;p Although I must admit I've written something like five nested if's just today... :-O
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
} -
Well I could have done this:
if (width <= 11 && height <= 4)
{
size = 29;
Spread(width, 15, 16, xpos, ypos);
}
else if (width <= 13 && height <= 5)
{
size = 30;
Spread(width, 13, 14, xpos, ypos);
}
else if (width <= 15 && height <= 6)
{
size = 31;
Spread(width, 11, 12, xpos, ypos);
}
else if (width <= 18 && height <= 7)
{
size = 32;
Spread(width, 9, 10, xpos, ypos);
}
else if (width <= 19 && height <= 8)
{
size = 33;
Spread(width, 9, 9, xpos, ypos);
}
else if (width <= 21 && height <= 10)
{
size = 34;
Spread(width, 8, 7, xpos, ypos);
}
else
{
size = 34;
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}Not great either, but still better I would say. At least the width and height requirements are actually grouped together with what should happen if they're met, instead of in a weird inverted way moved as far apart as possible.
Perhaps a map with a key class for width and height, and a value class for the passed parameters. Use std::map::upper_bound() to find the element you're looking for.... then it'll be dynamic with no hard coding...
-
Found this in code I wrote a long time ago. Not the stupidest code ever perhaps, but it's just, well, see for yourself.
size = 29;
if (width > 11 || height > 4)
{
size++;
if (width > 13 || height > 5)
{
size++;
if (width > 15 || height > 6)
{
size++;
if (width > 18 || height > 7)
{
size++;
if (width > 19 || height > 8)
{
size++;
if (width > 21 || height > 10)
{
// 7x6 compressed
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}
else
{
// 7x6
Spread(width, 8, 7, xpos, ypos);
}
}
else
{
// 8x8
Spread(width, 9, 9, xpos, ypos);
}
}
else
{
// 8x9
Spread(width, 9, 10, xpos, ypos);
}
}
else
{
// 10x11
Spread(width, 11, 12, xpos, ypos);
}
}
else
{
// 12x13
Spread(width, 13, 14, xpos, ypos);
}
}
else
{
// 14x15
Spread(width, 15, 16, xpos, ypos);
}How about something along the lines of:
size = 29;
if(width > 11 || height > 4) size++;
if(width > 13 || height > 5) size++;
if(width > 15 || height > 6) size++;
if(width > 18 || height > 7) size++;
if(width > 19 || height > 8) size++;
if(width > 21 || height > 10) size++;switch(size) {
case 29: Spread(width, 15, 16, xpos, ypos); break;
case 30: Spread(width, 13, 14, xpos, ypos); break;
case 31: Spread(width, 11, 12, xpos, ypos); break;
case 32: Spread(width, 9, 10, xpos, ypos); break;
case 33: Spread(width, 9, 9, xpos, ypos); break;
case 34: Spread(width, 8, 7, xpos, ypos); break;
case 35: SpreadCompressed(width, width > 21, height > 10, xpos, ypos); break;
} -
How about something along the lines of:
size = 29;
if(width > 11 || height > 4) size++;
if(width > 13 || height > 5) size++;
if(width > 15 || height > 6) size++;
if(width > 18 || height > 7) size++;
if(width > 19 || height > 8) size++;
if(width > 21 || height > 10) size++;switch(size) {
case 29: Spread(width, 15, 16, xpos, ypos); break;
case 30: Spread(width, 13, 14, xpos, ypos); break;
case 31: Spread(width, 11, 12, xpos, ypos); break;
case 32: Spread(width, 9, 10, xpos, ypos); break;
case 33: Spread(width, 9, 9, xpos, ypos); break;
case 34: Spread(width, 8, 7, xpos, ypos); break;
case 35: SpreadCompressed(width, width > 21, height > 10, xpos, ypos); break;
} -
Found this in code I wrote a long time ago. Not the stupidest code ever perhaps, but it's just, well, see for yourself.
size = 29;
if (width > 11 || height > 4)
{
size++;
if (width > 13 || height > 5)
{
size++;
if (width > 15 || height > 6)
{
size++;
if (width > 18 || height > 7)
{
size++;
if (width > 19 || height > 8)
{
size++;
if (width > 21 || height > 10)
{
// 7x6 compressed
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
}
else
{
// 7x6
Spread(width, 8, 7, xpos, ypos);
}
}
else
{
// 8x8
Spread(width, 9, 9, xpos, ypos);
}
}
else
{
// 8x9
Spread(width, 9, 10, xpos, ypos);
}
}
else
{
// 10x11
Spread(width, 11, 12, xpos, ypos);
}
}
else
{
// 12x13
Spread(width, 13, 14, xpos, ypos);
}
}
else
{
// 14x15
Spread(width, 15, 16, xpos, ypos);
}Is size used elsewhere? I would have probably calculated a widthSize and a heightSize separately and then size = max(widthSize, heightSize) This approach basically replaces six || operators with one compare in the max() function. You could have used an array, switch statement, collection or even if statements for calculating/looking up widthSize and heightSize. switch would probably be most readable (and fastest for these datapoints).
-
Then I would amend it to:
size = 29;
if(width > 11 || height > 4) size++;
if(width > 13 || height > 5) size++;
if(width > 15 || height > 6) size++;
if(width > 18 || height > 7) size++;
if(width > 19 || height > 8) size++;
if(width > 21 || height > 10) size++;switch(size) {
case 29: Spread(width, 15, 16, xpos, ypos); break;
case 30: Spread(width, 13, 14, xpos, ypos); break;
case 31: Spread(width, 11, 12, xpos, ypos); break;
case 32: Spread(width, 9, 10, xpos, ypos); break;
case 33: Spread(width, 9, 9, xpos, ypos); break;
case 34: Spread(width, 8, 7, xpos, ypos); break;
case 35:
SpreadCompressed(width, width > 21, height > 10, xpos, ypos);
size--; //back to 34 for you
break;