Re-thinking my if-thens - a software engineering question

Neil Cerutti horpner at yahoo.com
Wed Jan 24 15:55:17 EST 2007


On 2007-01-24, metaperl <metaperl at gmail.com> wrote:
>             if dict_key == 'PCN':
> 		fields = dict_val.split("/")
>                 mo = re.match( '(\w{2})(\d{2})(\d{2})' , fields[1] )
> 		if mo:
> 		    dict_val = "%s/%s%s/%s" % (fields[0], mo.group(1), mo.group(3),
> fields[2][1:])
> 		else:
> 		    dict_val = dict_val
>
> Ok, so now here is what has happened. This code was based on
> the assumption that dict_val would have 2 forward slashes in
> it. It turns out that I need to follow a different process of
> massaging when no slashes are present. A naive solution would
> be something like:
>
> if dict_key == 'PCN':
>                     fields = dict_val.split("/")
>                     if fields == 3:
>                         dict_val = pcn_three(fields) # where pcn_three
> is the code above
>                 else:
>                     # new logic
>
> But I am wondering if I should abstract the flow of control
> into a class or something.

This is what I do in Python when a new requirement pops up:

 1. Write the simplest/first thing that comes to mind to fix it.
 1. a) Am I done? Probably. But maybe not.
 2. Now I examine what I've written to see the lay of the code.
    Only after writing something new once do I usually have
    enough information to write it better. In other words,
    writing the code organizes my thoughts. I usually have to
    fully understand something, even to get a kludgey solution to
    work. The Kludgey solution informs the design of something
    better.
 2. a) Got to 1. a)

In the case above, I've tried to figure out what you're
specifically doing, and failed. So I don't have more specific
advice.

-- 
Neil Cerutti



More information about the Python-list mailing list