Transcript from the "Bad Style" Lesson
>> Douglas: So this is the last thing I'm going to complain about Thompson. And again, I have to appreciate Thompson, Thompson gave us Unix, Thompson gave us B, which led to all of our modern languages. Thompson gave us regular expressions, Thompson gave us UTF-8, which I think is one of the smartest things I've ever seen.
[00:00:24] Thompson has given us some amazing gifts, and as professional programmers, we all owe a tremendous amount to Thompson. Unfortunately, Thompson had terrible taste in programming languages, in programming language design, and even worse than that, he was extremely influential. So all of us have been reading Thompson's designs from our first hours of programming.
[00:00:48] And we've been doing it so long, it all looks right to us, we cannot see how bad this stuff is and how much it compromises us. So for example, the ++ operator, this was added to be for doing pointer arithmetic, and we have since determined that pointer arithmetic is harmful.
[00:01:09] So we don't do it any more, modern languages do not support pointer arithmetic. The last popular language to have pointer arithmetic was C++, a language so bad, it was named after this operator.
>> Speaker 2: [LAUGH]
>> Douglas: But the operator refused to die, it's still in all of our languages, even though we don't need it to increment pointers anymore.
[00:01:32] So now we use it to add one to variables, which is completely unnecessary, but we do it. And unfortunately, it leads to a style of programming in which you try to write one liners, in which you try to take a whole bunch of stuff and try to smush it down into one line.
[00:01:49] And that leads to really bad code, stuff which is very hard to maintain, very hard to correct. We've seen security errors, buffer overruns, those sorts of things, this operator is always implicated in those sorts of security errors. And I found in my own practice, any time I use ++ anywhere, this thing takes hold of me, and I can't control it, it makes me wanna take code and try to mush it down to one line.
[00:02:15] Even though I know that's a stupid thing to do, I can't control myself, this thing takes hold of me, and I start writing really stupid stuff, thinking I'm being really smart. So eventually, I had to stop because I couldn't do it a little bit. I had to stop completely.
[00:02:31] So I said no more ++, from now on, it's +=1, and I can relax, and it's easy. I can just write good programs now.
>> Douglas: Food tastes better, it's just, everything's great. And for a while, I thought it was just me, but now I'm recommending everybody +=1 all the time, every time, +=1, it's great, so much better.
[00:02:57] But I hear from people all the time, saying but I wanna be able to write x++ cuz it means the same thing, and instead of having to go, and I can't go, I don't have that kind of time. Except that the typing time is irrelevant, it is completely irrelevant, we don't spend our time typing.
[00:03:21] But more than that, it's ++x, which means about the same thing as x+=1. So any time I see someone writing x++ in increment position, I have to ask, does this clown understand the difference between pre-increment and post-increment? And it means I have to look at every ++ in his program and ask did he get this one right?
[00:03:42] Did he get this one right? Cuz it's a little dyslexic thing, which it's really hard to tell when you've got them reversed, and it causes an off by one area that's only off for an instant. But that's enough to cause a bug, and it's really hard to debug those things.
[00:03:58] The argument in favor of ++ is that it improves readability, which is bullshit. It does not improve readability, it improves ambiguity, it improves confusion, which are things which are not desirable. So I was reviewing some code and I saw ++x, ++x, so what's going on there? So it's possible that it was a copy and paste error, except the code seemed to be working, so more likely, what had happened was someone had done a ++x and then someone else noticed, there's an off by one problem here.
[00:04:30] So did it again, if the original code said +=1, then the obvious solution would be += 2, right? And it raises the question, why do we think we need completely different syntax for adding one to a variable, then every other value? How does that make any sense? The answer is, it does not make any sense, but there is this emotional attachment we have to bad grammar in our languages that makes us feel ++ is that's who I am, it's part of who I am.
[00:05:05] If you take ++ away from me, what am I? What's left? Wah.
>> Speaker 2: [LAUGH]
>> Douglas: Wah. So for no cost, by adopting a more rigorous style, many classes of errors can be automatically avoided. So it occurred to me, so who's writing with bad style? So I came up with four classes of bad stylists, and I have to confess at various times in my career, I have been all of these people.
[00:05:55] And nobody ever told these people, you can do this stuff well, right? They've never seen good examples, it's just sad. Then there is old school, people who are extremely skilled and experienced in one particular language, and now circumstances force them to work in another one, where they don't have that same knowledge or experience.
[00:06:31] That's principle.
>> Speaker 2: [LAUGH]
[00:06:50] And then there are the thrill seekers. These are guys who found out, hey, did you know you can put your semicolon at the beginning of a statement instead of at the end, and sometimes it works? Wah!
>> Speaker 2: [LAUGH]
>> Douglas: And so they just write this stupid looking crap just because.
[00:07:05] I think people like that should have to wear helmets when they're coding. And finally, the exhibitionist, these are people who will study the standard, study the implementations, find all the weird corner cases, and there are a lot of weird, bizarre corner cases in this language. And intentionally use them in everything that they write, stuff where you look at and go, what the hell's that supposed to mean?
[00:07:30] And they're proud of it. They go, look at that, you have no idea. But they're doing that, and they'll say that was intentional, I know what I'm doing. And I say no, if you knew what you were doing, you would not be doing that.