4 ms·
C is an extremely simple language. The problem demonstrated here is that sometimes people like to show off and write nonsensical code. Let's consider the exampl
by perryizgr8 5y ago
C is an extremely simple language. The problem demonstrated here is that sometimes people like to show off and write nonsensical code. Let's consider the examples given.
1. This is straight up unacceptable in any reasonable shop. You must initialise variables before use. This will be flagged at the static analysis step inside the CI/CD pipeline, and your code won't make it to production.
2. sizeof returns a size_t, not an int. This is basic knowledge. Just make it a habit never to perform arithmetic on size_t without casting. This will also probably fail in static analysis.
3. Don't use the same names for globals and locals. Make some kind of rule where globals always have to start with g_. Solved.
4. Just don't do this. I guarantee that you will never need to do strange things like this. As I said, C's problems appear when people are showing off.
5. Please just make two structs, and put one inside the other. Don't do this. It is never required.
I am not going to go on now. It just keeps getting more unnecessary and more ridiculous. There is absolutely no situation that requires you to write C code like this. IMO if you want to show off how smart you are, write code that is obvious at first glance to a kid just out of college. Writing useful code that is easily understandable is a real art, and demonstrates true expertise with the language.
- SloopJon 5y agoThe context here is a tool that has to handle everything, so you won't necessarily run into all of these in your run-of-the-mill project. However, the author does claim: > The following examples were actually encountered either in real programs or are taken from the ISO C99 standard or from the GCC’s testcases. I have definitely seen (and probably even written) show-off code in C++ and other languages, but when it comes to C, I feel like the tricky stuff tends to be borne of necessity. For example, I test a code base that requires some type punning. The last time I investigated this, there didn't seem to be consensus on how to do this without UB. Signed overflow is another bugbear in C. Code that worked perfectly fine for over twenty years was suddenly at the center of mysterious bugs when a new version of GCC decided to exploit some UB.