[Tutor] programming style question:
Sean 'Shaleh' Perry
shalehperry@attbi.com
Wed May 7 02:26:03 2003
On Wednesday 07 May 2003 22:52, Thomas CLive Richards wrote:
> Hey!
>
> I'm doing a degree course in Dunedin, New Zealand, and as part of this we
> have to do a pascal assignment. Well, to cut a long story short, i handed
> in my assignment, and was marked down because i used the following
> structure for a loop: (obviously i wrote this in pascal):
>
note, as much as we hate it, some instructors have preferred styles. It is
usually a good idea to just conform to their style -- no matter how stupid.
They probably have valid reasons for their style choice. Of course you are
in school to learn and any instructor should be willing to explain their
rationale. If not, perhaps you should speak to the next higher person.
Another comment here on style is you will often run into lame, stupid, weird,
broken, and plain odd styles, both in work and in the open source world.
Learning to adapt to the local style is a valuable skill to pick up. I have
heard from some that they have a hard time getting patches accepted by
upstream authors. On more than one occasion I have had someone ask me "did I
write this or did you?". This makes getting code accepted a lot easier. The
worst example I can give is a person who insisted on 3 space indented C code
along with a psuedo Hungarian naming style.
> while 1:
> do_lots_of_things_here()
> if (condition):
> break
> ...
>
> Now, the alternative would have (in my mind anyway) been more convoluted,
> and harder to read. What do you all think? It is a bit off topic, but do
> you guys think programming style is a personal thing, or should there be
> set rules? 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?
>
With the lecturing out of the way, on to my personal beliefs.
I recently had this discussion with a coder on one of my projects (in C++)
when while loops started showing up. Seems his coworker believes 'for' is
syntactic sugar and only 'while' should be used. We chatted about this for a
while and came up with the following.
* A while construct is to be used when the code is concerned about a condition
being met (or not met). Some typical examples:
while input != valid_input:
get_more_input()
while not complete:
do_computation() # computation sets complete to true
while not end of line: # or file
read_more_date()
while 1:
handle_event() # sets should_exit if user asks for the program to end
if should_exit: break
If you have a potentially infinite loop, or just have no idea how long it will
last it is probably a while loop.
* a for construct should be used when you have a known structure or items and
need to potentially access each one. counting loops, list walkers, etc.
Basically if you think "iteration" the for loop is probably the right choice.
So the following (using C++ because it is the easiest for me to write at
11pm):
it = container.begin();
while (it != end) {
handle(*it);
++it;
}
is less obvious to the reader than:
for(it = container.begin() ; it != end; ++it) {
handle(*it);
}
// yes, I realize for_each() is a better choice here, but this is an example
This matches the typical python usage directly.
for item in container:
handle(item)