The K&R style:
while (true) {
...
}
The Allman Style:
while (true)
{
...
}
The Emacs-default Style:
while (true)
{
...
}
For anything else, be prepared to argue with the instructor as to why your style is useful/sensible/valid.
for (int i = 0; i < 100; i++)
{
m[i] *= 100;
}
The code above is supposed to change units from centimeters to millimeters. Can you tell? Now consider the following: for (int i = 0; i < NUM_MEASUREMENTS; i++)
{
m[i] *= METERS_TO_CENTIMETERS;
}
Another nice reason is when you decide to change things later
on, you only have one place to look, and can avoid possibly-inaccurate
search/replace schemes. Imagine the headache when increasing the number
of measurements your lab-helper program can handle from 100 to 10000
when you have the following line of code: for (int i = 0; i <= 99; i++)rather than
for (int i = 0; i <= NUM_MEASUREMENTS - 1; i++)
© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.