Clean Code - Part 1, Arguments
Clean Code - Part 1, Arguments
www.objectmentor.com
Robert C. Martin
www.junit.org
fitnesse.org
Copyright 2006 by Object Mentor, Inc
All Rights Reserved
Have you ever been significantly impeded by
bad code?
2
Have you ever been significantly impeded by
bad code?
3
Does this look familiar?
Productivity vs Time
100
80
60
40
Productivity
20
0
Time
4
What do we do about bad code?
The Grand Redesign in the sky?
Incremental Improvement
5
The Grand Redesign in the Sky
The Design Degrades
The Developers Revolt
Management Concedes
A TIGER TEAM is selected
A long, long race.
6
Incremental Improvement
Always check code in better than you
checked it out.
Never let the sun set on bad code.
Test First!
Provides the necessary flexibility
7
I won’t apologize for showing code.
Code is our medium.
We should revel in reading it.
8
Too many people apologize for code.
They think it’s something to be gotten rid of.
MDA, etc.
9
You can’t get rid of code.
Because code is detail.
And you can’t get rid of detail.
You can change it’s form.
10
There are better languages
To be sure!
Ruby, Smalltalk, prolog.
These are all very dense languages.
11
Even a pictorial language
Will have to capture details.
And so it will still be code.
12
Face it.
Code is here to stay.
13
Clean Code: Args
An exploration of a mess that got cleaned.
History:
The Dave Astels Challenge.
14
Main: An example of how it’s used.
public static void main(String[] args) {
try {
Args arg = new Args("l,p#,d*", args);
boolean logging = arg.getBoolean('l');
int port = arg.getInt('p');
String directory = arg.getString('d');
executeApplication(logging, port, directory);
} catch (ArgsException e) {
System.out.printf("Argument error: %s\n", e.errorMessage());
}
}
15
Here’s what Args looked like
For just boolean arguments.
16
Args
public class Args {
private String schema;
private String[] args;
private boolean valid;
private Set<Character> unexpectedArguments =
new TreeSet<Character>();
private Map<Character, Boolean> booleanArgs =
new HashMap<Character, Boolean>();
private int numberOfArguments = 0;
17
parse()
private boolean parse() {
if (schema.length() == 0 && args.length == 0)
return true;
parseSchema();
parseArguments();
return unexpectedArguments.size() == 0;
}
18
parseSchema()
private boolean parseSchema() {
for (String element : schema.split(",")) {
parseSchemaElement(element);
}
return true;
}
19
parseSchemaElement()
private void parseSchemaElement(String element) {
if (element.length() == 1) {
parseBooleanSchemaElement(element);
}
}
20
parseArguments()
private boolean parseArguments() {
for (String arg : args)
parseArgument(arg);
return true;
}
22
getBoolean()
public boolean getBoolean(char arg) {
return booleanArgs.get(arg);
}
23
Misc
public int cardinality() {
return numberOfArguments;
}
24
Misc
private String unexpectedArgumentMessage() {
StringBuffer message = new StringBuffer("Argument(s) -");
for (char c : unexpectedArguments) {
message.append(c);
}
message.append(" unexpected.");
return message.toString();
}
25
This isn’t too bad.
Functions are small and obvious.
Code is relatively clean.
The hashmaps are a bit of a “trick” but they
aren’t hard to figure out.
26
The Wrath of Khan
This code is about to “Grow”.
We need to add integers and strings
Adding these two argument types turned out
to be much more complicated than the
previous code might have led us to believe.
28
Args
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;
30
parseSchema()
private boolean parseSchema() throws ParseException {
for (String element : schema.split(",")) {
if (element.length() > 0) {
String trimmedElement = element.trim();
parseSchemaElement(trimmedElement);
}
}
return true;
}
31
parseSchema()
private boolean parseSchema() throws ParseException {
for (String element : schema.split(",")) {
if (element.length() > 0) {
String trimmedElement = element.trim();
parseSchemaElement(trimmedElement);
}
}
return true;
}
32
parseSchemaElement()
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);
}
}
33
validateSchemaElementId()
private void validateSchemaElementId(char elementId)
throws ParseException {
if (!Character.isLetter(elementId)) {
throw new ParseException(
"Bad character:" + elementId +
"in Args format: " + schema, 0);
}
}
34
isxxxSchemaElement()
private boolean isStringSchemaElement(String elementTail) {
return elementTail.equals("*");
}
35
parsexxxSchemaElement()
private void parseBooleanSchemaElement(char elementId) {
booleanArgs.put(elementId, false);
}
36
There is a symmetry here.
But nothing is holding that symmetry together
except convention.
It’s just a bunch of functions with similar
names.
And if/else statements calling those
functions.
And a lot of duplication!
The original design pattern did not scale.
37
parseArguments()
private boolean parseArguments() throws ArgsException {
for (currentArgument = 0;
currentArgument < args.length;
currentArgument++)
{
String arg = args[currentArgument];
parseArgument(arg);
}
return true;
}
38
parseElements()
private void parseElements(String arg) throws ArgsException {
for (int i = 1; i < arg.length(); i++)
parseElement(arg.charAt(i));
}
39
setArgument()
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;
}
40
xxxIntArg()
private boolean isIntArg(char argChar)
{return intArgs.containsKey(argChar);}
41
xxxStringArg()
private void setStringArg(char argChar) throws ArgsException {
currentArgument++;
try {
stringArgs.put(argChar, args[currentArgument]);
} catch (ArrayIndexOutOfBoundsException e) {
valid = false;
errorArgumentId = argChar;
errorCode = ErrorCode.MISSING_STRING;
throw new ArgsException();
}
}
42
xxxBooleanArg()
private void setBooleanArg(char argChar, boolean value) {
booleanArgs.put(argChar, value);
}
43
MISC
public int cardinality() {
return argsFound.size();
}
44
MISC
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 integer parameter for -%c.",
errorArgumentId);
}
return "";
}
45
MISC
private String unexpectedArgumentMessage() {
StringBuffer message = new StringBuffer("Argument(s) -");
for (char c : unexpectedArguments) {
message.append(c);
}
message.append(" unexpected.");
return message.toString();
}
46
MISC
public String getString(char arg) {
return blankIfNull(stringArgs.get(arg));
}
47
So, this is pretty Yukky.
To make matters worse, we still have to add
doubles and string arrays to it!
The sheer number of variables is daunting.
Odd strings like “TILT”.
The hashsets, the try-catch blocks.
All add up to a:
48
So, this is pretty Yukky.
To make matters worse, we still have to add
doubles and string arrays to it!
The sheer number of variables is daunting.
Odd strings like “TILT”.
The hashsets, the try-catch blocks.
All add up to a:
Festering Pile.
49
But it didn’t start out that way.
It started out pretty clean.
The mess built up over time.
The initial structure didn’t scale well.
The more it grew, the worse it got.
Eventually I had to stop.
50
On Incrementalism
One of the best ways to ruin a program:
Make massive changes in the name of
improvement.
It’s hard to get the program working again.
TDD: Keep the system running at all times!
I am not allowed to make a change that
breaks the system.
Every tiny change I make must keep the
system working
51
So I started to Refactor.
Fortunately I had tests!
Very comprehensive tests.
52
ArgumentMarshaler
private class ArgumentMarshaler {
private boolean booleanValue = false;
53
All tests still passed. (ATP)
This obviously didn’t break anything.
54
This couldn’t possibly break anything?
private Map<Character, ArgumentMarshaler> booleanArgs =
new HashMap<Character, ArgumentMarshaler>();
55
But the tests failed.
Because:
56
Incrementally get the tests to pass.
I deleted the FalseIfNull function and removed the call from getBoolean
public boolean getBoolean(char arg) {
return booleanArgs.get(arg).getBoolean();
}
TEST
Next I split the function into two lines and put the ArgumentMarshaller into its
own variable.
public boolean getBoolean(char arg) {
Args.ArgumentMarshaler am = booleanArgs.get(arg);
return am.getBoolean();
}
TEST
And then I put in the null detection logic.
public boolean getBoolean(char arg) {
Args.ArgumentMarshaler am = booleanArgs.get(arg);
return am != null && am.getBoolean();
}
TEST
57
Granularity
This was the granularity!
Even if we go a bit faster in this presentation:
Never forget that all the changes I made,
were made at this level of granularity!
The steps were tiny!
58
Refactoring String Arguments
Similar to boolean arguments.
Must change hashmap, parse, set, and get
functions.
Only surprise is that I am loading all the
implementation into the ArgumentMarshaler
base class.
59
Refactoring String arguments.
private Map<Character, ArgumentMarshaler> stringArgs =
new HashMap<Character, ArgumentMarshaler>();
----
private void parseStringSchemaElement(char elementId) {
stringArgs.put(elementId, new StringArgumentMarshaler());
}
----
private void setStringArg(char argChar) throws ArgsException {
currentArgument++;
try {
stringArgs.get(argChar).setString(args[currentArgument]);
} catch (ArrayIndexOutOfBoundsException e) {
valid = false;
errorArgumentId = argChar;
errorCode = ErrorCode.MISSING_STRING;
throw new ArgsException();
}
}
60
Refactoring String arguments.
public String getString(char arg) {
Args.ArgumentMarshaler am = stringArgs.get(arg);
return am == null ? "" : am.getString();
}
private class ArgumentMarshaler {
private boolean booleanValue = false;
private String stringValue;
62
Refactoring Strategy
By now it should be clear.
All the marshaling behavior gets moved into
ArgumentMarshaler base class.
Once the rest of the app depends on
ArgumentMarshaller I’ll push the behavior into
derivatives.
So let’s do integers.
63
Refactoring Integer arguments.
private Map<Character, ArgumentMarshaler> intArgs =
new HashMap<Character, ArgumentMarshaler>();
----
private void parseIntegerSchemaElement(char elementId) {
intArgs.put(elementId, new IntegerArgumentMarshaler());
}
----
public int getInt(char arg) {
Args.ArgumentMarshaler am = intArgs.get(arg);
return am == null ? 0 : am.getInteger();
}
64
Refactoring Integer arguments.
private void setIntArg(char argChar) throws ArgsException {
currentArgument++;
String parameter = null;
try {
parameter = args[currentArgument];
intArgs.get(argChar).setInteger(Integer.parseInt(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();
}
}
65
Refactoring Integer arguments.
private class ArgumentMarshaler {
private boolean booleanValue = false;
private String stringValue;
private int integerValue;
67
Creating BooleanArgumentMarshaler
private abstract class ArgumentMarshaler {
protected boolean booleanValue = false;
private String stringValue;
private int integerValue;
68
Creating BooleanArgumentMarshaler
private void setBooleanArg(char argChar, boolean value) {
booleanArgs.get(argChar).set("true");
}
69
The Boolean Get Function.
Polymorphically deploying ‘get’ functions is
always tricky because of the return type
issue.
70
The Boolean Get Function
public boolean getBoolean(char arg) {
Args.ArgumentMarshaler am = booleanArgs.get(arg);
return am != null && (Boolean)am.get();
}
----
private abstract class ArgumentMarshaler {
protected boolean booleanValue = false;
...
72
Cleaning ArgumentMarshaller
private abstract class ArgumentMarshaler {
public abstract void set(String s) throws ArgsException;
public abstract Object get();
}
73
Cleaning ArgumentMarshaller
private class IntegerArgumentMarshaler extends ArgumentMarshaler {
private int intValue = 0;
public void set(String s) throws ArgsException {
try {intValue = Integer.parseInt(s);}
catch (NumberFormatException e) {throw new ArgsException();}
}
public Object get() {return intValue;}
}
74
And now we can clean Args.
public class Args {
private Map<Character, ArgumentMarshaler> marshalers;
private Set<Character> argsFound;
private ListIterator<String> currentArgument;
parseSchema(schema);
parseArgumentStrings(Arrays.asList(args));
}
75
And now we can clean Args.
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
throw new ArgsException(INVALID_ARGUMENT_FORMAT,
elementId, elementTail);
}
76
And now we can clean Args.
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;
}
}
}
78
And now we can clean Args.
public boolean getBoolean(char arg) {
return BooleanArgumentMarshaler.getValue(marshalers.get(arg));
}
79
Was this worth it?
Bad code gets harder and harder to clean as
time goes by.
If you want clean code, you have to clean it
as soon as it gets messy.
What about time to market?
The “Dinner” parable.
Have you ever been impeded???
80
Bad code.
Nothing has a more profound and long-term
degrading effect than bad code.
Bad schedules can be redone.
Bad requirements can be redefined.
Bad team dynamic can be resolved.
But bad code rots and ferments.
It becomes an inexorable weight that drags
the team down.
81
Professional Behavior
The “Green Band”.
Professionals write tests -- first.
Professionals clean their code.
Professionals know that the only way to go
fast
Is to go well.
82
The “Clean Code” project.
Articles:
The “Args” article.
The “Clean Code” book.
83
Contact Information
Robert C. Martin
[email protected]
Website:
www.objectmentor.com
FitNesse:
www.fitnesse.org
84