Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #3236
| From | "MikeWhy" <boat042-nospam@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Coding style for conditionals |
| Date | 2011-04-06 19:58 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <inj27e$u8o$1@dont-email.me> (permalink) |
| References | <b8402511-f313-4b06-90da-6ccbcc9cb817@a17g2000yqn.googlegroups.com> |
"mzdude" <jsanga@cox.net> wrote in message
news:b8402511-f313-4b06-90da-6ccbcc9cb817@a17g2000yqn.googlegroups.com...
>I have no interest in starting a religious war on the subject, but
> I've
> been doing some code reviews and running across code like
>
> Method 1:
> int x;
> if( 0 == ( x = MyFunct() ) )
> // do something
>
> I personally don't like the style. I seem to remember there were
> reasons not to do this but
> today google is not my friend. I can't find any reasons against this.
> Are there valid reasons
> not to do this style?
>
> I would prefer
>
> Method 2:
> const int x = MyFunct();
> if( 0 == x )
> // do something
>
> reasons for method 2
> 1) declare and initialize
> 2) allows variable to be const
>
> Are they any valid reasons to not do method 1?
A not so tenuous reason is return types other than int, possibly involving
non-trivial default construction, or reference types.
const Foo & YourFunct();
...
const Foo & bar = YourFunct();
if (!!bar)
{ // do stuff
}
//------ or,
Foo baz(YourFunct());
if (!baz)
{} // intentionally blank
else
{ // do non-const stuff.
}
//==========
As you can see, consistent stylistic rules still leave too much room
individual deviation.
As a stylistic concern, I'll often insist on a bool operator!() const in the
class rather than an operator bool() const. This leads to the bang-bang
test, which in turn leads repetitively to answering the inevitable "what the
heck" question.
Embrace the differences, unless they're truly awful. I personally dislike
many things that I see in your code, but to change each occurrence as I
encounter them will keep us both busily unproductive for quite some time.
(All of them little things.... if( test ), rather than if (test), for
starters. Unrelated, but still topical: operator++() versus operator++(int).
I'll often define a pre-increment but not a post-increment, so code should
default to pre-increment, following STL convention, if no other compelling
reason exists.)
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Coding style for conditionals mzdude <jsanga@cox.net> - 2011-04-06 12:05 -0700
Re: Coding style for conditionals Victor Bazarov <v.bazarov@comcast.invalid> - 2011-04-06 15:21 -0400
Re: Coding style for conditionals Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-06 15:24 -0700
Re: Coding style for conditionals Michael Doubez <michael.doubez@free.fr> - 2011-04-07 05:28 -0700
Re: Coding style for conditionals "MikeWhy" <boat042-nospam@yahoo.com> - 2011-04-06 19:58 -0500
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-07 00:10 -0700
Re: Coding style for conditionals mzdude <jsanga@cox.net> - 2011-04-07 04:57 -0700
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-07 06:06 -0700
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-07 07:02 -0700
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-07 08:31 -0700
Re: Coding style for conditionals Puppet_Sock <puppet_sock@hotmail.com> - 2011-04-07 13:26 -0700
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-07 23:07 -0700
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-08 01:44 -0700
Re: Coding style for conditionals "io_x" <a@b.c.invalid> - 2011-06-05 09:04 +0200
Re: Coding style for conditionals Michael Tsang <miklcct@gmail.com> - 2011-04-09 22:23 +0800
Re: Coding style for conditionals Marek Borowski <marek_remove_@borowski.com> - 2011-04-08 13:23 +0200
Re: Coding style for conditionals gwowen <gwowen@gmail.com> - 2011-04-08 05:33 -0700
Re: Coding style for conditionals "robertwessel2@yahoo.com" <robertwessel2@yahoo.com> - 2011-04-08 16:07 -0700
Re: Coding style for conditionals Leigh Johnston <leigh@i42.co.uk> - 2011-04-09 00:36 +0100
Re: Coding style for conditionals "robertwessel2@yahoo.com" <robertwessel2@yahoo.com> - 2011-04-09 06:25 -0700
Re: Coding style for conditionals Öö Tiib <ootiib@hot.ee> - 2011-04-09 07:28 -0700
Re: Coding style for conditionals Juha Nieminen <nospam@thanks.invalid> - 2011-04-09 07:32 +0000
csiph-web