第十四章 逐步改进——对一个命令行参数解析程序的案例研究
ps:本章设计代码示例所以篇幅会较长,推荐直接看原文,思路、代码讲解的很清楚
本章示例:解析命令行参数的工具 —— Args
Args的简单用法
public static void main(String[] args) {try{Argsarg=newArgs("l,p#,d*", args);boolean logging = arg.getBoolean('l');intport=arg.getInt('p');Stringdirectory=arg.getString('d');executeApplication (logging,port,directory);catch (ArgsExceptione){System.out.printf("Argumenterror:&s\n",e.errorMessage())}}}
14.1 Args的实现
Args.java
package com.pbjectmentor.utilities.args;import static com.objectmentor.utilities.args.ArgsException.ErrorCode.*;
import java.util.*;public class Args {private Map<Character, ArgumentMarshaler> marshalers;private Set<Character> argsFound;private ListIterator<String> currentArgument;public Args(String schema, String[] args) throws ArgsExcepticon {argsFound = new HashSet<Character>();marshalers = new HashMap<Character, ArgumentMarshaler>();parseSchema(schema);parseArgumentStrings(Arrays.asList(args));}private void parseSchema(String schema) throws ArgsException {for (Stringelement:schema.split(","))if (element.length() > 0)parseSchemaElement(element.trim());}private void parseSchemaElement(String element) throws ArgsException {char elementId = element.charAt(0);String elementTail = element.substring(1);validateSchemaElementId(elementId);if (elementTail.length() == 0)marshalers.put(elementid, new BooleanArgumentMarshaler());else if (elementTail.equals("*"))marshalers.put(elementid, new StringArgumentMarshaler());else if (elementTail.equals("#"))marshalers.put(elementid, new IntegerArgumentMarshaler());else if (elementTail.equals("##"))marshalers.put(elementid, new DoubleArgumentMarshaler());else if (elementTail.equals("[*]"))marshalers.put(elementId, new StringArrayArgumentMarshaler());elsethrow new ArgsException(INVALID_ARGUMENT_FORMAT, ellementId, elementTail);}private void validateSchemaElementId(char elementid) throws ArgsException {if (!Character.isLetter(elementId))throw new ArgsException(INVALID_ARGUMENT_NAME, elementid, null);}private void parseArgumentStrings(List<String> argSList) throws ArgsException {for (currentArgument = argsList.listIterator(); currentArgument.hasNext(); ) {String argString = currentArgument.next();if (argString.startsWith("-")) {parseArgumentCharacters(argString.substring(1));} else {currentArgument.previous();break;}}}private void parseArgumentCharacters(String argChars) throws ArgsException {for (int i = 0; i < argChars.length(); i++)parseArgumentCharacter(argChars.charAt(i));}private void parseArgumentCharacter(char argChar) throws ArgsException {ArgumentMarshaler m = marshalers.get(argChar);if (m == null) {throw new ArgsException(UNEXPECTED_ARGUMENT, argChar, null);} else {argsFound.add(argChar);try {m.set(currentArgument);} catch (ArgsException e) {e.setErrorArgumentId(argChar);throw e;}}}public boolean has(char arg) {return argsFound.contains(arg);}public int nextArgument() {return currentArgument.nextIndex();}public boolean getBoolean(char arg) {return BooleanArgumentMarshaler.getValue(marshaler(s.get(arg)));}public String getString(char arg) {return StringArgumentMarshaler.getValue(marshalers.get(arg));}public int getInt(char arg) {return IntegerArgumentMarshaler.getValue(marshalers.get(arg));}public double getDouble(char arg) {return DoubleArgumentMarshaler.getValue(marshalers.get(arg));}public String[] getStringArray(char arg) {return StringArrayArgumentMarshaler.getValue(marshalers.get(arg));}
}
ArgumentMarshaler.java
public interface ArgumentMarshaler {void set(Iterator<String> currentArgument) throws ArgsException;
}
BooleanArgumentMarshaler.java
public class BooleanArgumentMarshaler implemments ArgumentMarshaler {private boolean booleanValue = false;public void set(Iterator<String> currentArgument) throws ArgsException {booleanValue = true;}public static boolean getValue(ArgumentMarshaleram am) {if (am != null && am instanceof BooleanArgumentMarshaler)return ((BooleanArgumentMarshaler) am).booleanValue;elsereturn false;}
}
StringArgumentMarshaler.java
import static com.objectmentor.utilities.args.ArgsExceeption.ErrorCode.*;public class StringArgumentMarshaler implements ArgumentMarsShaler {private String stringvalue = "";public void set(Iterator<String> currentArgument) throws ArgsException {try {stringValue = currentArgument.next();} catch (NoSuchElementException e) {throw new ArgsException(MISSING_STRING);}}public static String getValue(ArgumentMarshaler am) {if (am != null && am instanceof StringArgumentMarshaler)return ((StringArgumentMarshaler) am).stringValue;elsereturn "";}
}
IntegerArgumentMarshaler.java
import static com.objectmentor.utilities.args.Args.ArggsException.ErrorCode.*;
public class IntegerArgumentMarshaler implements ArrgumentMarshaler {private int intValue = 0;public void set(Iterator<String> currentArgument) throws ArgsException {String parameter = null;try {parameter = currentArgument.next();intValue = Integer.parseInt(parameter);} catch (NoSuchElementException e) {throw new ArgsException(MISSING_INTEGER);} catch (NumberFormatException e) {throw new ArgsException(INVALID_INTEGER, parameeter);}}public static int getValue(ArgumentMarshaler am) {if (am != null && am instanceof IntegerArgumentMarshaler)return ((IntegerArgumentMarshaler) am).intValue;elsereturn 0;}
}
ArgsException.java
import static com.objectmentor.utilities.args.AArgsException.ErrorCode.*;
public class ArgsException extends Exception {private char errorArgumentId = '\0';private String errorParameter = null;private ErrorCode errorCode = OK;public ArgsException() {}public ArgsException(String message) {super(message);}public ArgsException(ErrorCode errorCode) {this.errorCode = errorCode;}public ArgsException(ErrorCode errorCode, String errorParameter) {this.errorCode = errorCode;this.errorParameter = errorParameter;}public ArgsException(ErrorCode errorCode, char errorArgumentId, String errorParameter) {this.errorCode = errorCode;this.errorParameter = errorParameter;this.errorArgumentId = errorArgumentId;}public char getErrorArgumentId() {return errorArgumentId;}public void setErrorArgumentId(char errorArgumentid) {this.errorArgumentId = errorArgumentId;}public String getErrorParameter() {return errorParameter;}public void setErrorParameter(String errorParameter) {this.errorParameter = errorParameter;}public ErrorCode getErrorCode() {return errorCode;}public void setErrorCode(ErrorCode errorCode) {this.errorCode = errorCode;}public String errorMessage() {switch (errorCode) {case OK:return "TILT:Should not get here.";case UNEXPECTED_ARGUMENT:return String.format("Argument -%c unexpected.", errorArgumentId);case MISSING_STRING:return String.format("Could not find string parameter for -%c.", errorArgumentId);case INVALID_INTEGER:return String.format("Argument -8c expects an integerbut was '%s'. ", errorArgumentId, errorParameter);case MISSING_INTEGER:return String.format("Could not find integer parametter for -%c.", errorArgumentId);case INVALID_DOUBLE:return String.format("Argument -%c expects a double but was '%s'.", errorArgumentId, errorParameter);case MISSING_DOUBLE:return String.format("Could not find double paarameter for -%c.", errorArgumentId);case INVALID_ARGUMENT_NAME:return String.format("'%c ' is not a valid argumentname.", errorArgumentId);case INVALID_ARGUMENT_FORMAT:return String.format("'%s' is not a valid argument fFormat.",errorParameter);}return "";}public enum ErrorCode {OK, INVALID_ARGUMENT_FORMAT, UNEXPECTED_ARGUMENT, INVALID_ARGUMENT_NAME,MISSING_STRING,MISSING_INTEGER, INVALID_INTEGER,MISSING_DOUBLE, INVALID_DOUBLE}
}
这段程序并非从一开始就写成现在的样子。要编写整洁代码,必须先写肮脏代码,然后再清理它。
14.2 Args:草稿
import com.google.android.gms.fido.u2f.api.common.ErrorCode;import java.text.ParseException;
import java.util.*;
public class Args {private String schema;private String[] args;private boolean valid = true;private Set<Character> unexpectedArguments = new TreeSet<Character>();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 Set<Character> argsFound = new HashSet<Character>();private int currentArgument;private char errorArgumentId = '\0';private String errorParameter = "TILT";private ErrorCode errorCode = ErrorCode.OK;private enum ErrorCode {OK, MISSING_STRING, MISSING_INTEGER, INVALID_INTEGER, UNEXPECTED_ARGUMENT}public Args(String schema, String[] args) throws ParseException {this.schema = schema;this.args = args;valid = parse();}private boolean parse() throws ParseException {if (schema.length() == 0 && args.length == 0)return true;parseSchema();try {parseArguments();} catch (ArgsExceptione) {return valid;}}private boolean parseSchema() throws ParseException {for (Stringelement:schema.split(",")) {if (element.length() > 0) {String trimmedElement = element.trim();parseSchemaElement(trimmedElement);}}return true;}private void parseSchemaElement(String element) throws ParseException {char elementId = element.charAt(0);String elementTail = element.substring(1);validateSchemaElementId(elementId);if (isBooleanSchemaElement(elementTail))parseBooleanSchemaElement(elementId);else if (isStringSchemaElement(elementTail))parseStringSchemaElement(elementId);else if (isIntegerSchemaElement(elementTail)) {parseIntegerSchemaElement(elementId);} else {throw new ParseException(String.format("Argument: %c has invalid format: %s.", elementId, elementTail), 0);}}private void validateSchemaElementId(char elementId) throws ParseException {if (!Character.isLetter(elementId)) {throw new ParseException("Bad character:" + elementId + "in Args format: " + schenna, 0);}}private void parseBooleanSchemaElement(char elementId) {booleanArgs.put(elementId, false);}private void parseIntegerSchemaElement(char elementId) {intArgs.put(elementId, 0);}private void parseStringSchemaElement(char elementId) {stringArgs.put(elementId, "");}private boolean isStringSchemaElement(String elementTail) {return elementTail.equals("*");}private boolean isBooleanSchemaElement(String elementTail) {return elementTail.length() == 0;}private boolean isIntegerSchemaElement(String elementTail) {return elementTail.equals("#");}private boolean parseArguments() throws ArgsException {for (currentArgument = 0; currentArgument < args.leength; currentArgument++) {String arg = args[currentArgument];parseArgument(arg);}return true;}private void parseArgument(String arg) throws ArgsException {if (arg.startsWith("-"))parseElements(arg);}private void parseElements(String arg) throws ArgsException {for (int i = 1; i < arg.length(); i++)parseElement(arg.charAt(i));}private void parseElement(char argChar) throws ArgsException {if (setArgument(argChar))argsFound.add(argChar);else {unexpectedArguments.add(argChar);errorCode = ErrorCode.UNEXPECTED_ARGUMENT;valid = false;}}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);elsereturn false;return true;}private boolean isIntArg(char argChar) {return intArgs.containsKey(argChar);}private void setIntArg(char argChar) throws ArgsException {currentArgument++;String parameter = null;try {parameter = args[currentArgument];intArgs.put(argChar, new Integer(parameter));} catch (ArrayIndexOutOfBoundsException e) {valid = false;errorArgumentId = argChar;errorCode = ErrorCode.MISSING_INTEGER;throw new ArgsException();} catch (NumberFormatException e) {valid = false;errorArgumentId = argChar;errorParameter = parameter;errorCode = ErrorCode.INVALID_INTEGER;throw new ArgsException();}}private void setStringArg(char argChar) throws ArgsException {currentArgument++;try {stringArgs.put(argChar, args[currentArgument]);} catch (ArrayIndexOut0fBoundsException e) {valid = false;errorArgumentId = argChar;errorCode = ErrorCode.MISSING_STRING;throw new ArgsException();}}private boolean isStringArg(char argChar) {return stringArgs.containsKey(argChar);}private void setBooleanArg(char argChar, boolean value) {booleanArgs.put(argChar, value);}private boolean isBooleanArg(char argChar) {return booleanArgs.containsKey(argChar);}public int cardinality() {return argsFound.size();}public String usage() {if (schema.length() > 0)return "-[" + schema + "]";elsereturn "";}public String errorMessage() throws Exception {switch (errorCode) {case OK:throw new Exception("TILT:Should not get here.");case UNEXPECTED_ARGUMENT:return unexpectedArgumentMessage();case MISSING_STRING:return String.format("Could not find string parameter for -%c.", errorArgumentId);case INVALID_INTEGER:return String.format("Argument -&c expects an integer but was '%s'.", errorArgumentId, errorParameter);case MISSING_INTEGER:return String.format("Could not find integerparameter for -%c.", errorArgumentId);}return "";}private String unexpectedArgumentMessage() {StringBuffer message = new StringBuffer("Argument(s)-");for (char c : unexpectedArguments) {message.append(c);}message.append(" unexpected.");return message.toString();}private boolean falseIfNull(Boolean b) {return b != null && b;}private int zeroIfNull(Integer i) {return i == null ? 0 : i;}
,private String blankIfNull(String s) {return s == null ? "" : s;}public String getString(char arg) {return blankIfNull(stringArgs.get(arg));}public int getInt(char arg) {return zeroIfNull(intArgs.get(arg));}public boolean getBoolean(char arg) {return falseIfNull(booleanArgs.get(arg));}public boolean has(char arg) {return argsFound.contains(arg);}public boolean isValid() {return valid;}private class ArgsException extends Exception {}}
混乱是逐渐产生的。更早的版本并不如此肮脏。
14.2.1 所以我暂停了
重构
首先,每种参数类型都要有解析其范式元素、从而为该种类型选择HashMap的方法。
其次,每种参数类型都需要在命令行字符号串中解析,然后再转换为真实类型。
最后,每种参数类型都需要一个getXXX方法,按照其真实类型向调用者返回参数值。许多种不同类型,类似的方法——听起来像是个类。ArgumentMarshaler的概念就是这样产生的。
14.2.2 渐进
毁坏程序的最好方法之一就是以改进之名大动其结构。
为了避免这种状况发生,采用了测试驱动开发的规程。这种手法的核心原则之一是保
持系统始终能运行。换言之,采用TDD,不会允许做出破坏了系统的修改。每次修改都必须
保证系统能像以前一样工作。
14.3 字符串参数
每次修改一个地方,持续运行测试。如果测试出错,在做下一个修改前确保通过。
对Args类所做的最主要的修改是在监测部分。从Args里面取出了大量代码,放到ArgsException中。还把全部ArgumentMarshaler转移到了它们自己的文件中。
优秀的软件设计,大都关乎分隔——创建合适的空间放置不同种类的代码。对关注面的
分隔让代码更易于理解和维护。
14.4 小结
代码能工作还不够。能工作的代码经常会严重崩溃。满足于仅仅让代码能工作的程序员不够专业。
没什么能比糟糕的代码给开发项目带来更深远和长期的损害了。
糟糕的代码可以清理。不过成本高昂。随着代码腐败下去,模块之间互相渗透,出现大量隐藏纠结的依赖关系。找到和破除除旧的依赖关系又费时间又费劲。另一方面,保持代码整洁却相对容易。
所以,解决之道就是保持代码持续整洁和简单。永不让腐坏有机会开始。