neoGFX C++ Code Naming Convention

From neoGFX
Revision as of 22:19, 12 November 2019 by Leigh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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
Prefix Meaning
none Local object/variable
a Function argument
i Class member ("instance") object/variable
s Static object/variable
Property FooBarBaz
Enumeration name (enum class) foo_bar_baz
Enumeration name (plain enum) foo_bar_baz_e
Enumerator name FooBarBaz
Template parameter FooBarBaz

* In neoGFX parlance an "interface" is a class that contains pure virtual functions and inline non-virtual helper functions only and it can only be derived from other interface classes. Interfaces are a good thing (even in C++), see SOLID for why.

Example code:

enum class grade
	A, B, C, D, E, F

class student
	typedef std::vector<grade> grades;
	student(const std::string& aName) : iName{aName} {}
	const std::string& name() const { return iName; }
	void add_grade(grade 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());
	std::string iName;
	grades iGrades;

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