Page 204 -
P. 204

code review


                       Head First
                        Code Review

                             The Head First Code Review Team has taken your code and
                             annotated it in the only way they know how: they’ve scribbled   I think we
                             all over it. Some of their comments are confirmations of what   can make a few
                             you might already know. Others are suggestions that might   improvements here.
                             make your code better. Like all code reviews, these comments
                             are an attempt to improve the quality of your code.

                             def sanitize(time_string):             A comment would
                                 if '-' in time_string:             be nice to have
                                                                   here.
                                     splitter = '-'
                                 elif ':' in time_string:
                                     splitter = ':'
                                 else:
                                     return(time_string)
                                 (mins, secs) = time_string.split(splitter)
                                 return(mins + '.' + secs)

            What happens     with open('james.txt') as jaf:                               Meet the Head
                                                                                          First Code Review
            if one of these       data = jaf.readline()                                   T eam.
             files is missing?!?   james = data.strip().split(',')
             Where’s your    with open('julie.txt') as juf:
             exception handling       data = juf.readline()       There’s a bit of duplication here. You
              code?          julie = data.strip().split(',')       could factor out the code into a small
                                                                   function; then, all you need to do is call

                             with open('mikey.txt') as mif:        the function for each of your athlete
                                 data = mif.readline()             data files, assigning the result to an
                             mikey = data.strip().split(',')
                                                                   athlete list.
                             with open('sarah.txt') as saf:
            There’s a lot        data = saf.readline()
             going on here,   sarah = data.strip().split(',')
             but we find it’s                                                      Ah, OK. We get it.
              not too hard to   print(sorted(set([sanitize(t) for t in james]))[0:3])  The slice is applied to
              understand if you  print(sorted(set([sanitize(t) for t in julie]))[0:3])  the list produced by
                             print(sorted(set([sanitize(t) for t in mikey]))[0:3])
              read it from the  print(sorted(set([sanitize(t) for t in sarah]))[0:3])  “sorted()”, right?
              inside out.




           168    Chapter 5
   199   200   201   202   203   204   205   206   207   208   209