lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Jan 8, 2021 at 12:51 PM bel wrote:
Since this is probably a matter of opinion

Yes, it is opinion-based.
And I want to know the mean opinion among Lua programmers.

 
+, -, * and ^, since these are the only ones everybody is supposed to learn in school.

Opinions may differ significantly:
Schoolkids know nothing except + - * ^
Experienced Lua programmers might get used to all Lua precedence rules.
I want to find the "happy medium" which could be used as a reasonable coding style guide
(and be implemented as the default option in a Lua code formatter)


Always add parentheses, except for +, -, * and ^

I doubt that over-parenthesizing improves code readability.
What about the standard chain of bitwise operators: | & <<
and logical operators: or and
IMO, every programming language has their priorities arranged in the same way.
So, even if you're a Lua newbie, but have a strong background in another language,
such priority chains look natural to you, so no extra parentheses are needed.

 
Even / can lead to confusion:  a*b/c*d
- Sometimes read by mathematicians as ((a*b)/(c*d)), with / as fraction line

"ab/cd" is really treated as "(a*b)/(c*d)" by mathematicians.
Do they use the star character for multiplication?

 
Sometimes you copy some code from one language to another (e.g., prototype with Lua but finally copy some mathematical algorithms to C for performance reasons).

Facilitating a code migration to another language is not a task for a coding style guide.
There should be special utilities to automate such conversions.

 
repeat
until (l > (a + b*c));

No bracket required, but readable in one second.

IMO, this code is over-parenthesized.
The usual Lua format
until l > a + b*c
is more readable to me.

 
Also properly placed spaces significantly improve readability.

What does "properly placed" mean?
Are you talking about context-dependent insertion of spaces?

Input example:
f(a+b*c)+g(x*y^z)

Usual rules output:
f(a + b * c) + g(x * y ^ z)
each binary operator is surrounded by spaces

Context-independent rules output:
f(a + b * c) + g(x * y^z)
low priority operators + * are surrounded by spaces,
high-priority operator ^ is not.

Context-dependent rules output:
f(a + b*c) + g(x * y^z)
ranking of operators into low- and high-priority depends on the current _expression_