Society of Robots - Robot Forum

Software => Software => Topic started by: o0tsubasa0o on March 28, 2010, 09:01:24 PM

Title: Need help with understanding the line of pogramming code
Post by: o0tsubasa0o on March 28, 2010, 09:01:24 PM
I need help in understanding what the number (3 and 5) means in the code below. Can anyone help me out ? thx .

The code is a part of a SUMO BOT porgramming using Silicon Laboratories .

AI_Opto_FR : distance sensor Front Right
AI_Opto_FL : distance sensor Front Left
AI_Opto_Frnt : distance sensor Front
AI_Opto_SFL : distance sensor Side Front Left
AI_Opto_SFL : distance sensor Side Back Left
AI_Opto_BK : distance sensor Back

wait_ms(EnemyChkTm);
   if ((AI_Opto_FR >= 3 && AI_Opto_FL >= 3 ) || (AI_Opto_FR >= 3 && DI_Opto_Frnt) || (AI_Opto_FL >= 3 && DI_Opto_Frnt) || AI_Opto_SFL >= 5 || AI_Opto_SBL >= 5 || DI_Opto_Bk)
   {
      MOVE(15,15);
      wait_ms(StopTm);
      Enemy_Locked = 1;   
      break;
   }
}
Title: Re: Need help with understanding the line of pogramming code
Post by: waltr on March 29, 2010, 07:32:16 AM
They are just numerical constants for the comparison.
This fragment:
   if (AI_Opto_FR >= 3)
says:
 if (AI_Opto is greater than or equal to 3)

Now, where does the AI_Opto_FR value come from?
Title: Re: Need help with understanding the line of pogramming code
Post by: Soeren on March 29, 2010, 12:39:26 PM
Hi,

I need help in understanding what the number (3 and 5) means in the code below.
They're so called "magic numbers", which is numbers that shows up in a piece of code without any explanation.
Magic numbers are considered very bad programming practise, as it fogs the purpose of the code.

The right way to do it, is to define them as constants in eg. the head of the code and make comments about their purpose and why they have those values etc.