Share a few lines of code that exemplify your programming style. Share innovations.
I'll start.
void UpdateAI()
{
CPlayer* p;
for(int i=0; i<PLAYERS; i++)
{
p = &g_player;
if(!p->on)
continue;
if(!p->ai)
continue;
UpdateAI(p);
}
}
Features:
- Use pointers with one, first letter of word of type (CPlayer* p, CUnit* u, CUnitType* t, CCamera* c, CClient* c, CEntity* e, CPacket* p) or purpose (position, radius)
Another example:
bool CollidesWithUnits(float x, float z, float hwx, float hwz, bool isUnit, CUnit* thisU, CUnit* ignore, bool checkPass)
{
CUnit* u;
CUnitType* t;
float r;
CVector3 p;
CCamera* c;
for(int i=0; i<UNITS; i++)
{
u = &g_unit; // <--
if(!u->on)
continue;
if(u == ignore)
continue;
if(u == thisU)
continue;
t = &g_unitType[u->type]; // <--
//...
c = &u->camera; // <--
p = c->Position(); // <--
r = t->radius; // <--
//...
}
return false;
}
- Multiple levels of pointers retrieving something from other pointers
Instead of nesting if-statements,
bool CWidget::DropDown_prelbuttonup()
{
if(ldown)
{
ldown = false;
if(opened)
{
if(mousescroll)
{
mousescroll = false;
return true; // intercept mouse event
}
for(...)
{
// list item?
if(...)
{
//....
opened = false;
return true; // intercept mouse event
}
}
// up button?
if(...)
{
//...
return true;
}
// down button?
if(...)
{
//...
return true;
}
opened = false;
return true; // intercept mouse event
}
return false;
}
return false;
}
I try to write
if(!...)
return;
whenever I can, so that it looks like this:
bool CWidget::DropDown_prelbuttonup()
{
if(!ldown)
return false;
ldown = false;
if(!opened)
return false;
if(mousescroll)
{
mousescroll = false;
return true; // intercept mouse event
}
for(...)
{
// list item?
if(...)
{
//....
opened = false;
return true; // intercept mouse event
}
}
// up button?
if(...)
{
//...
return true;
}
// down button?
if(...)
{
//...
return true;
}
opened = false;
return true; // intercept mouse event
}
This was a big paradigm shift for me, when I first thought of this. But now I do it all the time.