5 ms·
Ugh. for (i = 0; i < sizeof love / sizeof *love; i++) if (love[i] < 4) printf("%s", say[love[i]]); else for (j = 0;
by tom_mellior 8y ago
Ugh.
for (i = 0; i < sizeof love / sizeof *love; i++)
if (love[i] < 4)
printf("%s", say[love[i]]);
else
for (j = 0; j < love[i] / 4; j++)
putchar(show[love[i] % 4]);
Yes, C doesn't force you to use braces here, but this is really overdoing it.
And I say this as someone who does like Python's indentation-based syntax. Doing the same in C is not idiomatic.
- userbinator 8y agoThat's the same style in K&R, which is about as "idiomatic" as it gets. I don't see anything wrong with it.
- moccachino 8y agoWhat's wrong with it is that it's really really easy to make a mistake and introduce a subtle bug that will be hard to spot. Especially now since languages like python have been developed that have made people used to indentation-defined code. It's the style in K&R? Ok, so the literal inventor of the language was certain he wouldn't make this kind of mistake, that still doesn't mean I trust myself not to.
- neatcoder 8y agoOmitting braces for single statements is quite idiomatic in the C development world, whether we like it or not. If we don't trust ourselves that we wouldn't make this kind of mistake, use the right tooling: 'gcc -Wall' (since GCC 6) or 'clang-format' can easily detect misleading indentation.
- tom_mellior 8y agoI don't have a copy of K&R handy, but I feel like I would remember if it really had an example like this nested three levels deep. I would be grateful if someone could check.
- susam 8y ago§ 1.5.3 Line Counting #include <stdio.h> /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); } § 1.5.4 Word Counting #include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); } Also, see § 1.6 Arrays, § 2.8 Increment and Decrement Operators, etc. By the way, I would always use brace while working on a team project where there are other developers involved. But this is a one-time, personal, and hobby project where I am the only developer and I prefer to choose the K&R style when nobody else is involved.
- tom_mellior 8y agoWow, that was quick, thanks. Though the first example is only two levels deep and also less complex than your code in question, which has a loop in the else part of an if in a loop... The second example uses braces (as it must), so it's not an example of what I was asking for.
- susam 8y agoOkay, I was counting the nesting depth incorrectly. Here are a few more examples: § 3.8 Goto and labels found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* got one: a[i-1] == b[j-1] */ ... else /* didn't find any common element */ § 6.3 Arrays of Structures /* count C keywords */ main() { int n; char word[MAXWORD]; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) if ((n = binsearch(word, keytab, NKEYS)) >= 0) keytab[n].count++; for (n = 0; n < NKEYS; n++) if (keytab[n].count > 0) printf("%4d %s\n", keytab[n].count, keytab[n].word); return 0; }
- NeedMoreTea 8y agoDon't see how. It's exactly as you might see in K&R, and short enough that there's never going to be confusion from their lack.
- simias 8y agoI used to code like that for a long time so it doesn't bother me too much. Then I started learning Rust (which uses a C-style syntax but makes braces mandatory ever for single statements) and I started doing it even in C. In hindsight I feel stupid for not doing it earlier, it doesn't add too much noise (at least if you put the opening brace on the same line) and it wards off many potential nasty bugs if you copy code around or use poorly written macros.
- Sean1708 8y agoMy rule has always been that if you're going to write a single line if-statement it should actually be a single line if-statement, i.e. no line break between the condition and the curly bracket.
- JustSomeNobody 8y agoI coded like this when I was self taught and then in college. Once I got my first programming job and started fixing other people's bugs that the braces would have avoided, I stopped doing this.
- GrumpyNl 8y agoJust very clear code.
- rcdmd 8y agoI'm sorry but I have to point out the irony in showing so much disgust about a few lines of code that happen to literally say "I love you."
- johnisgood 8y agoThe content is completely irrelevant, s/he only showed dislike of the indentation with which I disagree. What does it matter whether or not it says "I love you"? Should we not be able to criticize any other aspects of code when it prints or displays something positive?
- rcdmd 8y agoGive criticism, sure, but keep some civility too. It's Valentine's Day! Be kind.
- johnisgood 8y agoHow was he not civil? Was it the "ugh" part? Yeah, well... I am lonely. :(
- freefal 8y agoAnd no unit tests or CI either? This is how rockets and hearts end up in pieces! Good thing we can indicate our love with persnickety comments and have no need for such a program.
- whatshisface 8y agoThere is no love, only unit testing and coding style debates.
- johnisgood 8y agoHmm, I can have debates while maintaining my love for that person with whom I am having a debate!