Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Readability part I - code Date: Sat, 23 May 2015 19:10:12 -0700 Organization: None to speak of Lines: 81 Message-ID: <87wpzypvy3.fsf@kst-u.example.com> References: <874mnojjn4.fsf@kst-u.example.com> <87d21ssawz.fsf@kst-u.example.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="9be37d943435275679235b07c7947eef"; logging-data="2324"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VOtWJHiQW/4Ml6Om6VYmo" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:JK1iAwDZlBTFJp8IenDqtxLAHBk= sha1:nPCTOrNu8QIn0Jy7EZSS+UFkCS4= Xref: csiph.com comp.lang.c:62509 Stephen Sprunk writes: > On 22-May-15 13:51, Keith Thompson wrote: >> One possible objection I can imagine to K&R sthle is that this: >> >> if (condition) >> statement; >> >> (which would be followed by statements *not* controlled by the "if") is >> difficult to distinguish from this: >> >> if (condition) { >> statement; >> >> (which would be followed by statements that *are* controlled by the >> "if"). Personally I avoid that by never using the first form. > > Perhaps you don't consider it sufficient, but the _lack_ of indent on > the line following "statement;" indicates fairly clearly that it's not > part of the conditional. > > I have no difficulty distinguishing these: > > if (condition) { > foo(); > bar(); > } > > vs > > if (condition) > foo(); > bar(); > > If someone messes up and indents the latter "bar();", well, then you > have another problem entirely... Clarity of indentation is not my only reason for always using braces for conditional and iteration statements. The fact that it simplifies the problem of seeing program structure is one of several benefits of that habit. > Also, I'll admit I've done this a few times to save space: > > if (condition) > foo(), > bar(); > > That's definitely not kosher according to most house styles, but it made > sense at the time and in the specific context. Ick. It took me a couple of minutes to realize that that's a comma after foo(). If I saw that without context, I'd assume it was a mistake, and I'd probably have to ask the author what the intent was. If I really needed to save space, I *might* consider writing if (condition) { foo(); bar(); } I once had to deal with some code that looked like this: if (condition) statement1; statement2; The context was such that, after a little thought, it was reasonably obvious that statement2 was meant to be outside the if. On closer examination, I realized that the author's tab setting were different from mine (and the code used a mixture of spaces and tabs), so the code would have looked like: if (condition1) statement1; statement2; Fortunately, I was able to send it back for a rewrite. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"