📚 Bookshelf

📜 Contents

👈 Prev

👉 Next

Chapter 14: Successive Refinement

  • Nothing but code

    private Map<Character, Boolean> booleanArgs =
        new HashMap<Character, Boolean>();
    private Map<Character, String> stringArgs =
        new HashMap<Character, String>();
    private Map<Character, Integer> intArgs =
        new HashMap<Character, Integer>();
    
    private boolean setArgument(char argChar) throws ArgsException {
      if (isBooleanArg(argChar))
        setBooleanArg(argChar, true);
      else if (isStringArg(argChar))
        setStringArg(argChar);
      else if (isIntArg(argChar))
        setIntArg(argChar);
      else
        return false;
      return true;
    }
    
    private void setBooleanArg(char argChar, boolean value) {
      booleanArgs.put(argChar, value);
    }
    private boolean isBooleanArg(char argChar) {
      return booleanArgs.containsKey(argChar);
    }
    -----------------
    private Map<Character, ArgumentMarshaler> marshalers =
        new HashMap<Character, ArgumentMarshaler>();
    
    private boolean setArgument(char argChar) throws ArgsException {
      ArgumentMarshaler m = marshalers.get(argChar);
      try {
        if (m instanceof BooleanArgumentMarshaler)
          setBooleanArg(m);
        else if (m instanceof StringArgumentMarshaler)
          setStringArg(m);
        else if (m instanceof IntegerArgumentMarshaler)
          setIntArg(m);
        else
          return false;
      } catch (ArgsException e) {
        valid = false;
        errorArgumentId = argChar;
        throw e;
      }
      return true;
    }
    -----------------
    private boolean setArgument(char argChar) throws ArgsException {
      ArgumentMarshaler m = marshalers.get(argChar);
      if (m == null)
        return false;
      try {
        if (m instanceof BooleanArgumentMarshaler)
          setBooleanArg(m, currentArgument);
        else if (m instanceof StringArgumentMarshaler)
          setStringArg(m);
        else if (m instanceof IntegerArgumentMarshaler)
          setIntArg(m);
        } catch (ArgsException e) {
        valid = false;
        errorArgumentId = argChar;
        throw e;
      }
      return true;
    }
    -----------------
    private boolean setArgument(char argChar) throws ArgsException {
      ArgumentMarshaler m = marshalers.get(argChar);
      if (m == null)
        return false;
      try {
        if (m instanceof BooleanArgumentMarshaler)
          m.set(currentArgument);
        else if (m instanceof StringArgumentMarshaler)
          setStringArg(m);
        else if (m instanceof IntegerArgumentMarshaler)
          setIntArg(m);
      } catch (ArgsException e) {
        valid = false;
        errorArgumentId = argChar;
        throw e;
      }
      return true;
    }
    -----------------
    private boolean setArgument(char argChar) throws ArgsException {
      ArgumentMarshaler m = marshalers.get(argChar);
      if (m == null)
        return false;
      try {
        m.set(currentArgument);
        return true;
      } catch (ArgsException e) {
        valid = false;
        errorArgumentId = argChar;
        throw e;
      }
    }
    
  • So the solution is to continuously keep your code as clean and simple as it can be. Never let the rot get started.

📚 Bookshelf

📜 Contents

👈 Prev

👉 Next