Welcome to my first blog on general programming techniques and object oriented software design patterns, part of my general programming series . I’m sharing these programming techniques and software design patterns with you in the hopes that it will enable you to write more readable, functional, re-usable and extensible code, saving you both time and money in software development.
This blog article is going to cover some standard basic class layout techniques that I use to keep classes neat and readable. Please note that class layout and design is a very subjective subject, so this may not be the most efficient or perfect way of laying out classes but I find that it works great for my needs.
All code outlined within my articles will be mainly based around the C++ language. I may include other languages now and again.
Beautiful Classes
When I design classes I tend to think about aesthetics as well as functionality. I like my class to basically look good and appear very readable
Here is the basic layout that I use for most of my classes:
class MyClass { // Section 1 - Public enums, typedefs and static data. //// Public Types public: typedef unsigned int u_int; //// Public Types End // Section 2 – Properties (Provides public access to class properties via getters and setters) //// Properties private: int ClassData1; bool ClassData2; public: void setClassData1(int data) { ClassData1 = data; } int getClassData1() const { return ClassData1; } void setClassData2(bool data) { ClassData1 = data; } int getClassData2() const { return ClassData1; } //// Properties End // Section 3 - Private / protected types, class data and methods (used by the class internally only) //// Class Internals private: bool PrivateClassData; //// Class Internals End // Section 4 - Construction / destruction and initialisation //// Initialisation public: MyClass() : ClassData1(0), ClassData2(false), private_class_data(false) {} ~MyClass() {} bool Init(); void Release(); //// Initialisation End // Section 5 - Public access class functionality //// Public Access public: void DoSomething1(); void DoSomething2(); //// Public Access End };
I usually split my classes into 5 sections:
- Section 1 – Public enums, typedefs and static data
- Section 2 – Properties provide public access to class private data via getters and setters. A property can be thought of as a private class variable that you want to allow the outside world to change but in a safe manner.
- Section 3 – Private / protected types, class data and methods (used by the class internally only)
- Section 4 – Construction / destruction and initialisation. Note that I usually like to move all class initialisation code out of the constructor and place it into my class Init() method, same goes for the destructor and the Release() method. This allows me to re-initialise and tear down a class without having to actually new or delete it. This can prove very useful in many situations such as object pooling to reduce memory fragmentation, where the idea is to preserve the class and re-use it.
- Section 5 – Public access class functionality
I find that this kind of system helps me to determine where to quickly look inside my classes to find what I need.