473,786 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comment on style

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

Jan 27 '06 #1
18 2125
po******@gmail. com wrote:
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.
Jan 27 '06 #2
* po******@gmail. com:

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();
}
}
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?
Jan 27 '06 #3
Pep
po******@gmail. com wrote:
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.

Jan 27 '06 #4
po******@gmail. com ha scritto:
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?
Jan 27 '06 #5
marco ha scritto:
po******@gmail. 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!
Jan 27 '06 #6
> 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
Jan 27 '06 #7
Pep
benben wrote:
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.

Jan 27 '06 #8

"benben" <be******@yahoo .com.au> wrote in message
news:43******** *************** @news.optusnet. com.au...
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. :-))

-Howard


Jan 27 '06 #9
> 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.
Or...you could just do this:

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

The problem with the "every brace gets its own line" philosophy is that
reading the code requires a lot more scrolling. It's extremely
aggrivating trying to figure out a piece of code where half of all the
lines are just braces, because there's never very much information on
one page.

I find that It's easy to tell how often someone has to read/debug other
peoples' code by how he structures his his own code. If he uses a
separate line for every brace, tabs all over the place (or worse yet,
spaces mixed with tabs), hungarian notation, etc., then he probably
hasn't spent much time looking at someone else's code.

And, no offense, but generally, people who code in a vacuum like that
don't often write good code. In fact, the best coders I know don't get
to write their own code very often. They spend most of their time
fixing someone else's bugs, since they're the only ones who can.
Finally I hate single character variable names, even in example code, even
though I sometimes (rarely) use them myself :)


There's nothing wrong with it if its scope is very localized. If I can
see what 'x' is simply by looking at the line of code above where it's
used, there's no reason to give 'x' a long name; it's just clutter.

Aaron

Jan 27 '06 #10

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

Similar topics

1
2138
by: Alessandro Crugnola | last post by:
Hi all, i need to use a regular expression to match javadoc style comments in a file, something like /** * Constructs a new Call instance. * * @param object the object the Function shall be executed on * @param func the Function that shall be executed * @throws IllegalArgumentException if neigther the object or the function is not available.
1
1667
by: Chuck | last post by:
I am working with C# on Visual Studio 2003 SP1 on an XP professional SP2 system. All of the *.htm files generated by "Build Comment Web Pages" are flagged as restricted sites when I try to view them on IE 6.0. This problem occurred after installing the recent service packs. The only work around I have found is either to reduce the protection enforced on a restricted site or to manually replace the header line in every generated htm...
6
9501
by: Rene Pijlman | last post by:
The code below results in an exception (Python 2.4.2): HTMLParser.HTMLParseError: bad end tag: "</foo' + 'bar>", at line 4, column 6 Should it? The end tag it chokes on is in comment, isn't it? import HTMLParser HTMLParser.HTMLParser().feed(""" <html><head><title></title></head><body><script>
6
2082
by: patrick j | last post by:
Hi I've been testing out the "Conditional Comment" for IE. This is because for my web-site I want to have two style sheets, one for IE 6 and one for other browsers. Thus I hope to have my web-site as I want it for "other browsers" but still have it presentable in IE. I understand why the conditional comment works, despite the subject header for this posting. What I don't understand is why IE "ignores" the link to the CSS that precedes...
3
2408
by: Martin P. Hellwig | last post by:
Hi all, I've been toying with python for about two years now. Not every day, just when I encounter something in my job (sysadmin) repetitively dull. The amazing thing is that like any other language (natural or not) learning it more gives you power to express your thoughts better and create something from nothing, for me this is something I can only compare to freedom. However since I'm learning more of python I've struggled with
29
2375
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and there's no doubt that context- dependent text formatting in IDEs (keywords, strings, comments etc) is a massive help too, therefore benefitting development and maintenance. This idea is in a similar vein, especially for when scripts grow large.
0
3561
by: karen987 | last post by:
Could someone please tell me what code to add here? I have a weblog, with daily news which readers can add comments to, which are stored in a database. The form to add the comments is on an ASP page, (see code below). There is a small box, where readers can type a comment. What i want to do is to add a simple text editor, nothing too elaborate. A toolbar above the text area, should suffice, and it should be the same size as the box obviously. ...
14
3073
by: Siv | last post by:
Hi, Just busily coding away and removed a procedure from my code and all of a sudden an error came up miles away from the location of the piece of code I removed and it relates to the XML comment inserted at the top of the procedure. I had this happen once before and I couldn't fathom why it was complaining, the XML comment is like this:
39
2887
by: polas | last post by:
Afternoon all. I was just wondering about this point - I have (generally) used // for commenting a single line in C, but from looking at code other people have written it seems many use /* */ (which I only use if my comment will be over multiple lines) - does one way have any advantages over the other, or is the style exactly that, a question of style? Cheers, Nick
0
9655
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
10363
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
10169
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...
1
7517
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
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.