April 2017
Beginner to intermediate
394 pages
9h 16m
English
Hard coding number literals into code is generally considered a bad idea. The problem with using a number literal instead of a named constant is that the purpose of that number is unknown to the reader. The number appears in code as if by magic. Consider the following code:
M5Object* pUfo = M5ObjectManager::CreateObject(AT_Ufo); pUfo->pos.x = M5Random::GetFloat(-100, 100); pUfo->pos.y = M5Random::GetFloat(-60, 60);
It is hard to know why those four numbers were chosen. It is also hard to know how the program will be changed if the values are modified. Code like this would be much more readable and maintainable if the named constants or variables were used:
M5Object* pUfo = M5ObjectManager::CreateObject(AT_Ufo); pUfo->pos.x ...
Read now
Unlock full access