At every company I've ever worked, curly braces were mandatory after if/elif/else statements and loops specifically because of #8.
My standard approach is to only ever omit the braces when the entire if statement fits on a single line. I also enforce a hard 80-char limit on my lines, which prevents a single enormous line.
Ways to get screwed by C
11–20 of 36 posts
Re: Ways to get screwed by C
#12Boohoo. I mean it. I don't think they even make compilers that don't warn about the top 2 issues, and 5 for sure. And after that it gets down to "I did something stupid and something stupid happened." I mean: int ii = i/++i; Seriously? As if defining the order of operations would magically take the suck out of that statement. There are like 1.5 nuggets of real pain in here. (1 point for returning a stack array, 0.5 p…
My favorite example is from #19, which is something that javascript programmers do all of the time.
int value = a && b && fn(a->x,b->x);
(The author goes on to complain about how value you obviously be whatever fn(a->x, b->x) returns).In javascript, that's actually good practice (I guess, I had a mentor once who encouraged it, but I never really bought into it). To a C programmer though, that just looks gross. (Or at least to me, and I like to pretend to be a C programmer).
Re: Ways to get screwed by C
#13Re: Ways to get screwed by C
#14My employer had code that ran on about 5 billion different platforms, and my job was to port it to a new one. Everything went fine, except for the weird random crashes that would happen periodically. The idea that a "char" could be an "unsigned char" by default was so far off my radar screen that I didn't figure out what was going on until a few days later when I reluctantly dived into the assembly. (It didn't help that it was a mobile platform with basically 0 support for gdb).
Turned out to be a 3 second fix - pass "-fsigned-char" to gcc. Nowadays in new code I always explicitly declare whether my chars are signed or unsigned.
Re: Ways to get screwed by C
#15 struct thing {
int x;
} aThing;
int x;
aThing,x = 3;
Do you see it?EDIT: fix typo
Re: Ways to get screwed by C
#16I liked
int a = 2 && 4 && 8; // what is the value of "a" ? would you belive a=1 ?
I'm not sure the author has the necessary qualifications to condemn C's "poor design."Re: Ways to get screwed by C
#17for(i=0;isome code here/ }
Re: Ways to get screwed by C
#18Seriously? I think this page reflects more on the person who wrote it than on C. A top 10 list with 19 items? Genius! Forgetting to include a tag on item #6? Classic sign of a sloppy programmer.
Re: Ways to get screwed by C
#19Or use "gcc -Werror" when compiling.
C is like a sharp knife: in skilled hands, it can do wonders. Unskilled hands end up missing a finger or two.
Re: Ways to get screwed by C
#20lint(1) [or splint(1)] is your friend. Or use "gcc -Werror" when compiling. C is like a sharp knife: in skilled hands, it can do wonders. Unskilled hands end up missing a finger or two.