473,657 Members | 2,609 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 1990

"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(ch ar *filename)
{
MYLEVEL *answer = 0;
FILE *fp = fopen("filename ", "rb");
if(!fp)
goto error;

answer = malloc(sizeof(M YLEVEL));
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********@yah oo.com) (cb********@wor ldnet.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
6449
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 COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
2
4302
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
10
3220
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 complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
5
1325
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 Class(Steps) with a nested Class(Questions) inside of it and a Collection of that Class(Steps) and it works fine, but the Nested Collection(Questions) is escaping me. Help!
1
2448
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 proceeds to tell me to construct CheckboxGroup, use FlowLayaout and add the Checkboxes to the frame along with ItemListener. It says addWindowListener()method, write code for itemStateChanged() which uses the getState() method and nested if statements....
1
3209
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 recently learned about XPath and know how to perform simple filtering
2
7228
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 to check the fileds filled. In the form page there are two radio buttons - fixed and auction - (only one can be chosen) and depend upon which one is chosen some text should be inserted in the text fields. For that I'm using a validation where...
1
1383
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 multiple tables thanks, raj
0
1283
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 subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which if clause a following else clause would belong: if test1: if test2: print x
4
2659
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 used Case statements instead. I've read several different examples of Case statements, but can't quite figure out the syntax, or if they can be used when you need to test for a pair of conditions, as I am doing below.
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4158
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.