Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
Your thoughts? 13 1381
On Nov 15, 8:26 pm, William Pursell <bill.purs...@gmail.comwrote:
Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
Your thoughts?
I think it is a mistake to code so that the debugger is happy.
Write it the way that is the most clear. If alternatives are
absolutely equal in your eyes, then choose the one you like (including
the one that makes the debugger happy).
William Pursell wrote, On 16/11/07 04:26:
Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
Your thoughts?
Firstly, I believe in making the code debugger friendly, but only where
it does not make it human unfriendly.
I agree that it is ugly. If it is not function calls you can easily
check the condition of all the variables and sub-expression so you can
see why it has taken or not taken the branch. If b was a function call
there would be more of an argument for separating it out, but not
necessarily conclusive.
Considering all that, I would use the first form.
--
Flash Gordon
Ugly.
while(x && y);
would become
if(x)
while(y)
if(!x)
break;
William Pursell wrote:
Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
If the sub-conditions are really ``a'' and ``b'' (i.e., simple references to
variables), then it doesn't make any difference. If they're, say, function
calls then you can assign them to temporaries.
For example, rather than this:
if (func1() && func2()) ...
you might write this:
int cond1 = func1();
int cond2 = func2();
if (cond1 && cond2) ...
This might be even more readable than the original if you choose sufficiently
descriptive names for the temporaries.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
news:fh**********@aioe.org...
William Pursell wrote:
>Lots of discussion lately on coding so that the debugger stops on each line, and this is yet another. In that debate, I fall in the camp of those who like code written such that every statement is on a separate line (although I can't stop using :?). I am curious to get opinions on the following style:
instead of: if( a && b ) foo();
use: if( a ) if( b ) foo();
This style puts the conditionals on separate lines, so it has that in its favor. But me thinks it has very few other redeeming features and is a bit ugly.
If the sub-conditions are really ``a'' and ``b'' (i.e., simple references
to variables), then it doesn't make any difference. If they're, say,
function calls then you can assign them to temporaries.
For example, rather than this:
if (func1() && func2()) ...
you might write this:
int cond1 = func1();
int cond2 = func2();
if (cond1 && cond2) ...
This might be even more readable than the original if you choose
sufficiently descriptive names for the temporaries.
But is is not the same. In the 1st case func2() may or may not be called,
depeding on the restult of func1(), in the 2nd it will always be called.
Bye, Jojo
user923005 <dc*****@connx.comwrites:
On Nov 15, 8:26 pm, William Pursell <bill.purs...@gmail.comwrote:
>Lots of discussion lately on coding so that the debugger stops on each line, and this is yet another. In that debate, I fall in the camp of those who like code written such that every statement is on a separate line (although I can't stop using :?). I am curious to get opinions on the following style:
instead of: if( a && b ) foo();
use: if( a ) if( b ) foo();
This style puts the conditionals on separate lines, so it has that in its favor. But me thinks it has very few other redeeming features and is a bit ugly.
Your thoughts?
I think it is a mistake to code so that the debugger is happy.
Debugger happy is normally reader happy too.
>
Write it the way that is the most clear. If alternatives are
absolutely equal in your eyes, then choose the one you like (including
the one that makes the debugger happy).
Assuming a and b were expressions I would have at first glance:
aFlag = a();
bFlag = b();
if (aFlag && bFlag)
foo();
We can set watch/break points on conditions for aFlag and bFlag (e.g
only break if aFLag is true and bFlag is false.
But at second glance we realise that (a && b) might be too clever for our
own good. C will not evaluate b() if a() evaluates to false ....
So as the writer of the ORIGINAL code I would have, to make it explicit
what I meant
if(a())
if(b()) /* optimized so as not to call b() if a() fails */
foo();
This is also debugger friendly as we can set a break on the second if().
We could do the intermediate flag trick if we wanted to break on a()
failing(returning false). We can already set a break on foo() when both
conditions are passed.
It is always worth considering debugger/reader friendly code. People
will thank you in the long run.
William Pursell wrote:
>
Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
Your thoughts?
Consider, too, the implitations of an else in the above. How
would you handle converting:
if ( a && b )
foo();
else
bar();
to your "debugger friendly" version?
And, I assume you don't really mean "a separate line", because
that would , to me, mean something like this:
if ( a
&& b
)
foo();
Though I have to admit I use such things when the condition gets
complex.
if ( ( some_long_condition
||
another_long_condition
)
&&
yet_another_condition
)
{
do_something;
}
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Keith Thompson wrote:
[...]
For example, rather than this:
if (func1() && func2()) ...
you might write this:
int cond1 = func1();
int cond2 = func2();
if (cond1 && cond2) ...
This might be even more readable than the original if you choose sufficiently
descriptive names for the temporaries.
This isn't the same. If func1() returns zero, func2() wouldn't be
called in the original case.
Consider:
if ( pointer != NULL && *pointer != 0 )
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Richard wrote, On 16/11/07 13:39:
user923005 <dc*****@connx.comwrites:
>On Nov 15, 8:26 pm, William Pursell <bill.purs...@gmail.comwrote:
>>Lots of discussion lately on coding so that the debugger stops on each line, and this is yet another. In that debate, I fall in the camp of those who like code written such that every statement is on a separate line (although I can't stop using :?). I am curious to get opinions on the following style:
instead of: if( a && b ) foo();
use: if( a ) if( b ) foo();
This style puts the conditionals on separate lines, so it has that in its favor. But me thinks it has very few other redeeming features and is a bit ugly.
Your thoughts?
I think it is a mistake to code so that the debugger is happy.
Debugger happy is normally reader happy too.
Normally, possibly, but definitely not always.
>Write it the way that is the most clear. If alternatives are absolutely equal in your eyes, then choose the one you like (including the one that makes the debugger happy).
Assuming a and b were expressions I would have at first glance:
aFlag = a();
bFlag = b();
if (aFlag && bFlag)
foo();
Which behaves differently.
We can set watch/break points on conditions for aFlag and bFlag (e.g
only break if aFLag is true and bFlag is false.
If I needed to do that in debugging I might well make it
if (aflag=a() && bflag=b())
foo();
But at second glance we realise that (a && b) might be too clever for our
own good. C will not evaluate b() if a() evaluates to false ....
It does not take me more than one glance. Also as a lot of C code (and
code in other languages) relies on the short-circuit evaluation I would
say that anyone not comfortable with it is not suitable for work on most
C code.
So as the writer of the ORIGINAL code I would have, to make it explicit
what I meant
if(a())
if(b()) /* optimized so as not to call b() if a() fails */
foo();
This would take me longer to read.
This is also debugger friendly as we can set a break on the second if().
We could do the intermediate flag trick if we wanted to break on a()
failing(returning false). We can already set a break on foo() when both
conditions are passed.
See comment above about using flags with the original format.
It is always worth considering debugger/reader friendly code. People
will thank you in the long run.
Debugger and reader friendly are not always the same, and I find your
alternatives to the original less readable. Someone without much
experience might find your alternatives more readable, but in my opinion
they need to be comfortable with the original to be able to maintain C
code anyway.
--
Flash Gordon
Joachim Schmitz wrote:
"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
news:fh**********@aioe.org...
>William Pursell wrote:
[...]
>>instead of: if( a && b ) foo();
use: if( a ) if( b ) foo();
This style puts the conditionals on separate lines, so it has that in its favor. But me thinks it has very few other redeeming features and is a bit ugly.
If the sub-conditions are really ``a'' and ``b'' (i.e., simple references to variables), then it doesn't make any difference. If they're, say, function calls then you can assign them to temporaries.
For example, rather than this:
if (func1() && func2()) ...
you might write this:
int cond1 = func1(); int cond2 = func2(); if (cond1 && cond2) ...
This might be even more readable than the original if you choose sufficiently descriptive names for the temporaries.
But is is not the same. In the 1st case func2() may or may not be called,
depeding on the restult of func1(), in the 2nd it will always be called.
Whoops, you're right of course (as is Kenneth Brody who also caught my
mistake).
How about this:
int cond1 = func1();
int cond2 = cond1 && func2();
if (cond2) ...
Again, you really need meaningful names for func{1,2} and cond{1,2}.
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Flash Gordon <sp**@flash-gordon.me.ukwrites:
<snip>
If I needed to do that in debugging I might well make it
if (aflag=a() && bflag=b())
foo();
nit-pick: if ((aflag = a()) && (bflag = b()))
--
Ben.
Flash Gordon wrote:
Richard wrote, On 16/11/07 13:39:
.... snip ...
>
If I needed to do that in debugging I might well make it
if (aflag=a() && bflag=b())
foo();
>But at second glance we realise that (a && b) might be too clever for our own good. C will not evaluate b() if a() evaluates to false ....
It does not take me more than one glance. Also as a lot of C code
(and code in other languages) relies on the short-circuit
evaluation I would say that anyone not comfortable with it is not
suitable for work on most C code.
>So as the writer of the ORIGINAL code I would have, to make it explicit what I meant
if(a()) if(b()) /* optimized so as not to call b() if a() fails */ foo();
To duplicate the action of && you need:
if (c = a()) c = b();
if (c) foo();
which you can complicate into aflag and bflag, replacing the final
c with a computation.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
"William Pursell" <bi**********@gmail.coma écrit dans le message de news: 04**********************************...oglegroups.com...
Lots of discussion lately on coding so that the debugger
stops on each line, and this is yet another. In that debate,
I fall in the camp of those who like code written such that
every statement is on a separate line (although I can't
stop using :?). I am curious to get opinions on the following
style:
instead of:
if( a && b )
foo();
use:
if( a )
if( b )
foo();
This style puts the conditionals on separate lines, so it
has that in its favor. But me thinks it has very few other
redeeming features and is a bit ugly.
Your thoughts?
If you are going to do this for debugging purposes, at least use proper
indentation and bracing to avoid stupid errors:
if (a) {
if (b) {
foo();
}
}
--
Chqrlie. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: wackyphill |
last post by:
I'm constructing a menu in a SQL Server database.
Each menu can have sub menus. So my table looks like this:
CREATE TABLE menu
(
id INT NOT NULL IDENTITY PRIMARY KEY,
name VARCHAR(30) NOT...
|
by: manning_news |
last post by:
Using SQL 7. I have a table with 2 fields in it that I wish to relate
to a lookup table. DDL for table 1:
CREATE TABLE . (
(10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
NULL ,
...
|
by: JMosey |
last post by:
Not sure if this has been covered ( a google search came up pretty
bare).
I have a site that:
- has multi-level cascading menus
- floats center of the browser window
- Will have fairly heavy...
|
by: (Pete Cresswell) |
last post by:
Seems like when there's a 1:1 relationship, the order of referential integrity
enforcement depends on which way you drag the mouse pointer when drawing the
relationship line.
If you drag from...
|
by: John |
last post by:
I have a make-table query that creates a table that shows various
attributes of other tables---the table created by the query is used
for Excel pivot-table linking.
The problem is that whenever...
|
by: Glenn Lerner |
last post by:
When I used Visual Studio 6 I was able to cascade source code windows.
This allowed me to view 2 separate source code files side by side and
copy and paste between windows.
How do I do this in...
|
by: Don Miller |
last post by:
I have one databound DDL (Categories) in a web form and when an item is
selected (SelectedIndexChanged) I want to populate a second DDL
(SubCategories). It was fairly easy to get this to work out...
|
by: Angus |
last post by:
I have had a look at
http://javascript.internet.com/navigation/cascade-menu.html
Which is roughly what I want to do.
Roughly the menu looks like this:
Menu #1 Menu #2 Menu #3 Menu #4
|
by: petwir |
last post by:
We've got a cascading delete being used in DB2. I've built new triggers under the parent/child tables in this cascading delete relationship.
So when they delete the parent, and the cascade...
|
by: datsandgirl |
last post by:
Okay, I'm sorry, but I lied. I need to ask one more question regarding my database. This has to do with the three cascading combo boxes mentioned in my original question. I mimicked my way through...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |