SQLServerCentral Editorial

Model Good Behavior

,

I'm answering a question in the forums and I spot something that crawls up my spine:

;WITH...

The person was using a Common Table Expression (CTE) which requires that the preceding statement in the batch have a statement terminator, the semi-colon. However, since the terminator isn't required everywhere, lots of people don't use it at all. And then, when it comes to writing a CTE, for convenience, they stick the terminator, you know, the thing that goes at the end of something, at the beginning of the CTE definition. Only, this time, they were trying to use two CTEs and had the terminator in there twice, obviously breaking the code.

Why would they think they're supposed to put that semi-colon at the front of the CTE? Because it's been modeled to them that way. For convenience, and repeatability, and just to reduce the number of complaints, a lot of people posting example code, put the terminator in front of their WITH clause. I get it. Those are reasons to model, what is actually a poor practice, as a way to make things easier. Same with using SELECT * in example code. You also see a lot of example code with all sorts of other bad behaviors because, hey, it's just an example. You also see it in other kinds of code, where the "Hello World" example, is poorly structured and badly written, but, hey, it's just an example.

And then we wonder why so many people are writing so much bad code. Well, we're teaching them to write bad code.

Look, if we want people to do things correctly, we need to model that good behavior. It's how you teach. Do the things you know to be right, all the time, in order to show others that's how they should be getting stuff done. Is it inconvenient to remove SELECT * from your example code? Good gosh yes. I'm actively rewriting one of my books and I'm throwing out almost every instance of SELECT * (except a couple where I use it on purpose to illustrate a point). It's work. However, it's necessary. If I'm going to teach people how to do things, I need to model good behavior, from the start, and all the way through. Even if it inconveniences me (and yeah, worth mentioning, I was dragged to this point of view in part by Hugo Kornellis, I'll let him tell the story).

OK. You can start flaming me now.

Rate

5 (3)

Share

Share

Rate

5 (3)