C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comment on style

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
pocmatos@gmail.com
Guest





PostPosted: Fri Jan 27, 2006 9:09 am    Post subject: Comment on style Reply with quote



Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I'd like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it's body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I've wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos

Back to top
Ian Collins
Guest





PostPosted: Fri Jan 27, 2006 9:38 am    Post subject: Re: Comment on style Reply with quote



[email]pocmatos (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I'd like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it's body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I've wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

What purpose would the returns serve? You are already in an if else

structure.

--
Ian Collins.

Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jan 27, 2006 9:42 am    Post subject: Re: Comment on style Reply with quote



* [email]pocmatos (AT) gmail (DOT) com[/email]:
Quote:

Imagine a function returning void (for example) and it's body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I've wrote it

No, you should always use proper indentation, like

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

Quote:
or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

With exception safe code it doesn't matter technically. But with
respect to clarity it's proper only for precondition checking. Within
the meat of the function it can hide the structure of the code (of
course that assumes proper indentation to begin with), and it can make
maintenance more difficult, for example where you want to add some
common action at the end, so there it's a judgement call.

For the C language the story is different, and I think the best advice I
can give to a novice here is: if you do not understand the difference
between C and C++ wrt. early returns, then absolutely don't use them;
otherwise, use clarity of code as the main guideline.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Back to top
Pep
Guest





PostPosted: Fri Jan 27, 2006 10:47 am    Post subject: Re: Comment on style Reply with quote

[email]pocmatos (AT) gmail (DOT) com[/email] wrote:

Quote:
Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I'd like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it's body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I've wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos

Yep, style is definitely personal.

Ian Collins has already asked what purpose would the returns serve as you
are already in an if/else block. I agree with his question and would point
out that you have said this a big function, so you need to consider whether
it would be helpful for another developer to come at a later time and have
to deal with multiple exit points when trying to solve a bug in the
routine, which I would say it is not.


Alf P. Steinbach has stated that you should use proper indentation which
again I agree with but as to whether proper indentation demands 2 spaces, 4
spaces or tabs is open to personal interpretation. Myself, I prefer tabs.

Now I differ from most people that follow the K&R style because I would
prefer the code writtens as

void foo(const MyClass &c)
{

if(c.bar())
{
x();
}
else if(c.foobar())
{
y();
}
else if(c.mybar())
{
z();
}

}

Which I find more readable foe one thing and find it easier to comment out a
block of code because

} else if(c.foobar()) {
y();
} else if(c.mybar()) {

requires you to edit the "} else if(c.foobar()) {" line to place the "else"
statement on a separate line in order to comment out the line or insert a
"/*" in the middle of the else line, whereas

}
else if(c.foobar())
{
y();
}
else if(c.mybar())

simply requires you to place a /* ahead of the "else if(c.foobar())" line
and a "*/" after the closing brace.

Similarly I prefer to see

else if(c.foobar())
{
y();
}

as opposed to

else if(c.foobar())
y();

As you say, style is personal.

Finally I hate single character variable names, even in example code, even
though I sometimes (rarely) use them myself :)

Cheers,
Pep.


Back to top
marco
Guest





PostPosted: Fri Jan 27, 2006 10:59 am    Post subject: Re: Comment on style Reply with quote

[email]pocmatos (AT) gmail (DOT) com[/email] ha scritto:
Quote:
Hi all,

While I was programming 5 minutes ago a recurring issue came up and
this time I'd like to hear some opinions on style. Although they are
usually personal I do think that in this case as also to do with making
the code easier to read.

Imagine a function returning void (for example) and it's body is a big
if with lots of special cases:

void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

The question is if you prefer to see like I've wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos

From your post I understand you want to know the difference between the

following two cases:

case 1:
void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

case 2:
void foo(const MyClass &c) {
if(c.bar()) {
x();
return;
} else if(c.foobar()) {
y();
return;
} else if(c.mybar()) {
z();
return;
}
}

Am I write or not?

Back to top
marco
Guest





PostPosted: Fri Jan 27, 2006 11:02 am    Post subject: Re: Comment on style Reply with quote

marco ha scritto:
Quote:
pocmatos (AT) gmail (DOT) com ha scritto:

The question is if you prefer to see like I've wrote it or with a
return; after a call to x(), and another one after a call to y(), etc
etc... ?

Cheers,

Paulo Matos

From your post I understand you want to know the difference between the
following two cases:

case 1:
void foo(const MyClass &c) {
if(c.bar()) {
x();
} else if(c.foobar()) {
y();
} else if(c.mybar()) {
z();
}
}

case 2:
void foo(const MyClass &c) {
if(c.bar()) {
x();
return;
} else if(c.foobar()) {
y();
return;
} else if(c.mybar()) {
z();
return;
}
}

Am I write or not?
of course in case #2 else "must" be removed!


Back to top
benben
Guest





PostPosted: Fri Jan 27, 2006 11:08 am    Post subject: Re: Comment on style Reply with quote

Quote:
Now I differ from most people that follow the K&R style because I would
prefer the code writtens as

void foo(const MyClass &c)
{

if(c.bar())
{
x();
}
else if(c.foobar())
{
y();
}
else if(c.mybar())
{
z();
}

}


I'd agree and that's what I would do...except when there's only one
statement in the block (and it'll remain in one statement in the near
future.) I prefer:

if (c.bar()) x();
else if (c.foobar()) y();
else if (c.mybar()) z();

Regards,
Ben

Back to top
Pep
Guest





PostPosted: Fri Jan 27, 2006 12:16 pm    Post subject: Re: Comment on style Reply with quote

benben wrote:

Quote:
Now I differ from most people that follow the K&R style because I would
prefer the code writtens as

void foo(const MyClass &c)
{

if(c.bar())
{
x();
}
else if(c.foobar())
{
y();
}
else if(c.mybar())
{
z();
}

}


I'd agree and that's what I would do...except when there's only one
statement in the block (and it'll remain in one statement in the near
future.) I prefer:

if (c.bar()) x();
else if (c.foobar()) y();
else if (c.mybar()) z();

Regards,
Ben

Yep, I can go along with that though myself I would still write it as a
brace'd block of code :)

Cheers,
Pep.


Back to top
Howard
Guest





PostPosted: Fri Jan 27, 2006 3:47 pm    Post subject: Re: Comment on style Reply with quote


"benben" <benhongh (AT) yahoo (DOT) com.au> wrote

Quote:
Now I differ from most people that follow the K&R style because I would
prefer the code writtens as void foo(const MyClass &c)
{

if(c.bar())
{
x();
}
else if(c.foobar())
{
y();
}
else if(c.mybar())
{
z();
}

}


I'd agree and that's what I would do...except when there's only one
statement in the block (and it'll remain in one statement in the near
future.) I prefer:

if (c.bar()) x();
else if (c.foobar()) y();
else if (c.mybar()) z();


The problem with that style is that many IDE's debuggers have trouble with
allowing you to step through that code nicely. I often set a breakpoint on
the action following the if, so that I can quickly see when the condition
succeeds. That's not possible if I can't set a breakpoint on it because
it's on the same line as the if. So... I'd put the action on the line
following the if, like this:

if (a)
doa();
else if b
do(b);

(Also, sometimes I'm not positive that I'll never add a second line of
action, or I get lost in what else goes with what if, so I end up adding
back those curly braces anyway. Smile)

-Howard







Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.