[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: const char* typedefs
- From: Sean Conner <sean@...>
- Date: Tue, 6 Feb 2018 15:03:23 -0500
It was thus said that the Great Paige DePol once stated:
>
> However, as I was thinking about it during this conversation I also realised
> that whenever I see "const char*" I just think "C String". A shorter name is
> always nicer, and "ccstr" also concisely summarises the object to me.
Okay, so "ccstr" is "typdedef char const *ccstr" [1] but does that imply
that "cstr" is "typedef char *cstr"? If so, then there's only a single
character difference between two different types so typos might be hard to
decern (maybe---it may be that the compiler might catch instances of ccstr
when a cstr was meant, but not so much the other way around).
My only other argument against it is that "char *" is *so* idiomatic for a
"string" that it seems silly to hide it just to save some typing [7].
-spc (Who realizes that "const char *" is more idiomatic than
"char const *" but does the later anyway ... )
[1] Why "char const" and not "const char"? Because "const" actually
binds to the right (except when it's at the leftmost position). I
was fond of doing stuff like:
const char *const p;
which defines p as a constant pointer to a constant character, but
it doesn't read that way. Instead:
char const *const p;
*does* read that way. I use that style mostly for global variables,
such as this one [2]:
extern void (*const c_conversion)(FILE *const restrict,FILE *const restrict);
which is a constant pointer to a function, said function takes two
FILE *s which are themselves constant [3] and guarenteed not to
point to the same thing [4]. Sometimes, globals are unavoidable,
but I try to make them read-only as much as possible [6].
[2] https://github.com/spc476/mod_blog/blob/6a92de05304987ae9d37d9e833a53523883a555d/src/globals.h#L69
[3] My thought when I wrote that at the time was to show that the
parameters were *not* arrays [5]. My thinking these days is to show
array parameters via '[]' which is more explicit in intent
(exception being C strings, which are technically arrays).
[4] That's what "restrict" is for.
[5] Because C makes it too easy to conflate pointers with arrays.
[6] Yes, they're defined in a single file:
https://github.com/spc476/mod_blog/blob/6a92de05304987ae9d37d9e833a53523883a555d/src/globals.c
and there, they aren't const. I have to set them somewhere. It's
only to the rest of the code that they are defined as constant. It
makes it a bit easier to reason about the code. There are a few
globals left that aren't "const" and I'm still trying to work on
those.
[7] We read code more than write it. Spend the extra few seconds
writing it.