Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Style question re common C expression. Message-ID: <588@haddock.UUCP> Date: Fri, 19-Jun-87 15:24:19 EDT Article-I.D.: haddock.588 Posted: Fri Jun 19 15:24:19 1987 Date-Received: Mon, 22-Jun-87 03:51:43 EDT References: <4934@utcsri.UUCP> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Distribution: world Organization: Interactive Systems, Boston Lines: 57 In article <4934@utcsri.UUCP> flaps@utcsri.UUCP (Alan J Rosenthal) writes: >Frequently I write expressions of the form "x ? x : y". For example, >in unix where "getenv" returns an environment variable, I might have >something like: ... (t = getenv("EDITOR")) ? t : "/bin/ed" ... >My complaint about this syntax is mostly about having to use the >temporary variable. I partially agree; one measure of the power of an operator (or language) is its ability to save you a temporary variable. I once proposed extending the "||" operator to do what you want -- this would break only those rare programs that pass non-booleans to "||" *and* use the result in a non-boolean context -- but decided it was a bad idea because (a) it blurs the distinction between boolean and integer, which is already fuzzy in C; and (b) (really a consequence of (a), I guess) it's not quite as powerful as such a construct could be, because the only comparison allowed is equality with 0. (Consider the similar example "(c=getchar()) != EOF ? c : defchar".) So, with tongue in cheek, I proposed the new syntax: getenv() unless ==0 inwhichcase "/bin/ed" (which immediately inspired my colleague to propose the "ifontheotherhand ... wemaysafelyassume" construct). Actually, I think what you really want is a pronoun. I first encountered this in PDL, in the construct if token_scanner_backed_up_flag is set unset it ... endif ; given this, your expression is just (getenv() ? it : "/bin/ed") and mine is (getchar() == EOF ? defchar : it). Of course, the problem now becomes one of defining the antecedent of the pronoun in a consistent way. >So the main question is, is there some nice way I missed to specify this? No, you need an explicit temporary (or a function call, in which the formal argument is the temporary). >And the secondary question is, if not then is there a standard macro >name and syntax I should use, like the standard [STREQ macro]? How will putting it into a macro solve anything? You still need the temp, and for a general macro you don't even have a consistent type for it. (Integer or pointer? Pointer to what?) I recommend you leave it as inline code. (Or else make a smarter function -- I don't bother for string getenv, but for reading numbers from the environment I've found int env(char *name, int defval) { char *ep; return ((ep = getenv(name)) == NULL || *ep == '\0' ? defval : atoi(ep)); } useful at times.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint