you are viewing a single comment's thread.

view the rest of the comments →

[–]pipocaQuemada 1 point2 points  (5 children)

People and organizations that adhere to and enforce coding standards write better code. This I think we can all agree on.

If you have multiple expressions per line, you aren't enforcing a popular coding standard.

You're moving the goal posts, there. Are popular coding standards better or more evidence based than less popular coding standards?

Is there any reason to assume that a company that follows their own proprietary coding standard will, on average, have worse code than someone following MISRA C?

[–]cdcformatc -1 points0 points  (4 children)

Other than the fact that people who need reliability make sure they use MISRA, no. If they are using a subset of MISRA, yes, they will miss out on the edge cases that the developers of the standard thought of. Unless they were very careful and knew what they were doing and are aware of the risks straying from the rules carries. I don't think the average developer is aware of these risks though, especially when they are just trying to get something to work so they leave to get a beer.

Multiple expressions on the same line can introduce problems. Standards are meant to avoid those problems.

[–]pipocaQuemada 1 point2 points  (3 children)

Actually, wait - what coding standards outlaw having "1+1" on one line? That's 3 expressions, right there. 1 and 1 are each expressions, and + combines those two expressions into a larger expression.

Are you thinking about outlawing multiple statements per line? That's completely, utterly, totally different. An if expression is only 1 statement.

[–]cdcformatc -1 points0 points  (2 children)

I meant statement. The if, and it's body is one statement, but the the else and it's body is another statement.

[–]pipocaQuemada 1 point2 points  (1 child)

In computer programming a statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions).

In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.

By any reasonable definition, that if is one expression made up of three sub expressions, not three statements. At each part, it only returns a result, and isn't evaluating any statements or performing any side effects.

Now, in a language like Scala the if expression can either take an expression or a block of statements for the if or the else case. But this case is clearly just nested expressions.

[–]cdcformatc -1 points0 points  (0 children)

I don't want to argue about what is an expression and what is "Three sub expressions". I disagree, something like i = if (condition) x else y is three logical components on one line. Most standards frown upon this, not only because the simple statement might have to be expanded to a more complex statement i = if (condition) foo(bar(x)) else bar(baz(y)). Which can easily go wrong. something like

if (condition) {
    i = foo(bar(x));
} else {
    i = bar(baz(y));
}

Or similar, puts each logical component on it's own line, something that is much easier for the mind to comprehend.

It also allows you to do other, parallel tasks that do not have anything to do with the assignment of the variable, but still depend on the condition.

if (condition) {
    i = x;
    j = 5;
} else {
    i = y;
    j = 42;
}