[Tutor] programming style question:

Alan Gauld alan.gauld@blueyonder.co.uk
Wed May 7 03:39:01 2003


> ...marked down because i used the following structure for a loop:
>
> while 1:
> do_lots_of_things_here()
> if (condition):
> break
> ...

In Pascal that probably should have been:

repeat
   do_lots_of_things_here()
until condition

And at the very least it should have been

while true
....
end

not while 1.

> Now, the alternative would have (in my mind anyway) been
> more convoluted, and harder to read. What do you all think?

If you translate one idiom to another directly it often appears
convoluted, if you use the native idiom it usually gets clearer.
The "while true" idiom in pascal is rarely seen and considered
bad practice because thats exactly what the repeat loop is there
for.

> It is a bit off topic, but do you guys think programming style
> is a personal thing, or should there be set rules?

There are usually rules, mainly for the sake of consistency.
However there is hard evidence to suggest some style issues
are important - levels of indentation, location of begin/end
markers etc. - See Code Complete by McConnel for more on these.

> Things like "do i use a for or while loop here? In pascal it's
> pretty much up to you; both are usable. what do you think?

Nope. In Pascal there are 3 loops each with a pretty distinct purpose.
Using the wrong loop is considered bad style.

FOR loops are for iterating over a fixed (or computable)
number of items

WHILE loops are for iterating over an unknown number of items,
and particularly where you may not want to iterate at all.

REPEAT loops are for cases where you always want to do the first
iteration, but don't know how many others you will need.


Alan G.