Expand Your Code

2008-01-27

I'm not familiar with more than a few debuggers, but, the ones I use generally have an issue with single line member functions.

class MyClass
{
public:
   int  GetSomeValue()      { return m_someValue; }
   void SetSomeValue(int v) { m_someValue = v; }
private:
   const char* m_id;
   int         m_someValue;
};

If the function is on a single line they will let you put a breakpoint there but they will not let you inspect variables. This makes it extremely hard to debug. For example if you put a breakpoint on GetSomeValue() above and you try to inspect this−>m_id or this−>m_someValue the debugger will show you the wrong value. This makes it impossible if you want to know if the class instance you are in is the correct one. You have no way to do it since the debugger will show the wrong thing.

The solution is to expand your code so it's not all on one line.

class MyClass
{
public:
   int  GetSomeValue()
   {
	   return m_someValue;
   }
   void SetSomeValue(int v)
   {
	   m_someValue = v;
   }
private:
   const char* m_id;
   int         m_someValue;
};

The problem is most programmers don't like that to expand their code. They find the second style less organized and bloated. Another way to do it is like this

class MyClass
{
public:
   int  GetSomeValue();
   void SetSomeValue(int v);
private:
   const char* m_id;
   int         m_someValue;
};

inline int MyClass::GetSomeValue()
{
   return m_someValue;
}

inline void MyClass::SetSomeValue(int v)
{
   m_someValue = v;
}

Whether that looks better or not is up for debate. The class declaration itself is left uncluttered with code with the code moved below.

My feeling is it's more important to be pragmatic so either 2 or 3 is better than 1 since style 1 actually makes debugging harder. Besides, if I run into a bug and while debugging I need to inspect something in a single line inline function I'm going to end up reformating it so I can debug through it. So, I just generally choose style 3 to save myself from frustation later.

Comments
Code Test
Magic Highway