[Tutor] First program -- would like comments and criticisms

benmarkwell at gmail.com benmarkwell at gmail.com
Mon Feb 20 04:58:31 CET 2006


Thanks Andrei for your input.

I've already implemented a couple of your suggestions and will certainly
give the others a go.


On 2/18/06, Andrei <project5 at redrival.net> wrote:
>
> benmarkwell at gmail.com wrote:
> > Here is my first stab at putting together a working program. It is a
> > glossary that you can add words and definitions to, or look up words and
> > their definitions.  There are a couple of problems here and there, but
> > it basically does what I set out for it to do. All comments and
> > criticisms are welcome. I attached the data file that goes with the
> > script in case anybody wanted to actually run it, so they wouldn't have
> > to make their own dictionary file to access.
>
> I haven't executed it, but it seems quite good, certainly for a first
> working program. For small programs there's no point in overengineering,
> but here are some simple improvements you could look into:
>
> - place a "if __name__=="__main__":" condition at the bottom and put
> your program code in there (or better yet, extract it in a function and
> call that function after the if __name__ stuff). This way the module can
> also be imported.
>
> - make the location of the dictionary a 'constant' (which in Python
> means a variable with all-uppercase name :)). Having magic strings in
> code is bad practice.
>
> - the menu system could be redesigned to make it more
> maintenance-friendly. At this point if you want to add/modify a choice,
> you'd have to modify a procedure and the main code. You could instead
> make a dictionary mapping each possible choice (keys) to tuples
> containing a description and a function name. Loop over the keys and
> print them with the description. Once the user makes a choice, you could
> look up the function in the dictionary and execute it. This way adding
> new functionality items only means you need to add an entry to that one
> dictionary.
>
> - you could replace the "while num != '3':" condition with a "while
> True:" - the loop is interrupted anyway if '3' is selected, because of
> the 'break'. This way you can also remove the initialization of num.
>
> - you could extract the actual logic (the glossary) to a class with
> methods like "load", "save", "lookup" and separate it completely from
> all the user interaction. The glossary class shouldn't print/ask for
> user input. It's overkill for such a small program, but you could then
> theoretically reuse the glossary in a different application (e.g. put a
> webbased interface onto it, or subclass it and make a glossary which can
> also do multiple language translations, or use a different storage
> backend, etc.).
>
> <snip original code>
>
> --
> Yours,
>
> Andrei
>
> =====
> Mail address in header catches spam. Real contact info:
> ''.join([''.join(s) for s in zip(
> "poet at aao.l pmfe!Pes ontuei ulcpss  edtels,s hr' one oC.",
> "rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060219/7d17c971/attachment.htm 


More information about the Tutor mailing list