When I talk about preaching, and I'm afraid you'll have to forgive me out there in the world, I think about people standing in a pulpit telling other people to do something (be that believe in a god, follow some ethos or buy a certain brand of snake oil) which is not particularly in their best interest.
In software I think there are many source of miss-information, which try to guide but result in causing harm, I'm sure many of my own posts are of this exact bent, but at least I warn you I'm trying to meddle (its in the very name of my blog) and I do also warn I'm a bit of a ranter...
But, the particular preaching I'm going to cast down today is the kind in print, and by supposedly high standing persons in C++ circles, I'm going to talk about a book I really hate... C++ Coding Standards by Herb Sutter & Andrei Alexandrescu.
I really don't like this book, I say as much when I reviewed it on the Amazon site, but it seems I am in a minority. Many people love and dote on this book, they take its lessons to heart, and there are a few good lessons in there, but the more I force myself to read the book the more negative items I find within.
Not least the book itself does not follow its own preaching, how many ministers have we caught taking a tipple themselves after advising others to avoid the bottle?... Well, this book is that exact bloody thing, here's a good example...
"Don't use Macro's"
"Define your own Assert Macro"...
This is, so annoying... Then the assert Macro section itself... Page 130-131 of this damn book... they given an example, and I like to think in a book then the author thinks to give an example they give a good and poignant example. But they don't... Look at this:
"For example, your whole design might rely on every Employee having a nonzero id_."
Right, so a none zero value to a variable, and they carry on to say:
"you can assert (id_ != 0) inside the implementation of Employee wherever you need to make sure an Employee is sane:
unsigned int Employee::GetID() {
assert ( id_ != 0 && "Employee ID is invalid (must be nonzero)" );
return id_;
}"
Right, people read this, and they think, yes... Yes I can do this... But you know what, its utter rubbish, this book is about C++, we can all go look at the C++ type and there's a much easier way to assert that the id_ here has a positive only value, and not only that but they do it!!.... Just the value a bloody unsigned. Then it can't ever be a negative, its unsigned by definition that means 0 and above! Gah, here's how I'd do it too...
class Employee
{
private:
unsigned int id_;
public:
const unsigned int GetID() { return id_; }
};
And my code is a lot better, for example, I can choose to make the value _id public, as the type enforces the usage for us, we can also make our return type constant, as the ID of am employee might very well stay the same so we can track them down.
And this code of mine is not just created off the top of my head on a whim (it was, but read on) no, I was guided by reading a much better primer into better code by Scott Meyer, yet it seems more people I run into put more stock into Herb & Andrei's stuff than Scott's, and I write this here post to put that to rights.
Go read Scott Meyer's books for a better insight into why I gave my counter example the way I did.
No comments:
Post a Comment