Author Topic: Need help with understanding the line of pogramming code  (Read 925 times)

0 Members and 1 Guest are viewing this topic.

Offline o0tsubasa0oTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Need help with understanding the line of pogramming code
« 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;
   }
}
« Last Edit: March 28, 2010, 09:03:05 PM by o0tsubasa0o »

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: Need help with understanding the line of pogramming code
« Reply #1 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?
« Last Edit: March 29, 2010, 09:22:06 AM by waltr »

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Need help with understanding the line of pogramming code
« Reply #2 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.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

 


Get Your Ad Here

data_list