473,386 Members | 2,129 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,386 software developers and data experts.

Makeup for if-statements?

I have made this:

if (parent[x] == &N::sentinel) {
/* Update root. */
parent[(*this).header] = y;
} else {
if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
}

but I don't really like it. Any suggestions for making it look better
and more clear?
May 29 '07 #1
12 1429
desktop wrote:
I have made this:

if (parent[x] == &N::sentinel) {
/* Update root. */
parent[(*this).header] = y;
} else {
if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
}

but I don't really like it. Any suggestions for making it look better
and more clear?
It is pretty and clear. No need to change it.
May 29 '07 #2
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<ff*@sss.comwrote,
>but I don't really like it. Any suggestions for making it look better
and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.

if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.

} else if (x == left[parent[x]]) {
left[parent[x]] = y;

} else {
right[parent[x]] = y;
}

May 29 '07 #3
On May 29, 11:28 am, desktop <f...@sss.comwrote:
I have made this:

if (parent[x] == &N::sentinel) {
/* Update root. */
parent[(*this).header] = y;
} else {
if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
}

but I don't really like it. Any suggestions for making it look better
and more clear?
There's nothing wrong with this. It's got the
K&R style of brackets, and my personal bias is
to line brackets up vertically. So I do it so:

if(parent[x] == &N::sentinel)
{
// if stuff here
}
else
{
// else case stuff here
}

And so on. But this is just a personal bias.
It's not something I'd suggest changing for
existing code that works.

Unless you have some mods to make, I'd suggest
leaving it alone. Working code shouldn't be
touched for "elegance" reasons. If it's doing
what it is supposed to do, and satisfying the
requirements of performance etc., don't risk
adding bugs.

Remember, every bug in your code was put there
by you.

If you have changes to make, where you go depends
on what you need to do. For example, if you were
to need to add a lot of new logic cases, you might
consider something like a table driven system.
Or if the processing for each case got much more
complicated you might consider putting in a function
to do the processing and keep the logic decisions
as simple and clear as reasonably possible. Or you
might consider that you need a state engine of some
kind, and maybe a class to handle it.
Socks

May 29 '07 #4
"David Harmon" <so****@netcom.comwrote in message
news:46**************@news.west.earthlink.net...
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<ff*@sss.comwrote,
>>but I don't really like it. Any suggestions for making it look better
and more clear?

First step, "chain" the else-if and get rid of the extra level of
brackets.

if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.

} else if (x == left[parent[x]]) {
left[parent[x]] = y;

} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs entirely, although
opionions vary on this.

if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
May 30 '07 #5
On May 30, 9:29 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"David Harmon" <sou...@netcom.comwrote in message
news:46**************@news.west.earthlink.net...
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<f...@sss.comwrote,
>but I don't really like it. Any suggestions for making it look better
and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.
if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.
} else if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs
entirely, although opionions vary on this.
if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
I think it depends somewhat on the bracketing style. Something
like:

if (parent[x] == &N::sentinel)
{
parent[(*this).header] = y; // Update root.
}
else if (x == left[parent[x]])
{
left[parent[x]] = y;
}
else
{
right[parent[x]] = y;
}

is pretty heavy. The post you were responding to, however,
treats the brackets more or less as part of the spelling of the
keywords, or at least as part of the syntax of if/else. Much
the way Modula or Ada does (except that they aren't brackets in
Modula or Ada).

My own preference is for the first style, but I'll use whatever
the company which pays me says to use. If the company style
normally puts both the opening and closing brackets on a line by
themselves, I have no compunction about dropping them if there
is only a single controlled statement. If it doesn't, and uses
something like the first example, I won't drop them, ever,
because the fact that they aren't there is far less visible.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 30 '07 #6
Jim Langston wrote:
"David Harmon" <so****@netcom.comwrote in message
news:46**************@news.west.earthlink.net...
>On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<ff*@sss.comwrote,
>>but I don't really like it. Any suggestions for making it look
better and more clear?

First step, "chain" the else-if and get rid of the extra level of
brackets.

if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.

} else if (x == left[parent[x]]) {
left[parent[x]] = y;

} else {
right[parent[x]] = y;
}

This is one instance I would even get rid of the backetrs entirely,
although opionions vary on this.

if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
(parent[x] == &N::sentinel ?
parent[this->header]
: x == left[parent[x]] ? left[parent[x]]
: right[parent[x]] ) = y;

:-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '07 #7
Victor Bazarov wrote:
Jim Langston wrote:
>"David Harmon" <so****@netcom.comwrote in message
news:46**************@news.west.earthlink.net.. .
>>On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<ff*@sss.comwrote,
but I don't really like it. Any suggestions for making it look
better and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.

if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.

} else if (x == left[parent[x]]) {
left[parent[x]] = y;

} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs entirely,
although opionions vary on this.

if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;

(parent[x] == &N::sentinel ?
parent[this->header]
: x == left[parent[x]] ? left[parent[x]]
: right[parent[x]] ) = y;

:-)
why not
(parent[x] == &N::sentinel && (parent[(*this).header] = y, true)) || (x
== left[parent[x]] && (left[parent[x]] = y, true)) || (right[parent[x]]
= y, true);

:)

Regards,

Zeppe
May 30 '07 #8
desktop wrote:
I have made this:

if (parent[x] == &N::sentinel) {
/* Update root. */
parent[(*this).header] = y;
} else {
if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
}

but I don't really like it. Any suggestions for making it look better
and more clear?
This is the best you can do in C++ but I should note that you have manually
compiled a pattern match. You would get much better results (shorter,
faster code) if you wrote the pattern match in its original form and let a
compiler do the work for you, creating all of the nested ifs.

http://www.ffconsultancy.com/ocaml/b..._matching.html

--
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/product...ntists/?usenet
May 30 '07 #9
"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@m36g2000hse.googlegro ups.com...
On May 30, 9:29 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"David Harmon" <sou...@netcom.comwrote in message
news:46**************@news.west.earthlink.net...
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<f...@sss.comwrote,
>but I don't really like it. Any suggestions for making it look better
and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.
if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.
} else if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs
entirely, although opionions vary on this.
if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
I think it depends somewhat on the bracketing style. Something
like:

if (parent[x] == &N::sentinel)
{
parent[(*this).header] = y; // Update root.
}
else if (x == left[parent[x]])
{
left[parent[x]] = y;
}
else
{
right[parent[x]] = y;
}

is pretty heavy. The post you were responding to, however,
treats the brackets more or less as part of the spelling of the
keywords, or at least as part of the syntax of if/else. Much
the way Modula or Ada does (except that they aren't brackets in
Modula or Ada).

My own preference is for the first style, but I'll use whatever
the company which pays me says to use. If the company style
normally puts both the opening and closing brackets on a line by
themselves, I have no compunction about dropping them if there
is only a single controlled statement. If it doesn't, and uses
something like the first example, I won't drop them, ever,
because the fact that they aren't there is far less visible.

------------------

Ahh, you caught me. Yes, I use a bracket on a line by itself asy our
example showed, which is why I sometimes drop them for single statements.
May 31 '07 #10
On May 30, 2:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jim Langston wrote:
"David Harmon" <sou...@netcom.comwrote in message
news:46**************@news.west.earthlink.net...
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<f...@sss.comwrote,
but I don't really like it. Any suggestions for making it look
better and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.
if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.
} else if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs entirely,
although opionions vary on this.
if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
(parent[x] == &N::sentinel ?
parent[this->header]
: x == left[parent[x]] ? left[parent[x]]
: right[parent[x]] ) = y;
:-)
Why the smiley? It's actually not bad, although I'd reformat
slightly:
( parent[x] == &N::sentinel
? parent[this->header]
: x == left[parent[x]]
? left[parent[x]]
: right[parent[x]] )
= y ;
The one thing that would make me hesitate is that I'm not sure
that many people are familiar with the way ?: chains (even
though it is exactly like if/else). To tell the truth, I'd
never really realized it myself until John Potter pointed it out
(and suggested that the above could be a "standard" idiom).

I also feel a little unconfortable using ?: for lvalues. But
that's probably just because I'm not used to it. But it makes
it especially clear that the goal is to assign y to something,
and that we always, unconditionally, update *something* with y.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #11
On May 30, 3:04 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
Victor Bazarov wrote:
Jim Langston wrote:
"David Harmon" <sou...@netcom.comwrote in message
news:46**************@news.west.earthlink.net.. .
On Tue, 29 May 2007 17:28:13 +0200 in comp.lang.c++, desktop
<f...@sss.comwrote,
but I don't really like it. Any suggestions for making it look
better and more clear?
First step, "chain" the else-if and get rid of the extra level of
brackets.
> if (parent[x] == &N::sentinel) {
parent[(*this).header] = y; // Update root.
} else if (x == left[parent[x]]) {
left[parent[x]] = y;
} else {
right[parent[x]] = y;
}
This is one instance I would even get rid of the backetrs entirely,
although opionions vary on this.
if (parent[x] == &N::sentinel)
parent[(*this).header] = y; // Update root.
else if (x == left[parent[x]])
left[parent[x]] = y;
else
right[parent[x]] = y;
(parent[x] == &N::sentinel ?
parent[this->header]
: x == left[parent[x]] ? left[parent[x]]
: right[parent[x]] ) = y;
:-)
why not
(parent[x] == &N::sentinel && (parent[(*this).header] = y, true)) || (x
== left[parent[x]] && (left[parent[x]] = y, true)) || (right[parent[x]]
= y, true);
Because unlike Victor's example, that doesn't express the
intent. The intent is to set a particular element to y. Which
element varies, according to the structure of the hierarchy, but
that's a secondary detail. So we put it in a sub-expression of
the main expression, which is "<something= y". The key to
Victor's expression is that the "= y" is an unconditional part
of the top expression, and you don't have to check three
different branches to verify that they all end in an "= y".

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #12
James Kanze wrote:
On May 30, 3:04 pm, Zeppe
>why not
(parent[x] == &N::sentinel && (parent[(*this).header] = y, true)) || (x
== left[parent[x]] && (left[parent[x]] = y, true)) || (right[parent[x]]
= y, true);

Because unlike Victor's example, that doesn't express the
intent. The intent is to set a particular element to y. Which
element varies, according to the structure of the hierarchy, but
that's a secondary detail. So we put it in a sub-expression of
the main expression, which is "<something= y". The key to
Victor's expression is that the "= y" is an unconditional part
of the top expression, and you don't have to check three
different branches to verify that they all end in an "= y".
That's true. It was actually a joke to point out that the Victor's
example was too tricky to understand not having any need to be so (as it
was his intent, given the smiley). I actually would leave the
conditional operator for really simple tests or for some situations that
are difficult to solve otherwise (for example, a test in a copy
constructor).

I'm not afraid of the complex instructions, just I think that they
shouldn't be used if there is no need. And, in that case, that
expression in my opinion doesn't replicate the way the human brain reasons.

Regards,

Zeppe
May 31 '07 #13

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

Similar topics

5
by: vishal | last post by:
hi i am using session in php. but if the client has disabled his cookie then will my session work or not????????? thxs for your reply in advance.............
3
by: JD | last post by:
I have the below code that displays a small thumbnail if a user is saluted, if they aren't saluted it checks to see if they might have a profile picture, this all works with the exception that is...
4
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
17
by: Keith | last post by:
How can I do the following: If recordset is empty, redirect to page1 but if recordset has something in, redirect to page 2. Thanks
6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
5
by: Angelina | last post by:
Hi, I need some help in writing a 'if-then-else' statement. I currently have wrote one that looks something like this.. If Combobox1 = xxx and textbox1 <> "" then 'run stored procedure 1 to...
0
by: John Whitmer | last post by:
I am trying to put together a web page to show remote users which PC's are available for RDP so they can use. The page work's fine except for when the goes to the pc to pull the info on if anybody...
8
by: Drew | last post by:
I have an app that I am building that takes user input on 1 page then inserts/updates it on the next page. There are a total of 20 pages, with data from page1 being modified by page2 and so on. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...

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.