Adde posted a link to his(?) library and I took a look at it and wanted to comment on the coding style.
This is not a post polling everyone and their opinions, you're welcome to post if you have something constructive to say. If you use a style without understanding why, or without considering others that may improve your programming performance or skill, then please don't post. I don't want this to be a pissing contest, and neither do I care if every person in existance does it a different way.
Like what the other thread became, if all you have to say is "I use blah" don't bother, make a poll instead. Thanks.
Here is the same code:
Its a fairly simple class, its just a button after all. But there is a problem, he is allocating memory in his constructor, so if the constructor fails, that memory is never freed, ie the destructor is not called.
Personally I view accessors as the interface of the class, so I use what you would assume to be normal variable names for the accessors, in this case it would simply be myButton->state().
I don't particularily care for the m_/m notation, at least at the beginning of the name, since its the first thing you read and impacts readability, I use a trailing _ since it does not affect readability. But the real problem to me is that this is a programmer constructed device, and there is no method in which to enforce it, this goes for comments and a number of other things. Comments become out of date, often. While I think doxygen is great for open source, I would never use that internally at a company or on my own code, because once they are out of date they are no longer useful and can actually be harmful. And without turning the code into a bulky behemoth there is no simple way to version the documentation, whereas a seperate document can. Not to mention on a real project you may have someone suited for technical documentation (who is paid to write it, so its gets written).
There is a consistency problem with the BlitSurface method, I don't often use "using" but he has within a method but still used Frog::Color, I would assume that isn't needed. I originally viewed using as a tool to help update C code and keep it running, so never use it myself. In addition why call that method BlitSurface, when Blit is more consistent with the parent class?
As a side note I despise Hungarian notation. And its unfortunate that is bled from C into C++ where it shouldn't be needed. And because of its very nature should be ignored, its another programmer construct that doesn't offer any verification, so may or may not be valid. In addition the class writer should know what he is doing when he is modifing a class, so type information with a variable name gives him a false sense of confidence, when the person should look at the class definition anyway. When writing a button class, if my state is down or up, I know this and need no notation.
This type notation advances to other sorts as you see in the above example, the overuse of the word button, in a button class. To me thats like saying the the. I know, and you know, that it a button class, so naturally by context I figure that a surface is the button's surface. And the width and height are the button's width and height. For instance I imagine seeing this sort of code using your button class "exitButton->GetButtonWidth()", whereas to me it would be much clearer like "exitButton->width()". This is less a consideration of style than design, in the first example I read the the word word button twice. The operative words are get and width, acting on the name I would be more familar with (since I am writing it) exitButton.
This is not a post polling everyone and their opinions, you're welcome to post if you have something constructive to say. If you use a style without understanding why, or without considering others that may improve your programming performance or skill, then please don't post. I don't want this to be a pissing contest, and neither do I care if every person in existance does it a different way.
Like what the other thread became, if all you have to say is "I use blah" don't bother, make a poll instead. Thanks.
Here is the same code:
- Code:
namespace Pluto
{
Button::Button(Frog::DisplayDevice* display, const TCHAR* filename
,const int& button_width, const int& button_height) :
m_pDisplay(display)
, m_pButton(NULL)
, m_ButtonWidth(button_width)
, m_ButtonHeight(button_height)
, m_bIsDown(false)
, m_bState(false)
{
#if _WIN32_WCE
std::wstring fname = L"\\";
fname += std::wstring(filename);
m_pButton = Frog::LoadImage(m_pDisplay, fname.c_str());
#else
m_pButton = Frog::LoadImage(m_pDisplay, filename);
#endif
if (m_pButton == 0)
{
::MessageBox(0, _T("Unable to load button."), _T("Button Initialization Error:"), MB_OK);
}
}
Button::~Button()
{
delete m_pButton;
}
void Button::BlitSurface(const int& x, const int& y)
{
using namespace Frog;
// First, draw the button
if (m_bIsDown)
m_pDisplay->Blit(x, y, m_pButton, &Rect(m_ButtonWidth,0,2*m_ButtonWidth,m_ButtonHeight));
else
m_pDisplay->Blit(x, y, m_pButton, &Rect(0,0,m_ButtonWidth,m_ButtonHeight));
// Then, draw the border
m_pDisplay->DrawRect(x,y,x+m_ButtonWidth-1,y+m_ButtonHeight-1,Frog::Color(0,0,0));
// Finally, clear down flag
m_bIsDown = false;
}
void Button::PushDown()
{
// Flip/toggle state
m_bIsDown = true;
if (m_bState)
m_bState = false;
else
m_bState = true;
}
bool Button::GetState() const
{
return m_bState;
}
Its a fairly simple class, its just a button after all. But there is a problem, he is allocating memory in his constructor, so if the constructor fails, that memory is never freed, ie the destructor is not called.
Personally I view accessors as the interface of the class, so I use what you would assume to be normal variable names for the accessors, in this case it would simply be myButton->state().
I don't particularily care for the m_/m notation, at least at the beginning of the name, since its the first thing you read and impacts readability, I use a trailing _ since it does not affect readability. But the real problem to me is that this is a programmer constructed device, and there is no method in which to enforce it, this goes for comments and a number of other things. Comments become out of date, often. While I think doxygen is great for open source, I would never use that internally at a company or on my own code, because once they are out of date they are no longer useful and can actually be harmful. And without turning the code into a bulky behemoth there is no simple way to version the documentation, whereas a seperate document can. Not to mention on a real project you may have someone suited for technical documentation (who is paid to write it, so its gets written).
There is a consistency problem with the BlitSurface method, I don't often use "using" but he has within a method but still used Frog::Color, I would assume that isn't needed. I originally viewed using as a tool to help update C code and keep it running, so never use it myself. In addition why call that method BlitSurface, when Blit is more consistent with the parent class?
As a side note I despise Hungarian notation. And its unfortunate that is bled from C into C++ where it shouldn't be needed. And because of its very nature should be ignored, its another programmer construct that doesn't offer any verification, so may or may not be valid. In addition the class writer should know what he is doing when he is modifing a class, so type information with a variable name gives him a false sense of confidence, when the person should look at the class definition anyway. When writing a button class, if my state is down or up, I know this and need no notation.
This type notation advances to other sorts as you see in the above example, the overuse of the word button, in a button class. To me thats like saying the the. I know, and you know, that it a button class, so naturally by context I figure that a surface is the button's surface. And the width and height are the button's width and height. For instance I imagine seeing this sort of code using your button class "exitButton->GetButtonWidth()", whereas to me it would be much clearer like "exitButton->width()". This is less a consideration of style than design, in the first example I read the the word word button twice. The operative words are get and width, acting on the name I would be more familar with (since I am writing it) exitButton.