473,511 Members | 16,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested Statements.

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

Nov 14 '05 #1
4 1979

"Profetas" <xu*****@yahoo.com> wrote

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;
}
Nov 14 '05 #2
Profetas wrote:

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);

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3
"Profetas" <xu*****@yahoo.com> wrote:
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
Nov 14 '05 #4
On Sat, 6 Nov 2004 23:09:04 -0000, in comp.lang.c , "Peter Pichler"
<pi*****@pobox.sk> wrote:
"Profetas" <xu*****@yahoo.com> wrote:
Nested statements helps to generate complex source code,
what is the nesting limit that you normally consider?


Infinity :)


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
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6425
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
4288
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
10
3187
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
5
1320
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
1
2441
by: redpayne | last post by:
Ok-I am doing homework out of a book and the instructions are to display an interface with 5 option buttons in a frame. When clicked, each button changes the background color of the frame. It...
1
3196
by: sql_er | last post by:
Hi all, I am trying to convert an SQL statement into an XPath (or a sequence of XPath) statements. More specifically, I have the following: SELECT a FROM b WHERE c IN (SELECT d FROM e) I...
2
7210
by: pradeep.thekkottil | last post by:
I'm setting up an auction website using PHP and MySQL. There in the section where logged in members can put up new auction in a form, I want to run a form validation where I used if else statements...
1
1378
by: RAJ | last post by:
hi there, can anybody suggest me best sites for learning php with nested sql statements. i need sql query for searching from database with lots of options like using AND, OR etc even using...
0
1267
by: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on...
4
2651
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.