Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Write int as a 4 byte big-endian to file. |
| Date | 2012-03-13 09:56 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrn3vfsjlu6e5.nro.ike@sverige.freeshell.org> (permalink) |
| References | (2 earlier) <op.way6wnqc5qv6o3@toshiba-laptop> <kfn8vj6oq86.fsf@x-alumni2.alumni.caltech.edu> <3f8166c1-cc22-42d2-b758-d1dcfdd560d2@gw9g2000vbb.googlegroups.com> <jjlv7g$rki$2@speranza.aioe.org> <5d46cdc7-5414-4444-9dcf-f3fdab9e50bc@g16g2000yqe.googlegroups.com> |
On 2012-03-13, gwowen <gwowen@gmail.com> wrote:
[about
bytes[i] = 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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Write int as a 4 byte big-endian to file. MS <ms@no_spam_thanks.com> - 2012-03-10 18:00 +0000
Re: Write int as a 4 byte big-endian to file. Don Y <this@isnotme.com> - 2012-03-10 11:11 -0700
Re: Write int as a 4 byte big-endian to file. MS <ms@no_spam_thanks.com> - 2012-03-10 18:36 +0000
Re: Write int as a 4 byte big-endian to file. Stephen Sprunk <stephen@sprunk.org> - 2012-03-10 14:09 -0600
Re: Write int as a 4 byte big-endian to file. Keith Thompson <kst-u@mib.org> - 2012-03-10 14:06 -0800
Re: Write int as a 4 byte big-endian to file. Stephen Sprunk <stephen@sprunk.org> - 2012-03-10 21:53 -0600
Re: Write int as a 4 byte big-endian to file. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-10 15:20 -0500
Re: Write int as a 4 byte big-endian to file. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-10 15:22 -0500
Re: Write int as a 4 byte big-endian to file. Bl0ckeduser <bl0ckedusersoft@gmail.com> - 2012-03-10 14:42 -0500
Re: Write int as a 4 byte big-endian to file. Bl0ckeduser <bl0ckedusersoft@gmail.com> - 2012-03-10 16:33 -0500
Re: Write int as a 4 byte big-endian to file. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-10 13:46 -0800
Re: Write int as a 4 byte big-endian to file. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-03-10 17:42 -0700
Re: Write int as a 4 byte big-endian to file. Bl0ckeduser <bl0ckedusersoft@gmail.com> - 2012-03-10 22:53 -0500
Re: Write int as a 4 byte big-endian to file. Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-10 11:44 -0800
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-10 17:18 -0500
Re: Write int as a 4 byte big-endian to file. Ike Naar <ike@iceland.freeshell.org> - 2012-03-10 23:00 +0000
Re: Write int as a 4 byte big-endian to file. Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 13:08 -0700
Re: Write int as a 4 byte big-endian to file. tom st denis <tom@iahu.ca> - 2012-03-12 05:56 -0700
Re: Write int as a 4 byte big-endian to file. jacob navia <jacob@spamsink.net> - 2012-03-13 00:01 +0100
Re: Write int as a 4 byte big-endian to file. gwowen <gwowen@gmail.com> - 2012-03-13 00:55 -0700
Re: Write int as a 4 byte big-endian to file. Ike Naar <ike@sverige.freeshell.org> - 2012-03-13 09:56 +0000
Re: Write int as a 4 byte big-endian to file. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-13 12:26 +0000
Re: Write int as a 4 byte big-endian to file. Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 16:04 -0700
Re: Write int as a 4 byte big-endian to file. Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 15:54 -0700
Re: Write int as a 4 byte big-endian to file. Sjouke Burry <s@b> - 2012-03-10 19:44 +0000
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-10 17:21 -0500
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-10 14:57 -0500
Re: Write int as a 4 byte big-endian to file. Ike Naar <ike@iceland.freeshell.org> - 2012-03-10 21:17 +0000
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-10 17:22 -0500
Re: Write int as a 4 byte big-endian to file. Lew Pitcher <lpitcher@teksavvy.com> - 2012-03-10 15:55 -0500
Re: Write int as a 4 byte big-endian to file. Geoff <geoff@invalid.invalid> - 2012-03-10 13:01 -0800
Re: Write int as a 4 byte big-endian to file. Stephen Sprunk <stephen@sprunk.org> - 2012-03-10 22:01 -0600
Re: Write int as a 4 byte big-endian to file. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-11 00:36 -0800
Re: Write int as a 4 byte big-endian to file. "io_x" <a@b.c.invalid> - 2012-03-13 12:29 +0100
Re: Write int as a 4 byte big-endian to file. "io_x" <a@b.c.invalid> - 2012-03-13 13:14 +0100
Re: Write int as a 4 byte big-endian to file. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-10 12:39 -0800
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-10 17:29 -0500
Re: Write int as a 4 byte big-endian to file. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-10 15:02 -0800
Re: Write int as a 4 byte big-endian to file. "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-11 18:26 -0400
Re: Write int as a 4 byte big-endian to file. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-10 21:12 +0000
Re: Write int as a 4 byte big-endian to file. jgk@panix.com (Joe keane) - 2012-03-11 21:29 +0000
Re: Write int as a 4 byte big-endian to file. Ian Collins <ian-news@hotmail.com> - 2012-03-12 10:34 +1300
Re: Write int as a 4 byte big-endian to file. jgk@panix.com (Joe keane) - 2012-03-12 21:37 +0000
Re: Write int as a 4 byte big-endian to file. Keith Thompson <kst-u@mib.org> - 2012-03-12 23:40 -0700
Re: Write int as a 4 byte big-endian to file. Kaz Kylheku <kaz@kylheku.com> - 2012-03-13 07:11 +0000
Re: Write int as a 4 byte big-endian to file. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-11 21:45 +0000
csiph-web