Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] Zen Refactoring

Expand Messages
  • Elizabeth Keogh
    ... Oops! Sorry. ... I ve extracted a method from a test, then applied the extraction to a similar chunk in another test, but missed out one of the parameters
    Message 1 of 42 , Jun 1 7:40 AM
    • 0 Attachment
      Ron Jeffries wrote on 31/05/2005 19:17:40:

      > I'm into legible myself. I usually answer, when people ask about
      > refactoring the tests without test-tests (quis custodiet ad
      > infinitum), that when we refactor tests, they tend to break. You've
      > come up with one way to call me a liar

      Oops! Sorry.

      > ... are there other common
      > ways to refactor tests so that they appear to run, but don't?

      I've extracted a method from a test, then applied the extraction to a
      similar chunk in another test, but missed out one of the parameters which
      made it different. It was for a shape in a Tetris game, so it ended up
      testing "rotateRight" twice, instead of "rotateRight" and "rotateLeft". It
      was in an abstract class which had common tests for all shapes. I didn't
      notice that the S-shape was broken until I got the Gui working and saw it
      on the screen. Fortunately the other shapes were fine, so it was easy to
      fix.

      The only way round this that I (and others here) have come up with is to
      break the code, then refactor the test, checking that the test still
      red-bars (and is the only test that does so), put the code back and test
      that the suite green-bars again. The simplest way I've found to break the
      code is to throw a

      new Exception("TODO - remove this!")

      as close to the desired result as possible.

      Refactoring for red bars feels very unnatural. It's not foolproof, either,
      as I might break the wrong bit of code, but I've found it works better
      than doing it blind.

      Liz.

      --

      Elizabeth Keogh
      ekeogh@...
      http://www.livejournal.com/users/sirenian


      [Non-text portions of this message have been removed]
    • Alex Chaffee
      ... One way I ve found to avoid this is to use IntelliJ s awkwardly named Replace Method Code Duplicates refactoring. It looks for all blocks of code that
      Message 42 of 42 , Jun 4 8:03 AM
      • 0 Attachment
        On 6/1/05, Elizabeth Keogh <ekeogh@...> wrote:
        > I've extracted a method from a test, then applied the extraction to a
        > similar chunk in another test, but missed out one of the parameters which
        > made it different.

        One way I've found to avoid this is to use IntelliJ's awkwardly named
        "Replace Method Code Duplicates" refactoring. It looks for all blocks
        of code that have the same structure as the contents of the method,
        and replaces them with a call to that method. It's the natural
        successor to Extract Method -- perhaps a better name would be "Apply
        Method". It's not perfect, but when it works it's a thing of beauty.

        Yesterday it saved us from just the problem you mentioned. We tried to
        apply it and it *didn't* select the code we thought it would; it
        turned out we'd missed a parameter. Once we introduced that parameter
        it worked. If we'd been doing this by hand we'd have made your
        mistake.

        - A
      Your message has been successfully submitted and would be delivered to recipients shortly.