basic coding

can someone explain to me SIZE. why they use SIZE?
help!

1
2
3
    const int SIZE = 3; 
    int age[SIZE];      
    string name[SIZE];
Three reasons.

Firstly, it makes mistakes less likely when I write the code. If the same value, with the same meaning, is meant to be used in many places, manually typing it in each time increases the chance of error.

1
2
 int age[3];      
 string name[2];   // made a mistake here 


If this value is spread throughout the code, the chances of making a mistake why typing it in are increased.

Secondly, it carries meaning for the reader. If I see the number 3 used, I don't know why that was used. Does it matter? What does it represent? If I see the word SIZE, I know it's meant to represent the size of something. It makes the code more meaningful, making it easier to understand and easier to fix mistakes.

Thirdly, if I ever need to change the value from 3 to 7, or 9, or anything, I only have one place to change it:

const int SIZE = 3;
Change it here, and everything else in the code that uses SIZE now has the correct, new value. Massively reduces the chances of making mistakes.
Last edited on
Thanks Moschops.
Now I do understand
Topic archived. No new replies allowed.