473,408 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

A conundrum in C: found this fragment in Steve McConnell's CODECOMPLETE

CODE COMPLETE, page 356

if(StatusOK)
{
if(DataAvail)
{
ImportantVar = x;
goto MID_LOOP;
}
}
else
{
ImportantVar = GetVal( );
MID_LOOP;

/* lots of code */
. . .
}

* * * * * * end fragment * * * * *

The idea here is to rewrite the code without the goto LABLE.

First we identify the Work.
Initialize or re-initialize the variable ImportantVar,
conditionally,
in the if or the else clause.
And,
execute /* lots of code */ unconditionally, no matter what the
conditions are.

Reading the code is not the problem, or even writing code.
The real problem is in divining the intent of the coder.

Let us say the intent (IntentA) is to do the Work under any
conditions.
Then this fragment has a problem.
In the event that SatusOK is true, but DataAvail is false, then the
outer
if will ( since its own test is ture ) execute its code block, the
inner if,
but will fall thru, doing no Work.
The else is the companion clause of the outer if, and since the if
tried
to execute, the else will be skipped.
No Work will be done at all.

Either this is a defect, or we have some other intent (IntentB):
Do the Work under all conditions, except *this one*.

Here is my rewirte for IntentA, a bit simpler than what McConnell
offered.
**********************************
If( StatusOK && DataAvail )
{
ImportantVar = x;
}
else
{
ImportantVar = GetVal( );
}

/* lots of code */ // What is the sense of putting
unconditionally

Apr 2 '08 #1
1 1202
be*******@mail.com writes:
CODE COMPLETE, page 356

if(StatusOK)
{
if(DataAvail)
{
ImportantVar = x;
goto MID_LOOP;
}
}
else
{
ImportantVar = GetVal( );
MID_LOOP;
Presumably this is intended to be MID_LOOP:
/* lots of code */
. . .
}
The sort of "obvious" re-write is just to separate the setting of teh
variable from the execution of the labeled code block. To my mind it
makes thing much clearer. Personally, I'd invert the first test
because it makes the setting simpler:

if (!StatusOK)
ImportantVar = GetVal();
else if (DataAvail)
ImportantVar = x;

if (!StatusOK || DataAvail) {
/* lots of code */
}

Of course, if the tests are complex so that re-evaluating them is not
acceptable then I'd be thinking about adding a new function.

--
Ben.
Apr 3 '08 #2

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

Similar topics

4
by: Stuart Baker | last post by:
I just added session support to a web site and the URL's that contain a #fragment no longer function. Is there a delimiter that I can add following the #fragment that will tell the browser that the...
3
by: Steve Dussinger | last post by:
Hi All: I am attempting to use an XSL stylesheet to take data from an existing DOM document, and place it into a different existing DOM document. The problem I'm having is that I get the...
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
4
by: Jim Owen | last post by:
I am storing all my application data in the application cache. Anytime I have a method as part of an asp.net form, I need to access the objects in the cache. The only way I can think of to do this...
0
by: Iain | last post by:
Can I apologise for the lengthy nature of this post. The scenario is complicated (though I hope the solution is not!) basically, I've got a custom template control which binds itself to a tree...
6
by: RitaG | last post by:
Hello. I have a VB.Net program that copies a file from one server to another server. This program has been working without any problems for about a year. A couple of weeks ago the "server from"...
2
by: subnunciation | last post by:
i know, this shouldnt be a conundrum right? one just shouldnt divide by zero. but this is suddenly happening *all over* my site. after chasing the error here and there, i simplified things down to:...
3
by: Gustaf | last post by:
I'm trying to grasp this little passage from the XBRL spec: "The xlink:href attribute MUST be a URI. The URI MUST point to an XML document or to one or more XML fragments within an XML document....
1
by: Just Me | last post by:
OK, Heres the deal. VS2005, AJAX 1.1, Menu With Child Items Inside UpdatePanel. Why, because Im using ajax and need to dynamically update the enabled state of one of the menu options. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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,...

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.