r/C_Programming • u/wizards_tower • Sep 09 '20
Discussion Bad habits from K&R?
I've seen some people claim that the K&R book can cause bad habits. I've been working through the book (second edition) and I'm on the last chapter. One thing I noticed is that for the sake of brevity in the code, they don't always error check. And many malloc calls don't get NULL checks.
What are some of the bad habits you guys have noticed in the book?
60
Upvotes
28
u/Chillbrosaurus_Rex Sep 09 '20 edited Sep 09 '20
One thing I noticed is the book seemed to enjoy using little tricks like variable instantiation evaluating as an expression:
if (x=readInput()) {...}
Instead of a separate null check.
I can't help but feel modern practice tries to be a little more explicit with their code than 80's coding culture promoted, maybe because compilers are far better at optimizing now than they were then?
Edit: Several commentators have pointed out that there are many situations where this idiom promotes readability and saves vertical space. I'll defer to their wisdom. I don't have enough experience to say this is a bad habit, it was just something that looked off to me, reading the book.
7
u/[deleted] Sep 09 '20 edited Sep 09 '20
Nah, you're mostly fine, just need to keep a couple of things in mind:
while (*str++)loops, use library functions, K&R assumes ASCII or EBCDIC, Unicode breaks these assumptions37
u/SamGauths23 Sep 09 '20
Honestly error checks are kinda like leaving comments. Everybody claims to do error checks but when you look at other people's code they rarely do it
9
u/TheBB Sep 09 '20
They like code golf expressions with post- and preincrement operators. Takes me more than a moment to figure out the order of evaluation sometimes.
4
u/bumblebritches57 Sep 09 '20 edited Sep 10 '20
ALWAYS name your parameters, and name them well.
The standard libraries biggest flaw is how function signatures only show the types it takes, or if it does give names, they're the most generic shit ever.
While we're talking about the standard, don't name your function an abbreviation of something like atoi for example, sure you can work out what it means, but ASCII2Integer, or even String2Integer (or To if you don't like using numbers in functions for whatever reason) is objectively easier to read.
6
u/EighthDayOfficial Sep 09 '20 edited Sep 09 '20
The worst habit it teaches is how to code in C, zing!
For real though, if you are getting failed malloc calls, there is something else wrong. Probably a memory leak and where it crashes won't tell you much.
If you are a pro, then by all means, do it right. Keep in mind, I am an am amateur but I have been programming on and off since 1994.
If you are a hobbyist trying to make a game (like me), then you literally do not have to worry about that stuff.
I started out my project about 3 years ago and initially did error checks on everything. Checked for invalid inputs, etc.
Then I just said, you know what, if this code ends up in someone else's hands and they can't work with it, that is a problem I'd like to have. That would mean the game was fun and had success.
Id rather have that problem than "well commented and checked code, boring game." I can pay someone to fix poorly documented and checked code, I can't pay someone to fix a boring game.
If you are working on a hobby project, the rules are completely different. Its "move fast and break things" because you are rushing against your "boredom clock."
My codebase is now at 157,000 lines of code and growing, and a major reason I keep up with it is I gave up "well documented" on the agile triangle (the other two being "on time" and "all the planned features work."
I'd say the biggest change from K&R is that today it is more common place to have really good self commenting code. You can make your function names so descriptive that minimal comments are necessary. As I recall, in the 1970s and 1980s you couldn't have variables and function names that are as long as today (could be wrong on that).
2
u/[deleted] Sep 09 '20 edited Sep 14 '20
One I’ve heard anecdotally (not a habit per-se) is thinking that the hashing function on page 144 is a good enough hash function to use. It really isn’t!
I’d add not using explicit types (e.g.
unsignedabove instead ofunsigned int) as a bad habit. Some people also dislike the use of for/if/while etc. without braces.3
u/[deleted] Sep 09 '20
it tends to have short names imo
and I'm not a fan of their bracing
if(x){
}
I like having everything on new lines, easier to debug
if(x)
{
}
1
u/BlindTreeFrog Sep 09 '20
The coding style in K&R was to save cost of printing. Things are consolidated onto the fewest lines possible so the code takes up less space.
They didn't write it as a style guide, but as a language guide. Just don't copy their style.
-4
u/Orlha Sep 09 '20
Checking malloc calls for Null is rarely useful.
0
u/[deleted] Sep 09 '20
[deleted] — view deleted comment