Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualThe King2

Posted 03 April 2013 - 10:41 AM

I'm not a big fan of #defines, as far as there are better alternatives really. It would work for normal enums so thats not the problem. I choose to go with strongly typed enums for multiple reasons:

 

- It makes code much more clear. Consider this:

 

void Cursor::ChangeState(CursorState newState)
{
	m_state = newState;
	switch(m_state)
	{
	case CursorState::IDLE:
		m_sFileName = L"CursorIdle.png";
		m_offsetX = 0;
		m_offsetY = 0;
		break;
	case CursorState::DRAG_V:
		m_sFileName = L"CursorDragV.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_H:
		m_sFileName = L"CursorDragH.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_LD:
		m_sFileName = L"CursorDragLD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_RD:
		m_sFileName = L"CursorDragRD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	}
}

over this:

void Cursor::ChangeState(UINT newState)
{
	m_state = newState;
	switch(m_state)
	{
	case IDLE:
		m_sFileName = L"CursorIdle.png";
		m_offsetX = 0;
		m_offsetY = 0;
		break;
	case DRAG_V:
		m_sFileName = L"CursorDragV.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_H:
		m_sFileName = L"CursorDragH.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_LD:
		m_sFileName = L"CursorDragLD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_RD:
		m_sFileName = L"CursorDragRD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	}
}

Its not something that absolutely needs to be, but I think in the first code it everything is much clearer, extending to how the m_state variable is declared (CursorState instead of UINT) etc...

 

- I won't possibly ever run in a name clash for these enums.

 

- There is no possibility to asign a wrong value. Thing is, that like you said, the old code works regardeles of the actual type used, I don't like that very much. I can pass in My NoResize - flag from the WindowStyleFlags to the CursorState, which isn't possible with strongly typed enums.

 

Over all I just tend to prefern them after I read about them some while ago, I just decided to implement them tody. Surely the old code works, and I wouldn't need them, the "enum class" makes things just more clean, in my opinion, and my goal after getting code to work is now to polish it up... except for that bitwise-thingy, that is :/


#1The King2

Posted 03 April 2013 - 10:41 AM

I'm not a big fan of #defines, as far as there are better alternatives really. It would work for normal enums so thats not the problem. I choose to go with strongly typed enums for multiple reasons:

 

- It makes code much more clear. Consider this:

 

void Cursor::ChangeState(CursorState newState)
{
	m_state = newState;
	switch(m_state)
	{
	case CursorState::IDLE:
		m_sFileName = L"CursorIdle.png";
		m_offsetX = 0;
		m_offsetY = 0;
		break;
	case CursorState::DRAG_V:
		m_sFileName = L"CursorDragV.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_H:
		m_sFileName = L"CursorDragH.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_LD:
		m_sFileName = L"CursorDragLD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case CursorState::DRAG_RD:
		m_sFileName = L"CursorDragRD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	}
}

over this:

void Cursor::ChangeState(UINT newState)
{
	m_state = newState;
	switch(m_state)
	{
	case IDLE:
		m_sFileName = L"CursorIdle.png";
		m_offsetX = 0;
		m_offsetY = 0;
		break;
	case DRAG_V:
		m_sFileName = L"CursorDragV.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_H:
		m_sFileName = L"CursorDragH.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_LD:
		m_sFileName = L"CursorDragLD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	case DRAG_RD:
		m_sFileName = L"CursorDragRD.png";
		m_offsetX = 16;
		m_offsetY = 16;
		break;
	}
}

Its not something that absolutely needs to be, but I think in the first code it everything is much clearer, extending to how the m_state variable is declared (CursorState instead of UINT) etc...

 

- I won't possibly ever run in a name clash for these enums.

 

- There is no possibility to asign a wrong value. Thing is, that like you said, the old code works regardeles of the actual type used, I don't like that very much. I can pass in My NoResize - flag from the WindowStyleFlags to the CursorState, which isn't possible with strongly typed enums.

 

Over all I just tend to prefern them after I read about them some while ago, I just decided to implement them tody. Surely the old code works, the "enum class" makes things just more clean, in my opinion.. except for that bitwise-thingy, that is :/


PARTNERS