Nested Statements.

P

Profetas

Hi,

Nested statements helps to generate complex source code,
what is the nesting limit that you normally consider?

I know that the Halsted and McCabe analyses can define the acceptable
level, but in your personnel point of views what is the limit before you
re-code the block into multiple blocks

a 3 level nesting example

If (z is true)
while (x < Y )
begin
if (z==x)
......
else
.......
end if

end
end
else

while (x < Y )
begin
if (z==x)
......
else
.......
end if

end
end

end if


I think that 3 level sometimes 4 is acceptable.
but I was wondering would you restrict it to 3 or 4?

is there anyway to avoid this excluding the creation of new functions?

Thanks
 
M

Malcolm

Profetas said:
Nested statements helps to generate complex source code,
what is the nesting limit that you normally consider?
Ideally about three levels is the maximum that a human can easily read, but
in practise it isn't possible to achieve this
is there anyway to avoid this excluding the creation of new functions?
No always. Even creating a new function is often not the answer. A function
should do something which is discrete and can be summarised in a short
comment. Often complex flow logic is to handle errors or similar conditions
and it is not possible to break it out into a function.
You can use goto. This is a terrible solution when badly used, but
acceptable when used carefully. eg

MYLEVEL *createlevel(char *filename)
{
MYLEVEL *answer = 0;
FILE *fp = fopen("filename", "rb");
if(!fp)
goto error;

answer = malloc(sizeof(MYLEVEL));
if(!answer)
goto error;

answer->cell = 0;
/* etc */

answer->cell = malloc(width * height);
if(!answer->cell)
goto error;

etc etc etc;

return answer;

error:
/*
code to free up a partially-allocated level
*/
return NULL;
}
 
C

CBFalconer

Profetas said:
Nested statements helps to generate complex source code,
what is the nesting limit that you normally consider?

I know that the Halsted and McCabe analyses can define the
acceptable level, but in your personnel point of views what is the
limit before you re-code the block into multiple blocks

a 3 level nesting example

If (z is true)
while (x < Y )
begin
if (z==x)
......
else
.......
end if

end
end
else

while (x < Y )
begin
if (z==x)
......
else
.......
end if

end
end

end if

I think that 3 level sometimes 4 is acceptable.
but I was wondering would you restrict it to 3 or 4?

is there anyway to avoid this excluding the creation of new
functions?

First, take your own example and avoid creating non-existent
levels. The 'begin's (which are '{' in C) are not a level of
control. Then limit the line length to 72 chars or so. You should
also use the rule of 7, which says that a function should deal with
no more than 7 items at once, because the human brain becomes
confused with more.

Rewriting your example into reasonable (IMO) format:

If (z is true) {
while (x < Y) {
if (z == x) ......
else .......
}
}
else {
while (x < Y) {
if (z == x) {
......
}
else {
.......
}
}
}

Which smells (to me) as if misconstructed. I suspect the .....
areas should have been parametized in some form, so that the
overall code should be simplified to:

if (z) operateon(x, y, zz);
else operateon(z, y, z);
 
P

Peter Pichler

Profetas said:
Nested statements helps to generate complex source code,
what is the nesting limit that you normally consider?

Infinity :) Seriously, my limit would be about 20, i.e. when the indentation
reaches the middle of the screen.
a 3 level nesting example

If (z is true)
while (x < Y )
begin
if (z==x)
......
else
.......
end if
end
end
else

What has the above to do with C?
I think that 3 level sometimes 4 is acceptable.
but I was wondering would you restrict it to 3 or 4?
No.

is there anyway to avoid this excluding the creation of new functions?

Yes, there is. But why? (One way could be using a lot of gotos. But then
again, why?)

Peter
 
M

Mark McIntyre


I'd suggest 127 would be a safer limit...
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at least one
program that contains at least one instance of every one of the following
limits:
- 127 nesting levels of blocks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top