Isn't prefix better than postfix?

  • Follow


I was under the impression that it is better to use to use prefix
operators than to use postfix operators when one has a choice, because
the former doesn't require a temporary (In case of overloaded
operators, assuming they are written to mimic the usual meaning) . In
case of in-built types, the compiler can optimise away the temporary
(in case of the postfix operators), on detecting that the return value
is not used. Won't it be a better coding style to always use prefix,
even for in-built types, when there is a choice? However, in TC++PL,
postfix operators are used at times, even when the return value is to
ignored. Is there more to this than I see?

--                                                    --
Abstraction is selective ignorance. -Andrew Koenig
--                                                    --

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kprateek88 11/4/2004 12:09:08 PM

Prateek R Karandikar wrote:
> I was under the impression that it is better to use to use prefix
> operators than to use postfix operators when one has a choice, because
> the former doesn't require a temporary (In case of overloaded
> operators, assuming they are written to mimic the usual meaning) . In
> case of in-built types, the compiler can optimise away the temporary
> (in case of the postfix operators), on detecting that the return value
> is not used.

So, it doesn't really matter for built-ins, does it?

 > Won't it be a better coding style to always use prefix,
> even for in-built types, when there is a choice?

For a novice it would be a good style, probably.

 > However, in TC++PL,
> postfix operators are used at times, even when the return value is to
> ignored. Is there more to this than I see?

Surely there is more to this.  Using pre-increment all the time is a good
practice for novices who need some good habits formed before they can
venture into the real world.  Using post-increment and not pre-increment
where it really doesn't matter is a prerogative of a mature programmer.

OOH, an author of a book targeting _novices_ should perhaps follow the
"use pre-increment because it promotes forming of a good habit" rule.
OTOH, any author is free to do whatever if the end-result doesn't depend
on the method used.

After all, style is a personal preference.  It can always be judged.  And
we have waaaaay too many judges of style around.

V

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Victor 11/5/2004 3:22:37 AM


> Is there more to this than I see?

Does the answer for "Which is more efficient: i++ or ++i?" contain
informations that you do not know already?
http://www.fmi.uni-konstanz.de/~kuehl/c++-faq/operator-overloading.html#faq-13.12

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Markus 11/5/2004 3:19:19 PM

"Prateek R Karandikar" <kprateek88@yahoo.com> wrote in message
news:607f883e.0411030206.492a9cda@posting.google.com...
 > I was under the impression that it is better to use to use prefix
 > operators than to use postfix operators when one has a choice, because
 > the former doesn't require a temporary (In case of overloaded
 > operators, assuming they are written to mimic the usual meaning) . In
 > case of in-built types, the compiler can optimise away the temporary
 > (in case of the postfix operators), on detecting that the return value
 > is not used. Won't it be a better coding style to always use prefix,
 > even for in-built types, when there is a choice? However, in TC++PL,
 > postfix operators are used at times, even when the return value is to
 > ignored. Is there more to this than I see?

C programmers often prefer postfix operators, maybe because of the
symmetry between:   a+=2;
              and:   a++;
Old habits die hard...

The same could be said of the usage of p<end instead of p!=end
(e.g. as the exit condition of a for loop). In C++ the latter is
known to be more general, but you'll often see code using
the "classic" form, which some perceive as safer...


Cheers,
Ivan
-- 
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ivan 11/5/2004 5:24:56 PM

You are correct that the prefix operators should be prefered to
postfix.

see: http://www.gotw.ca/gotw/074.htm, search for "Prefer using prefix"

I believe that the popularity of the postfix operators come from the
name of the language: "c++".
__
Jonas Norberg


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Jonas 11/5/2004 5:26:28 PM

Prateek R Karandikar wrote:

 > I was under the impression that it is better to use to use prefix
 > operators than to use postfix operators when one has a choice, because
 > the former doesn't require a temporary (In case of overloaded
 > operators, assuming they are written to mimic the usual meaning) .

True, and I think it was a sad mistake in the language design to have
let the operator functions do everything on their own with such a big
freedom, which makes people have to "unlearn" i++ and change into ++i
even when they don't have to care whether it's a prefix or a postfix:

http://groups.google.com/groups?selm=bd47bb0e.0306280817.17db0fe2%40posting.google.com

-- 
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Seungbeom 11/6/2004 2:01:12 PM

"Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> wrote in
message news:<cme0vh$hm1$1@newshispeed.ch>...
> "Prateek R Karandikar" <kprateek88@yahoo.com> wrote in message
> news:607f883e.0411030206.492a9cda@posting.google.com...

>  > I was under the impression that it is better to use to use prefix
>  > operators than to use postfix operators when one has a choice,
>  > because the former doesn't require a temporary (In case of
>  > overloaded operators, assuming they are written to mimic the usual
>  > meaning) . In case of in-built types, the compiler can optimise
>  > away the temporary (in case of the postfix operators), on detecting
>  > that the return value is not used. Won't it be a better coding
>  > style to always use prefix, even for in-built types, when there is
>  > a choice? However, in TC++PL, postfix operators are used at times,
>  > even when the return value is to ignored. Is there more to this
>  > than I see?

> C programmers often prefer postfix operators, maybe because of the
> symmetry between:   a+=2;
>               and:   a++;
> Old habits die hard...

That's the first time I've ever been appraised of the symmetry.  I know
that I prefer a++ for the simple reason that that is what Kernighan and
Richie did.  As to why Kernighan and Richie chose this style, I can only
guess: they were working mainly on a PDP-11, and the machine
instructions have post-increment, or perhaps most of the time they did
use the value, they needed post-increment, so it seemed more likely.  Or
maybe thy just flipped a coin, since it didn't make a difference.

> The same could be said of the usage of p<end instead of p!=end
> (e.g. as the exit condition of a for loop). In C++ the latter is known
> to be more general, but you'll often see code using the "classic"
> form, which some perceive as safer...

In general, I would say use < for integral indexes, and != for anything
pointer-like.  The < expresses part of the loop invariant more
precisely.

--
James Kanze           GABI Software         http://www.gabi-soft.fr
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

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 11/8/2004 11:36:19 PM

"Seungbeom Kim" <musiphil@bawi.org> wrote in message
news:cmi6fh$erq$1@news.Stanford.EDU...
 > Prateek R Karandikar wrote:
 >
 > > I was under the impression that it is better to use to use prefix
 > > operators than to use postfix operators when one has a choice, because
 > > the former doesn't require a temporary (In case of overloaded
 > > operators, assuming they are written to mimic the usual meaning) .
 >
 > True, and I think it was a sad mistake in the language design to have
 > let the operator functions do everything on their own with such a big
 > freedom, which makes people have to "unlearn" i++ and change into ++i
 > even when they don't have to care whether it's a prefix or a postfix:
 >
 > http://groups.google.com/groups?selm=bd47bb0e.0306280817.17db0fe2%40posting.google.com

I don't see how programmer conventions and misconceptiosn can be considered
a mistake of the language design. I agree certain implementations of
operators are less than ideal, but that is a programmer issue not a language
issue. It is best though that the language design be flexible, I would argue
in fact that it was in fact a mistake to predefine all of the primitive
operations. In Heron, for instance, all primitive operations are defined in
the standard library so the operators require great freedom.

Christopher Diggins
http://www.heron-language.com


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply christopher 11/12/2004 12:07:22 PM

7 Replies
154 Views

(page loaded in 0.115 seconds)

Similiar Articles:













7/22/2012 1:30:07 AM


Reply: