Mark Michaelis' Weblog :
Updated: 9/1/2004; 6:56:23 AM.

 








Subscribe to "Mark Michaelis' Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Subscribe To
Mark's Weblog

 
 

Thursday, January 30, 2003

Refactorying:

  • Why Refactor?
    Create clear code that works!  - Ron Jeffries
  • What is refactoring?
    • A series of small steps, each of which changes a program's internal structure without changing its external behavior.
    • Move from one valid state to another more readable/clear one.
    • Requires existing tests so you don't risk breaking stuff when refactoring.
    • Should be performed in small steps (no longer than 10 min between successful runs of units tests).
  • Each method should have operations that are at the same level.
    • Bad example:

      public void Compile(string[] csFileList,

            string [] templateFileList)

            FileInfo[] filesToProcess;

            FileInfo[] templateFiles;

       

            // Validate parameters

            if( (csFileList==null)

                  || (csFileList.Length<1)

                  )

            {

                  // TODO: Localize error message.

                  throw new ArgumentException("A C# name is required.");

            }

       

       

            // Iterate through each item inside the csFileName array.

            // Note that these items could include wildcards.

       

            filesToProcess = GetFilesFromFileList(csFileList);

            templateFiles = GetTemplateFilesFromFileList(templateFileList);

       

            if (filesToProcess.Length < 1)

            {

                  // TODO:  Localize this error message.

                  throw new FileNotFoundException(

                        "No C# files found");

            }

       

            if (templateFiles.Length > 0)

            {

                  foreach (FileInfo curFileToProcess in filesToProcess)

                  {

                        CodeGenerator.Compile(

                              curFileToProcess, templateFiles);

                  }

            }   
       

    • Good example:

      public void Compile(string[] csFileList,

            string [] templateFileList)

            FileInfo[] filesToProcess;

            FileInfo[] templateFiles;

       

            ValidateCompileParameters(csFileList, templateFileList);

       

            filesToProcess = GetFilesFromFileList(csFileList);

            templateFiles = GetTemplateFilesFromFileList(templateFileList);

       

            ValidateThereAreFilesToCompile(filesToProcess);

       

            CompileEachFile(templateFiles, filesToProcess);

      }

  • Rules of Simple Design
    1. The code passes all tests.
    2. There is no duplication.
      // ; Sometimes 2 and 3 can be switched.
    3. The code expresses the programmer's intention.
    4. Using the smallest number of classes and methods.
  • An interesting observation is that XP is inherantly contradictory in some ways because including refactoring within XP explicitly violates the "don't do anything until it is needed" and "don't write any code without any failing tests" principles.
  • Naming Ideas:
    • Name by intention not by implementation
    • Use "Is" to prefix interrogatives
      eg. - IsReadOnly
  • Pair Programming
    • Switching programmers from test to implementation
    • One user could be looking up stuff while the other is programming
    • Set a goal of only doing 4 hours per day
    • Start inviting people to help you in order to get others programming in pairs
    • Do you focus far better because you are paired, I know I do.
    • Pairing is a great opportunity to lead.
    • We have learned that mandating paired programming doesn't work.
  • If you aren't doing the pair programming thing and are instead using code reviews, are you really doing code reviews and when you do them are you actvely participating and are you changing code?  How many are reviewing the code?
  • 80% of five things completed is not nearly as good as one thing 100% completed.  Complete stories so you have something to show.
  • App.hasUser(id) versus App.Users.contains(id)
  • How do you estimate with refactoring and pairing?
  • Unit-less values take away the tendency to say, "How come you are only getting 3 weeks of work done by 10 people."
  • Values are initially (until you can go to unit-less values) ideal man days by a pair of people
  • During first iteration you are probably going to get no work done
  • Story owner is responsible for making sure integration happens
  • Task estimates take on units such as days, however, they total to what you can do in an iteration not to actual.
  • Can non-team members modify our code.
  • If you can't pair consider writing acceptance tests, and unit tests with mock objects.
  • If you batch the coding of unit tests at once (not switching to code the implementation) programming then comment them out or use the ignore attribute and code them one at a time.
  • Consider not commenting private methods and classes.
  • Add hyperlink from API documetation to unit tests that will then server as examples.

3:02:38 PM   []    comment []

© Copyright 2004 Mark Michaelis.



 


January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Feb


Recent Posts