[Tutor] a few question about my evolving program

Alan Gauld alan.gauld at btinternet.com
Wed Aug 12 11:40:41 CEST 2015


On 12/08/15 04:23, Clayton Kirkwood wrote:

> Question 2:
> My current code:
> See "Look here" below.

If I understand your 'question', your code *should* reduce
to this for the given filename:

...
> target_directory_file_list = master_directory_file_list[target_directory]
...
> for current_directory_path in master_directory_file_list.keys():
>      current_file_list = master_directory_file_list[current_directory_path]
>
>      for current_filename in current_file_list:
>           if current_filename in target_directory_file_list:


Now, what is your expected output?
And what are you getting?
I'm not sure I understand what Question 2 actually is...

> As you can see the current_filename does not exist in target_directory_file
> list. Yet, I fall through to the next line.

What does fall through mean? Which line do you fall through to?

> Yes, the indents are all fine: I
> wouldn't have gotten to running code otherwise.

Running code does not mean correct code. The indents could all
align with something but not necessarily what you want them to
align with. However, I can't tell since I'm not sure what is
actually going on yet.

> I turned my head upside
> down and still couldn't see why it doesn't work

I doubt if that would help :-)

What might help is breaking the code into say 3 helper functions
That would leave the higher level structure clear to see.

Also, although we normally ask for 'meaningful variable names'
yours are very long which actually makes the code harder to read.

You could consider shortening some of them, especially those
only used within a loop such as current_.... Just dropping
the "current_" would help. It doesn't really add any extra
information. Similarly we know that paths are made up of
directories so just path instead of directory_path probably
works. (Use file_path to explicitly call out file paths
if you must - its shorter...)

As an example consider my shortened code above using
shorter names:

target_file_list = master_directory_file_list[target_directory]
...
 > for path in master_directory_file_list.keys():
 >      files = master_directory_file_list[path]
 >
 >      for name in files:
 >           if name in target_file_list:

Notice I kept the long name for the master list because it
is used in multiple segments. But those only used in
the local loop are implicitly current and short lived
so you don't need a long name.

Readability is the first step to finding bugs and although your
long names may seem more descriptive their length actually gets
in the way of reading the code structure easily.
IMHO at least... :-)

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list