Path: csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Do you insist on const-correctness? Date: Sat, 23 Sep 2023 11:57:14 -0700 Organization: A noiseless patient Spider Lines: 86 Message-ID: <86msxck99x.fsf@linuxsc.com> References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc> <86a5tkmj1b.fsf@linuxsc.com> <20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="bbbdca6269fb3628276797e440c29686"; logging-data="972045"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yWMMCPb3FEp+TgtCJyLM9c5JCHK2jFt0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:Y5IUlYwLfE6XtQrDTVGroOl+fgY= sha1:6NCkMuexr7ghRuKbb0gLQT+GjGs= Xref: csiph.com comp.lang.c:176264 Anton Shepelev writes: > Tim Rentsch to Anton Shepelev: > > [an unused variable removed from quoted code:] > >>> void* a_setlen( void * a, unsigned len ) >>> { struct meta_t *m ; >>> >>> m = META ( a ); >>> m = setlen( m, len ); >>> if( m != NULL ) a = DATA( m ); >>> else a = NULL ; >>> return a; >>> } >> >> Is there some reason not to write a shorter and simpler >> function, such as the function below (please ignore >> differences in layout style) >> >> void* >> a_setlen( void *a, unsigned n ){ >> struct meta_t *const m = setlen( META( a ), n ); >> >> return m ? DATA( m ) : NULL; >> } > > My personal reasons are the following: > > 1. I prefer not to mix variable declaration and > initialisation, but first to declare my data and then > to work with it. > > 2. I consider nested calls a bit less readable than > sequential, alghough I do not alwasy avoid them. > > 3. Sequential calls let me insert ad-hoc debugging code > at the right points, e.g. between the invocations of > META() and setlen() above. > > 4. I consider the ternary operator a redundant special > case of the `if' statement that increases expression > nesting to the (granted, often but slight) detriment > of readability. > > I have snipped your comments about `const', but not without > having perused them. I have reflected on these comments and also some of the responses you have given to other posters. Here are some high-level reactions (without specifics). One: all of your preferences basically boil down to "this is what I'm used to". Two: you really need to learn new ways of thinking. You won't see (I predict) the advantages of alternative approaches until after you have assimilated them. If you don't make an effort to try and embrace other approaches you'll be caught in a vicious cycle. Three: Why should you give any weight to my suggestions? Think about this: I have been where you are. All of the things you're saying are things I might have said earlier in my programming experience. Fortunately I got some guidance from some very smart people and was able to learn new ways of thinking about programs. As an exercise try writing a_setlen() like this (and I mean really try it, put the code in and get it to compile). Don't complain about how awful it looks, I know it's different from what you're used to. void * ATEM( struct meta_t *m ){ return m ? DATA( m ) : NULL; } void* a_setlen( void *a, unsigned n ){ return ATEM( setlen( META( a ), n ) ); } Needless to say 'ATEM' is 'META' spelled backwards. If you think of a more suitable name feel free to use it, otherwise exactly like this (with my usual caveat to ignore differences in layout style).