From 665e7a3cc49c1eb85c2fb19c9c56b830475aee9b Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 21 May 2015 08:25:45 -0600 Subject: [PATCH] Describe reasoning behind m_ and t_ prefixes --- 03-Style.md | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/03-Style.md b/03-Style.md index 5b01faa..d208d87 100644 --- a/03-Style.md +++ b/03-Style.md @@ -22,11 +22,41 @@ C++ Standard Library (and other well known C++ libraries like boost) use these g ## Distinguish Private Object Data -Name private data with a `m_` prefix to distinguish it from public data. +Name private data with a `m_` prefix to distinguish it from public data. `m_` stands for "member" data ## Distinguish Function Parameters -Name function parameters with an `t_` prefix. +Name function parameters with an `t_` prefix. `t_` can be thought of as "the," but the meaning is arbitrary. The point is to distinguish function parameters from other variables in scope while giving us a consistent naming strategy. + +By using `t_` for parameters and `m_` for module data, we can have consistency with both public members of structs and private members of classes. + +Any prefix or postfix can be chosen for your organization. This is just one example. + +``` +struct Size +{ + int width; + int height; + + ValueType(int t_width, int t_height) : width(t_width), height(t_height) {} +}; + +// this version might make sense for thread safety or something, +// but more to the point, sometimes we need to hide data, sometimes we don't +class PrivateSize +{ + public: + int width() const { return m_width; } + int height() const { return m_height; } + ValueType(int t_width, int t_height) : m_width(t_width), m_height(t_height) {} + + private: + int m_width; + int m_height; +}; +``` + + ## Don't name anything starting with an `_`