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

42992Re: [XP] Re: Simplicity pressures

Expand Messages
  • alex@jguru.com
    Feb 6 11:21 AM
    • 0 Attachment
      On Wed, Feb 06, 2002 at 05:37:53PM -0000, JDonWells wrote:
      > "Once and only once" has two parts. You are applying the "only once"
      > part perhaps a bit to rigorously. You now need to apply the "once
      > and" part. If the code is different it is probably different for a
      > very good reason. What is that difference? Why is it different?
      > There is probably something left unsaid about what you are trying to
      > communicate with the two pieces of code. Once you have refactored to
      > explicitly make the difference clear your desire to make it a single
      > piece of code may very well dissipate.
      >
      > Don Wells

      That sounds like good advice, but I can't see the application here.

      It's different simply because it's at a different level in the
      grammar. The logic is the same.

      I appreciate you humoring me by walking through this code, since I'm
      inexperienced enough at refactoring that I don't see my way out of
      this refactoring cul-de-sac.

      The original method was something like:

      private String evaluateExpression(Tokenizer tokens)
      {
      if (!tokens.hasMoreTokens())
      return null;

      String left = evaluateTerm(tokens);
      long valLeft = Long.parseLong(left);

      while (tokens.hasMoreTokens()) {
      String operator = tokens.nextToken();
      if (operator == null) {
      // we wanted an operator, but if all we got was the
      // end of the token stream (all whitespace), that's OK
      // too
      break;
      }
      if (operator.equals("+") || operator.equals("-")) {
      String right = evaluateTerm(tokens);
      long valRight = Long.parseLong(right);
      valLeft = operate(operator, valLeft, valRight);
      }
      else {
      // any other operator (e.g. ')') signals the end of this expression
      tokens.pushBack(operator);
      break;
      }
      }
      return ""+valLeft;
      }

      The other method (evaluateTerm) was identical except for
      "*" and "/" instead of "+" and "-"
      evaluateFactor instead of evaluateTerm

      I factored out the operators as parameters, but I was left with the
      function call. Since it happens inside a loop, it's difficult to
      extract.

      - A

      --
      Alex Chaffee mailto:alex@...
      jGuru - Java News and FAQs http://www.jguru.com/alex/
      Creator of Gamelan http://www.gamelan.com/
      Founder of Purple Technology http://www.purpletech.com/
      Curator of Stinky Art Collective http://www.stinky.com/
    • Show all 9 messages in this topic