Take the following word-count program I have, where a word is (naively) taken as alnums between whitespace:
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void)
{
// grab a segment of text until EOF
int current_char;
int num_words=0, num_chars=0, num_lines=0; // ignore whitespace
bool in_word;
printf("Enter your text here:\n");
while ((current_char=getchar()) != EOF)
{
if (current_char == '\n')
in_word ? num_words++, num_lines ++, in_word-- : num_lines++;
else if (isalnum(current_char))
num_chars ++, in_word=true;
else if (isspace(current_char))
in_word ? num_words++, in_word-- : ' ';
}
if (in_word) num_words ++;
printf("\nChar Count: %d | Line count: %d | Word count: %d\n", num_chars, num_lines, num_words);
}
Ignoring the naivety of the word-detection, is this considered bad practice to cram things into a single statement so braces can be skipped? For example, I suppose the 'long-form' version of the first if would be:
if (current_char == '\n')
{
num_lines ++;
if (in_word)
num_words ++;
in_word = false;
}
This seems more readable and easy for me to understand, but often I'll see long one-liners in C and so I was wondering why one choice may be used over the other.
For example, here's a line from postgres which seems to be like a too-long-one-liner that I have a difficult time understanding:
if (!gsets)
ereport(ERROR,
(errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
errmsg("too many grouping sets present (maximum 4096)"),
parser_errposition(pstate,
qry->groupClause
? exprLocation((Node *) qry->groupClause)
: exprLocation((Node *) qry->groupingSets))));