I
Ike Naar
[about
bytes = what % 256, what /= 256;
]
There can be a good reason to write code like that.
Suppose we have variables x and y that represent the position
of a bishop on a chess board. The piece is currently on
a black square, and this is an invariant according to the rules
of the game. No legal move can put it on a white square.
Now we move it from (x, y) (black square) to (x+3, y-3) (black square).
It could be written like this:
/* initial: black square */
x = x+3;
/* intermediate: white square */
y = y-3;
/* final: black square */
but the intermediate state violates the invariant and that's ugly.
It would be cleaner to write it like this (using Python-like multiple
assignment):
/* initial: black square */
x, y = x+3, y-3;
/* final: black square */
and avoid the invalid intermediate state.
Of course C does not have multiple assignment, but this comes close:
/* initial: black square */
x = x+3, y = y-3;
/* final: black square */
I think here the use of a comma expression leads to somewhat cleaner
code: the fact that it's a single statement shows the intention
to make one legal move, instead of two illegal moves that combine
into a legal move.
bytes = what % 256, what /= 256;
]
Clever people do not write code like that. Clever people *can* write
code like that, but are clever enough not to, because they understand
maintainance burdens, and why code clarity beats code cleverness in
99.9999% of cases.
There can be a good reason to write code like that.
Suppose we have variables x and y that represent the position
of a bishop on a chess board. The piece is currently on
a black square, and this is an invariant according to the rules
of the game. No legal move can put it on a white square.
Now we move it from (x, y) (black square) to (x+3, y-3) (black square).
It could be written like this:
/* initial: black square */
x = x+3;
/* intermediate: white square */
y = y-3;
/* final: black square */
but the intermediate state violates the invariant and that's ugly.
It would be cleaner to write it like this (using Python-like multiple
assignment):
/* initial: black square */
x, y = x+3, y-3;
/* final: black square */
and avoid the invalid intermediate state.
Of course C does not have multiple assignment, but this comes close:
/* initial: black square */
x = x+3, y = y-3;
/* final: black square */
I think here the use of a comma expression leads to somewhat cleaner
code: the fact that it's a single statement shows the intention
to make one legal move, instead of two illegal moves that combine
into a legal move.