Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site harvard.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!stew From: stew@harvard.ARPA (Stew Rubenstein) Newsgroups: net.lang.c Subject: Re: C programming style Message-ID: <251@harvard.ARPA> Date: Sun, 14-Jul-85 18:36:46 EDT Article-I.D.: harvard.251 Posted: Sun Jul 14 18:36:46 1985 Date-Received: Wed, 17-Jul-85 05:35:42 EDT References: <11434@brl-tgr.ARPA> <480@mmintl.UUCP> Reply-To: stew@harvard.UUCP (Stew Rubenstein) Organization: Aiken Computation Laboratory, Harvard Lines: 34 Summary: i++ leads to confusion, but ++i is often an excellent alternative. In article <480@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes: > >I agree that "i++" is an abomination. (I do use it, however, to be >consistent with the rest of the code I work with.) Actually, C has >a third way to represent this operation: "i += 1". Personally, I >think this is the superior notation. It is concise, yet easy enough >for a person unfamiliar with the language to interpret. One opinion that I haven't heard expressed: I, too, agree that "i++" is not the greatest. But why do people always use this postfix form when the prefix form, "++i", is available? From my vantage point as a long- time programmer, it is hard to say with certainty, but isn't it a whole lot more clear that ++ is an operator and is doing something to "i" in this case? The only excuse for i++ is that you want to use the value before incrementing. A good compiler, or a very bad one, might generate better code in some cases where i++ is used for its value, but frequently this results in the program doing extra work to stuff the old value in a temporary until the expression is evaluated! There are some common constructs in which i++ (or ptr++) is useful, but in the hands of many programmers, this construct leads to confusing statements performing two unrelated functions. >Incidently, "i = i + 1" is not all that obvious, either. Many people's >response, on seeing such an expression for the first time, is "impossible. >'i' can't equal 'i + 1'.". Once a beginning programmer learns about assignment statements, this is a non-issue. It would be nice if we had chosen some other character, but someone decided that underscore was more important than left-arrow back in the 60's, and now we're stuck.