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

146259Re: TDD Like You've Never Seen it Before, Refactored

Expand Messages
  • Jeff Grigg
    Nov 13, 2008
    • 0 Attachment
      --- "John A. De Goes" <john@...> wrote:
      > A short screencast showing myself and two other developers
      > TDD'ing a Stack in the Java programming language:
      > http://www.vimeo.com/1653402
      > I think it makes a good introduction to test-driven
      > development, [1] and shows how following TDD strictly
      > leads to test coverage of all code paths. You can
      > download the source code from
      > http://www.n-brain.net/outbox/una-tdd-stack.zip

      I'm not sure about the "good introduction" training aspect of starting
      with the target class and method shells, and then writing tests. I
      would consider this an "acceptable" TDD approach, but not pure, and
      not the approach I take when teaching TDD.

      Overall, I would call it a successful TDD session. And an interesting

      _ _ _ _ _

      I'll push the refactoring another two or three steps:

      #1. I don't like duplicate data.

      So I killed the 'private volatile int size;' property, giving...

      public int size() {
      int size = 0;
      for (Node<T> node = head; node != null; node = node.getNext())
      return size;

      public boolean isEmpty() {
      return (head == null);

      [and a few more deleted lines]

      If you want efficient, add a test for it. (...that 'Node.getNext()'
      isn't called N times.)

      #2. Simplify 'push'...

      public void push(T value){
      if (head == null) {
      head = new Node<T>(value, null);
      else {
      Node<T> oldHead = head;
      head = new Node<T>(value, oldHead);

      public void push(T value){
      head = new Node<T>(value, head);

      #3. Delete the default constructor. Java generates one.

      public Stack() {

      There. Better now? ;->
    • Show all 67 messages in this topic