On 15/01/18 16:08, Manfred wrote:
> On 1/15/2018 1:13 AM, David Brown wrote:
>> On 14/01/18 22:48, Stefan Ram wrote:
>>> Vir Campestris <vir.cam...@invalid.invalid> writes:
>>>> Please consider that future readers of your code may not have memorised
>>>> them all.
>>>
> <snip>
>>>
>>> Disclaimer: I don't know all precedence levels by heart,
>>> I look them up if need be. This is what I can
>>> write from my memory, but I'm just guessing, I'm not sure:
>>>
>> <snip mistakes>
>>>
>>> I'm sure I have made some mistakes and omissions above.
>>> As I said, I look up the precedence when I need to know it.
>>>
>>
>> It is simply /crazy/ to write code that where you have a reasonable
>> expectation that average programmers will have to look up a reference
>> manual to interpret it correctly.
>>
> I believe this is a bit hard as it is stated. I agree this is not good
> practice, but I wouldn't categorize as hard as crazy.
> Bad practice is not the same as crazy, I think. Besides, you wrote
> yourself that there are cases where code cannot be written to be easily
> readable.
Yes, I was a bit harsh. But there is a big difference between code that
has to be hard because it is a complex task, and knowingly making code
harder when there is a clear and obvious way to make it easier to
follow. For most operator expressions, the precedence is easy to see
and get right - but where it is not, you put in the extra parenthesis
(or split up the expression - there is no need to put everything in one
expression or one line).
>
>> Some types of code are naturally difficult - some code in C++ is
>> downright mystical even for the world's leading experts. You can
>> expect that it will be difficult for most people to understand - and
>> most people will use references when trying to interpret it.
>>
>> But most people will /not/ use references when reading or writing code
>> with operators. They will, like you, guess. And they will mostly get
>> it correct. Sometimes, however, they will get it wrong, especially
>> when using less common operators and combinations. "1<<4 + 5" is 512,
>> not 21. "(1 << 4) + 5" leaves no room for confusion.
>>
>> It is irresponsible to write code that it harder to understand than
>> necessary, for something as trivial as saving a couple of characters.
>> (If there are so many parenthesis that they become confusing, the
>> expression should be split up.)
> I tend to agree with the baseline, although I think that expression
> splitting serves more that readability only.
Yes. Splitting it up can make it easier to follow. It can give you
additional naming points to make the logic clearer. It can make types
more explicit (that is quite relevant in my line of work, where types
smaller than "int" are common and unsigned arithmetic is often used -
you can make your integer promotions explicit and force conversion to
unsigned types without needing casts). It can enforce the ordering of
evaluations, give you lines for breakpoints, make it easier to add
temporary "printf debugs", asserts, etc.
I very rarely use the comma operator because it can easily be
misinterpreted. /I/ know how it works, and can get the precedence
levels right most of the time even without looking up a reference - but
I certainly can't be sure that applies to everyone who might be reading
my code. So in most cases where I might think about using the comma
operator, or using the result of an assignment expression, I will
consider splitting up the expression instead. My aim is that where
possible, the code can be understood at a glance - it should be as easy
as possible to interpret it correctly, and as hard as possible to
interpret it incorrectly. Minimising the number of parenthesis or
keeping everything in one complicated expression is /way/ down in my
list of priorities.
>
> It is downright scary to hear this
>> attitude from someone who claims to teach C and C++ classes. I really
>> hope your students have other sources of good programming practice
>> than their teacher.
> Here is where I tend to disagree. I think that it is right in the
> context of schooling that it may be required for students to be more
> familiar than average with the language syntax and rules.
> This is different than a work environment, where "time is money"[1], so
> you want to make life easy for yourself and your colleagues, and if a
> couple of parentheses can help, you should definitely use them.
It is not about "time is money" - it is about making it as easy as
possible to be correct (in reading and writing code), and as hard as
possible to be wrong. Saving money (with reduced test/debug/re-code
cycles) is a bonus - writing quality code in the first place is the aim.
>
> [1] In a school context, on the other hand, priority should be knowledge.
Presumably at a school you are teaching people to be able to use the
language in the real world. You thus must teach them to write code in a
good way - as clearly as possible. You also, as a teacher, do not give
your students sample code that is so messy and obfuscated that you can't
understand it yourself without looking up the details!
Of course, you also have to teach the students to understand worse code
than you teach them to write - they will see all sorts of stuff in the
real world. But that is a minor part in comparison to teaching them
good practices.