3 ms·
The three exhibits of beginner-vs-expert C code are quite perplexing. - In the first one, there is advocacy for replacing (pointer == NULL) with (!pointer). I
by t239487293 3y ago
The three exhibits of beginner-vs-expert C code are quite perplexing.
- In the first one, there is advocacy for replacing (pointer == NULL) with (!pointer). I have no issue with either, but to me this sounds like canonical bikeshedding. I would certainly not argue that experts use only (!pointer) and beginners only (pointer == NULL).
- Then it advises replacement of a malloc()ated struct with an on-stack variable containing the same struct (fine so far, why not), then says "the assembly" is even superior if we make that variable "static". This completely glosses over the fact that those three options (malloc, on-stack, static) have completely different legitimate use-cases.
- In the second exhibit, there is a comparison of "glib-style" linked-list (in which the payload is separate, with pointers to it in the linked list), with "kernel-style" linked-list (linked-list pointers as a member structure in the payload structure). The author argues that the latter is what "ace" C coders do. Again, they have completely different use cases. Say you have a large payload structures in an array, and you want to represent a subset of them. "glib-style" can be the right approach then.
- The third exhibit, says "ace" C coders replace "fopen()/fread()" with "mmap()". They do mention in passing the small problem of portability (saying this is solved by just adding #ifdefs and having a separate codepath for Win32). Besides the obvious issues with that, what happens if the input is a pipe?
I find it particularly funny when the article so prominently mentions Dunning-Kruger.
- felipec 3y ago* I do not argue that all experts use !pointer, but most of them do, and if you understand the machine code that will be generated, it's for a good reason. * I did not say the variable being static was "superior", I said they were advantages. * If you have a large array and want a linked list with pointers to the elements, then just create a `struct node` with a pointer. The linux-style intrusive linked-list allows both use cases. Dunning-Kruger shows again that people who believe they are proficient at reading technical articles are not as proficient as they think they are.