Deutsch English Français Italiano |
<vijnl4$36a0m$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: else ladders practice Date: Mon, 2 Dec 2024 08:29:40 +0100 Organization: A noiseless patient Spider Lines: 94 Message-ID: <vijnl4$36a0m$1@dont-email.me> References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org> <slrnvi746p.fkp.dan@djph.net> <else-20241116103316@ram.dialup.fu-berlin.de> <vihlgt$29jun$1@paganini.bofh.team> <vihvm2$2j9p0$1@dont-email.me> <viinlp$2bd76$1@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 02 Dec 2024 08:29:41 +0100 (CET) Injection-Info: dont-email.me; posting-host="8e313e19d33393365c924d71ecb789d8"; logging-data="3352598"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+P7dSJh8v+lwnkGHOfBpUT" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:9V8tskyCOp9YAa4p7HjIObOdIAA= In-Reply-To: <viinlp$2bd76$1@paganini.bofh.team> X-Enigmail-Draft-Status: N1110 Bytes: 4895 On 01.12.2024 23:23, Waldek Hebisch wrote: > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >> On 01.12.2024 13:41, Waldek Hebisch wrote: >>> Stefan Ram <ram@zedat.fu-berlin.de> wrote: >>>> >>>> My bad if the following instruction structure's already been hashed >>>> out in this thread, but I haven't been following the whole convo! >>>> >>>> In my C 101 classes, after we've covered "if" and "else", >>>> I always throw this program up on the screen and hit the newbies >>>> with this curveball: "What's this bad boy going to spit out?". >>>> >>>> Well, it's a blue moon when someone nails it. Most of them fall >>>> for my little gotcha hook, line, and sinker. >>>> >>>> #include <stdio.h> >>>> >>>> const char * english( int const n ) >>>> { const char * result; >>>> if( n == 0 )result = "zero"; >>>> if( n == 1 )result = "one"; >>>> if( n == 2 )result = "two"; >>>> if( n == 3 )result = "three"; >>>> else result = "four"; >>>> return result; } >>>> >>>> void print_english( int const n ) >>>> { printf( "%s\n", english( n )); } >>>> >>>> int main( void ) >>>> { print_english( 0 ); >>>> print_english( 1 ); >>>> print_english( 2 ); >>>> print_english( 3 ); >>>> print_english( 4 ); } >>>> >>> >>> That breaks two rules: >>> - instructions conditioned by 'if' should have braces, >> >> I suppose you don't mean >> >> if (n == value) { result = string; } >> else { result = other; } >> >> which I'd think doesn't change anything. - So what is it? >> >> Actually, you should just add explicit 'else' to fix the problem. >> (Here there's no need to fiddle with spurious braces, I'd say.) > > Lack of braces is a smokescreen hiding the second problem. > Or to put if differently, due to lack of braces the code > immediately smells bad. I know what you mean. Though since in the given example it's not the braces that correct the code, and I also think that adding the braces doesn't remove the "bad smell" (here). - YMMV, of course. - For me the smell stems from the use of sequences of 'if' (instead of 'switch'), and the lacking 'else' keywords. - Note that the OP's original code *had* braces; it nevertheless had a "bad smell", IMO. Spurious braces may even make the code less readable; so it depends. And thus a "brace rule" can (IME) only be a "rule of thumb" and any "codified rule" (see below) should reflect that. > >>> - when we have the result we should return it immediately. >> >> This would suffice to fix it, wouldn't it? > > Yes (but see above). > >>> Once those are fixed code works as expected... >> >> I find this answer - not wrong, but - problematic for two reasons. >> There's no accepted "general rules" that could get "broken"; it's >> just rules that serve in given languages and application contexts. >> And they may conflict with other "rules" that have been set up to >> streamline code, make it safer, or whatever. > > No general rules, yes. But every sane programmer has _some_ rules. > My point was that if you adopt resonable rules, then whole classes > of potential problems go away. I associated the term "rule" with formal coding standards, so that I wouldn't call personal coding habits "rules" but rather "rules of thumb" (formal coding standards have both). But personal projects (and programmers' habits) are anyway not my major concern, while coding standards actually are. When you formulate coding standards (and I've done that for a couple languages) you often have to walk on the edge of what's possible and what's sensible. Janis