White-Gold

Welcome to the ‘white-gold’ blog of Head First Design Patterns in C++. It is called ‘white-gold’, an alloy of ‘silver’ and ‘gold’ because it is just that, a combination of the ‘silver’ and (forthcoming) ‘gold’ version. I am not satisfied with the ‘silver’ version and I intend to replace it with this one once it has been vetted. As far as the ‘gold’ version, well let’s just say I’m still deciding what that will be. In the mean time I’ll concentrate on this version. I will begin by introducing primitive classes that will form the foundation of ‘Objectville’; a library of artifacts from the book and some that are not.

But first, some ground-work, disclaimer and rules-of-engagement; I do not claim to be a C++ expert; rather, a disciple of design patterns and object-oriented programming. If you are reading this, then you too are on a journey through a jungle of design patterns, practices, principles and idioms, and if you will endulge me for a moment, strangely reminds me of the Disney Jungle Cruise ride. The Jungle Cruise is of course, a whimsical and controlled boat ride through the world’s most exotic rivers. Along the way, the captain makes pun-y remarks about various audio-animatronics systematically positioned on its predetermined route. For a short time, passengers surrender their conception of reality and enjoy the ride; they seem to embrace and enjoy the suggestions, between conventional wisdom and artistic expression. At the end of the ride, when the boat returns to the dock, the captain says his final goodbye…

“If you enjoyed the ride, my name is Glenn and this has been the world-famous Jungle Cruise.
And if you had a lousy ride… well, then my name is Ryan and this has been Space Mountain.”

Whimsically, of course, the jungle cruise is Head First Design Patterns in C++. Seriously, of course, if you find errors, omissions or you think I completely missed the boat, please let me know. If you find the coding-style (explained below) or any other syntactical-sugar nauseating, well, then your going to have a lousy ride. ;-)

First stop, The seemingly redundant access modifiers (public, protected & private). Being explicit as possible is desirable — even at the cost of a few keystrokes. Most apparent is the adoption of Java and C# access modifier syntax.

For example:

Class Foo

{

public: Foo();

public: ~Foo();

private: int _Bar;

private: void Bar() { }

}

Feedback has suggested that this syntax looks verbose. Yep; however, there is a certain quality about a self-contained definition; one does not have to scan up the class definition looking for its access modifier block. If the code is terse, block style, access modifiers are effective; however, if the code is verbose, contains  documentation or in the case of templates, contains the implementation of a method as well, block modifiers are less effective; their definition is not in close proximity to the members definition. The intent is to be as explicit as possible without sacrificing clarity. It is a style choice that I have grown to appreciate in practice. I never understood the complaint of a few extra keystrokes at the expense of clarity. It is probably a cultural carry-over of the C axiom of not paying for what you do not use; apparently, this extends to writing code as well. Frankly, I don’t care how long it takes a compiler to parse my code! I care how long it takes someone to understand and maintain my code!

“Programs must be written for people to read, and only incidentally for machines to execute.”
~ Hal Abelson, Structure and Interpretation of Computer Programs, Second Edition

Next stop is the indentation style of method declarations, whereby the method name appears on a subsequent line.

For example:

public: const std::type_info&

get_type_info() const

{

return get_type_info_impl();

}

Here you see the declaration of ‘public’ and return type appear on the first line, whereas, the name of the method appears on the subsequent line. This is in contrast to the common convention of placing everything on a single line:

For example:

public: const std::type_info& get_type_info() const

{

return get_type_info_impl();

}

It is a matter of style and esthetics, the experience C++ developer might object; however, and because I adopted the STL naming convention, I find it tedious to visually scan this line, picking out the method name from its declarations. It is argued that adopting a camelCase convention would make it easier; however, the intent of this library is to demonstrate design patterns, not debate naming conventions — this, like many things, is a style choice, I like it because it makes it easier (for me) to pick out the method name and it keeps the line shorter, especially if you have many parameters. Where the curly brace ‘{‘ is positioned is syntactical-sugar and irrelevant (to me) as long as it is consistent.

Next stop, and possibly the most controversial transgression, I combine method declaration and definitions in a single (.hpp) file, giving ‘true’ meaning to the extension ‘.hpp’ — it is a header that contains code. I do this for convenience to the reader who is trying to understand the pattern or axiom; I am not trying to build an industrial-strength enterprise library. Therefore, I hereby absolve myself from any transgression of including code in headers, even though templates require you to do so. ;-)

You will find the word ‘Idiom’ throughout the code-base. These phrases are not mine, they came from one of several sources; however, to avoid verbose citations, code clutter and the possibility that they exist in more than one source, I adopted for the solitary word: ‘Idiom’ and list any/all possible sources here:

Effective STL [Scott Meyers]

Effective C++ Third Addition [Scott Meyers]

More Effective C++ [Scott Meyers]

Modern C++ Design [Andrei Alexandrescu]

C++ Coding Standards [Herb Sutter, Andrei Alexandrescu]

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: