How to actually write a program?

Mike C. Fletcher mcfletch at rogers.com
Sat Sep 4 00:05:50 CEST 2004


This may seem a bit weird:

    Create another text file and save it as test_mykewlprogram.py.  Add
    the following to it:

    import unittest
    import mykewlprogram

    class BasicTests( unittest.TestCase ):
        def test_something( self ):
           """Do some basic test of functionality"""

    if __name__ == "__main__":
        unittest.main()

    And run that from the command line.  If the unit-test passes (it
    should), then add some more code to it until it doesn't pass, then
    fix your program to make it pass, then immediately go back to
    writing unit tests.  For instance:

    def test_something( self ):
        """Can we create an instance of our frobnitz?"""
        mykewlprogram.Frobnitz()

    which should fail, because you haven't defined a Frobnitz class
    yet.  So you simply define it in mykewlprogram as:

    class Frobnitz( object ):
        """Frobnitzes are responsible for killing Whirlygigs"""

    then run your test suite again.  It should now pass, so go back to
    writing code that tests to see if your program does what it's
    supposed to do.


That (called test-driven development) works very well if you've got a 
general idea of what you want to do, and your project is not about user 
interface design or the like (where test-driven development can be quite 
messy).

There are lots of other ways to do it.  I was originally taught a method 
that was taught in the very early days of computers wherein you figure 
out the whole program in your head/on paper, running it in mental 
emulation until you're sure you've figured out the major features of the 
program.  It tends to impress the heck out of certain people (you have 
to be able to hold entire complex systems in your head to do it, and 
that takes a lot of practice that most people have never mastered), 
really speeds up planning meetings, and you can do it in the shower, in 
bed, while cooking, etceteras, but it's a lot less common these days.  
It's also pretty slow and error-prone :) .

True enlightenment lies somewhere between the extremes.  There's 
hundreds (thousands?) of books that purport to show you the 
one-true-solution to this problem.  Any time you see that you can be 
pretty sure that there is no "one" solution and that you'll have to 
experiment with the ideas expounded by the various religions and figure 
out what works *for you*.

Have fun,
Mike


Nick Evans wrote:

>Hello there,
>I have been on and off learning to code (with python being the second
>language I have worked on after a bit of BASIC). What I really want to know
>is, if you are going to actually write a program or a project of some sort,
>how do you actually start.
>
>Picture this, you know what you want the program to do (its features), you
>have a possably rough idea of how the program will work. You have opened an
>empty text file and saved it as 'mykewlprogram.py' and now your sitting in
>front of an empty text file... Now then.... where do I start.
>
>Any ideas about this problem :-)
>
>Ta
>Nick
>  
>
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list