On Wed, Feb 16, 2011 at 10:35 AM, snorble <snor...@hotmail.com> wrote: > I use Python a lot, but not well. I usually start by writing a small > script, no classes or modules. Then I add more content to the loops, > and repeat. It's a bit of a trial and error learning phase, making > sure I'm using the third party modules correctly, and so on. I end up > with a working script, but by the end it looks messy, unorganized, and > feels hacked together. I feel like in order to reuse it or expand it > in the future, I need to take what I learned and rewrite it from > scratch.
To some extent, writing code well just comes from practice with programming, and practice with a language. Exploratory programming is pretty normal (though some still insist on having a complete design before starting on coding), but I find that having lots of automated tests helps make exploratory programming more practical. You may or may not want to read a bit about agile programming: http://en.wikipedia.org/wiki/Agile_software_development A rewrite once in a while is not the end of the world, unless your management decides it is (then it's just a pain to live without the rewrite). ^_^ Oh, and if you use modules and classes and even just functions to limit the impact of one detail on another (each design decision probably should be wrapped up into its own scope somehow), you'll find that rewrites of Portions of your code are pretty viable - without having changes need to cascade through one's codebase. Just saying "I want this to read clearly, not run marginally faster" helps, as does using tools like pylint, pychecker, pyflakes and/or pep8 (pylint probably obviates the pep8 script, but pychecker or pyflakes likely work well in combination with pep8 - so far I've only used pylint). Also, pymetrics is nice for its McCabe Complexity statistic - if the # gets too high, simplify - this often means subdividing large functions or methods into a larger number of smaller functions or methods. But pylint and perhaps others have a warning if your code blocks get too long - that almost gives the same benefit as McCabe. You may find that http://rope.sourceforge.net/ helps with your refactoring, though I have yet to try rope - I just use vim and n.n.n. I hear that some IDE's support refactoring well - pycharm might be a good example. IOW, using some automated tools should give you lots of nearly immediate feedback and assistance in your goal. Yes, some people do start with classes at the outset. Just think of a class as a jack in the box - something with an external view, and a different, hidden, internal view. When you see a need for something like a jack in a box in your code (two different views of what's going on, to limit detail getting scattered more broadly than necessary), consider using a class or perhaps a generator. And yeah, sometimes functions are enough - hey, some functional programming languages have no other means of limiting the impact of details, and there is still some really good functional code out there. Finally, look over someone else's code now and then for ideas; that's a great way to learn. You don't necessarily have to hover over someone's shoulder to learn from them - fortunately we live in a world with symbolic language :). Make sure the copyright on the code won't bite you though. HTH -- http://mail.python.org/mailman/listinfo/python-list