neoGFX C++ Code Naming Convention

From neoGFX
Jump to: navigation, search

The C++ code naming convention adopted by the neoGFX library is a derivative of the naming convention used in the now defunct mobile phone operating system Symbian OS. At first glance it may appear to be Hungarian Notation however Hungarian encodes type in the prefix whilst neoGFX encodes scope which is arguably more useful information to display when glancing at code to quickly understand intent.

The following table summarises the notation for various code elements:

Artefact Convention
Function name foo_bar_baz
Class name foo_bar_baz
Interface class name i_foo_bar_baz
Object/variable name i, x, foo, fooBarBaz
prefixFooBarBaz
Prefix Meaning
none Local object/variable
a Function argument
i Class member ("instance") object/variable
s Static object/variable
Enumeration name (enum class) foo_bar_baz
Enumeration name (plain enum) foo_bar_baz_e
Enumerator name FooBarBaz
Template parameter FooBarBaz
Macro FOO_BAR_BAZ

Example code:

enum class grade
{
	A, B, C, D, E, F
};

class student
{
private:
	typedef std::vector<grade> grades;
public:
	student(const std::string& aName) : iName{aName} {}
public:
	const std::string& name() const { return iName; }
	void add_grade(grade aGrade)
	{
		iGrades.push_back(aGrade);
	}
	grade average_grade() const
	{
		if (iGrades.empty())
			return grade::F;
		int total = 0;
		for (auto g : iGrades)
			total += static_cast<int>(g);
		return static_cast<grade>(total / iGrades.size());
	}
private:
	std::string iName;
	grades iGrades;
};

int main()
{
	student s{"Mr Flibble"};
	s.add_grade(grade::A);
	s.add_grade(grade::B);
	s.add_grade(grade::C);
	s.add_grade(grade::D);
	s.add_grade(grade::E);
	std::cout << s.name() << "'s average grade is " << (static_cast<char>(s.average_grade()) + 'A') << std::endl;
}