Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Coding style for small if statements Date: Sat, 22 Feb 2020 07:52:05 -0800 Organization: A noiseless patient Spider Lines: 31 Message-ID: <86r1ymd3h6.fsf@linuxsc.com> References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <20200131235446.1eb54beabc491ad372460592@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="a1e79922b2c02e9149a52088916eaff2"; logging-data="16010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SPNZCZeCgKbHL57ohGiS/dLXIy1za5as=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:EUtk9W1mVSUcGo/qyArGth06dls= sha1:HRLdHaPo90m2rB2ISgQpSZtgfnA= Xref: csiph.com comp.lang.c:150146 Anton Shepelev writes: [combined with additional context from earlier post] > I do not consider ?: because I think it redundant and > unintuitive. My friend, a talented programmer and > mathematician, initially thought that the second argument was > used in case the first was false, because of the ordering of > truth values in ascending order. Nothing in the syntax hints > at the implied analogy with the if-then-else statement. The comments you give help explain your reaction. I am also a programmer and mathematician, and I never had any such reaction. After all, there are plenty of cases in math books where something is defined as " if and otherwise." Besides which, the ?: operator in C is a new notation, so "intuition" about it shouldn't carry much weight. In any case, whatever the reasons for your original reaction, I recommend overcoming that initial reaction and embracing the use of ?: in situations like the context of the earlier discussion. I think you will find the ?: operator is quite useful in writing programs more concisely and understandably, even if in some technical sense it is "unnecessary" because of if()/else. Good notation is a powerful tool. The ?: operator is not a substitute for if()/else, but a related construction in a functional thought mode, as opposed to the more usual (for C) imperative thought mode. Programming in C should not be limited to an imperative thought mode; there are plenty of circumstances where an imperative thought mode is the way to go, but there are also plenty of circumstances where an functional thought mode is the way to go, and ?: can help with those.