clean code - part 1, arguments

Post on 26-Dec-2014

511 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Clean Code I: Argumentsvimeo.com/13439458

TRANSCRIPT

Clean CodeObject Mentor, Inc.

Copyright 2006 by Object Mentor, IncAll Rights Reserved

fitnesse.org

www.objectmentor.com

www.junit.org

Robert C. Martin

2

Have you ever been significantly impeded by bad code?

3

Have you ever been significantly impeded by bad code?

Why did you write it?

4

Does this look familiar?

Productivity vs Time

0

20

40

60

80

100

Time

Productivity

5

What do we do about bad code?

The Grand Redesign in the sky?

Incremental Improvement

6

The Grand Redesign in the Sky

The Design Degrades

The Developers Revolt

Management Concedes

A TIGER TEAM is selected

A long, long race.

7

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

8

I won’t apologize for showing code.

Code is our medium.

We should revel in reading it.

9

Too many people apologize for code.

They think it’s something to be gotten rid of.

MDA, etc.

10

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.

11

There are better languages

To be sure!

Ruby, Smalltalk, prolog.

These are all very dense languages.

12

Even a pictorial language

Will have to capture details.

And so it will still be code.

13

Face it.

Code is here to stay.

So let’s look at some.

14

Clean Code: Args

An exploration of a mess that got cleaned.

It’s a comand line argument parser.

History:The Dave Astels Challenge.

15

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()); } }

>java MyProgram -l -p80 -d/home/logs

16

Here’s what Args looked like

For just boolean arguments.

17

Argspublic 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;

public Args(String schema, String[] args) { this.schema = schema; this.args = args; valid = parse(); }

public boolean isValid() { return valid; }

18

parse() private boolean parse() { if (schema.length() == 0 && args.length == 0) return true; parseSchema(); parseArguments(); return unexpectedArguments.size() == 0; }

19

parseSchema() private boolean parseSchema() { for (String element : schema.split(",")) { parseSchemaElement(element); } return true; }

20

parseSchemaElement() private void parseSchemaElement(String element) { if (element.length() == 1) { parseBooleanSchemaElement(element); } }

private void parseBooleanSchemaElement(String element) { char c = element.charAt(0); if (Character.isLetter(c)) { booleanArgs.put(c, false); } }

21

parseArguments() private boolean parseArguments() { for (String arg : args) parseArgument(arg); return true; }

private void parseArgument(String arg) { if (arg.startsWith("-")) parseElements(arg); }

private void parseElements(String arg) { for (int i = 1; i < arg.length(); i++) parseElement(arg.charAt(i)); }

22

parseElement() private void parseElement(char argChar) { if (isBoolean(argChar)) { numberOfArguments++; setBooleanArg(argChar, true); } else unexpectedArguments.add(argChar); }

private boolean isBoolean(char argChar) { return booleanArgs.containsKey(argChar); }

private void setBooleanArg(char argChar, boolean value) { booleanArgs.put(argChar, value); }

23

getBoolean() public boolean getBoolean(char arg) { return booleanArgs.get(arg); }

24

Misc public int cardinality() { return numberOfArguments; }

public String usage() { if (schema.length() > 0) return "-["+schema+"]"; else return ""; }

public String errorMessage() { if (unexpectedArguments.size() > 0) { return unexpectedArgumentMessage(); } else return ""; }

25

Misc private String unexpectedArgumentMessage() { StringBuffer message = new StringBuffer("Argument(s) -"); for (char c : unexpectedArguments) { message.append(c); } message.append(" unexpected.");

return message.toString(); }

26

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.

The Wrath of Khan

This code is about to “Grow”.

28

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.

29

Argspublic 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}

30

Args Constructor 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 (ArgsException e) { } return valid; }

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

parseSchema() private boolean parseSchema() throws ParseException { for (String element : schema.split(",")) { if (element.length() > 0) { String trimmedElement = element.trim(); parseSchemaElement(trimmedElement); } } return true; }

33

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); } }

34

validateSchemaElementId() private void validateSchemaElementId(char elementId) throws ParseException { if (!Character.isLetter(elementId)) { throw new ParseException( "Bad character:" + elementId + "in Args format: " + schema, 0); } }

35

isxxxSchemaElement() 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("#"); }

36

parsexxxSchemaElement() 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, ""); }

37

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.

38

parseArguments() private boolean parseArguments() throws ArgsException { for (currentArgument = 0; currentArgument < args.length; currentArgument++) { String arg = args[currentArgument]; parseArgument(arg); } return true; }

private void parseArgument(String arg) throws ArgsException { if (arg.startsWith("-")) parseElements(arg); }

39

parseElements() 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; } }

40

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; }

41

xxxIntArg() 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(); } }

42

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(); } }

private boolean isStringArg(char argChar) { return stringArgs.containsKey(argChar); }

43

xxxBooleanArg() private void setBooleanArg(char argChar, boolean value) { booleanArgs.put(argChar, value); }

private boolean isBooleanArg(char argChar) { return booleanArgs.containsKey(argChar); }

44

MISC public int cardinality() { return argsFound.size(); }

public String usage() { if (schema.length() > 0) return "-[" + schema + "]"; else return ""; }

45

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 ""; }

46

MISC 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; }

47

MISC 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 { }

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:

49

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.

50

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.

51

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

52

So I started to Refactor.

Fortunately I had tests!Very comprehensive tests.

Adding the String and Integer typesMade it clear that each new types forced changes in three places:

The schema element needs to be parsed.The arguments need to be parsed.Each argument needs a getxxx method.

Many different types? All with similar functions?That sounds like a class!

And so the Argument Marshaler was born.

53

ArgumentMarshaler private class ArgumentMarshaler { private boolean booleanValue = false;

public void setBoolean(boolean value) { booleanValue = value; }

public boolean getBoolean() {return booleanValue;} }

private class BooleanArgumentMarshaler extends ArgumentMarshaler { }

private class StringArgumentMarshaler extends ArgumentMarshaler { }

private class IntegerArgumentMarshaler extends ArgumentMarshaler { }}

54

All tests still passed. (ATP)

This obviously didn’t break anything.

55

This couldn’t possibly break anything?

private Map<Character, ArgumentMarshaler> booleanArgs = new HashMap<Character, ArgumentMarshaler>();

This broke a few statements, which I quickly fixed.

private void parseBooleanSchemaElement(char elementId) { booleanArgs.put(elementId, new BooleanArgumentMarshaler()); }

private void setBooleanArg(char argChar, boolean value) { booleanArgs.get(argChar).setBoolean(value); }

public boolean getBoolean(char arg) { return falseIfNull(booleanArgs.get(arg).getBoolean()); }

56

But the tests failed.

Because:

public boolean getBoolean(char arg) { return falseIfNull(booleanArgs.get(arg).getBoolean()); }

The .get can return null.

And falseIfNull is no longer needed.

So I made the following changes:

57

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

58

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!

59

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.

All in good time, dearie; all in good time.

60

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(); } }

61

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;

public void setBoolean(boolean value) { booleanValue = value; }

public boolean getBoolean() { return booleanValue; }

public void setString(String s) { stringValue = s; }

public String getString() { return stringValue == null ? "" : stringValue; } }

62

Granularity Reminder

These changes were made one tiny step at a time.

All tests were kept running.

When a test broke I stopped and got it running again before continuing.

63

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.

64

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(); }

65

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(); } }

66

Refactoring Integer arguments. private class ArgumentMarshaler { private boolean booleanValue = false; private String stringValue; private int integerValue;

public void setBoolean(boolean value) { booleanValue = value; }

public boolean getBoolean() { return booleanValue; }

public void setString(String s) { stringValue = s; }

public String getString() { return stringValue == null ? "" : stringValue; }

public void setInteger(int i) { integerValue = i; }

public int getInteger() { return integerValue; } }

67

All the Marshaling is encapsulated.

Now all the marshalling is in ArgumentMarshaler.

So it’s time to push the behavior down into the derivatives.

We’ll start, as always, with the booleans.

68

Creating BooleanArgumentMarshaler

private abstract class ArgumentMarshaler { protected boolean booleanValue = false; private String stringValue; private int integerValue;

public void setBoolean(boolean value) {. . .} public boolean getBoolean() {. . .} public void setString(String s) {. . .} public String getString() {. . .} public void setInteger(int i) {. . .} public int getInteger() {. . .}

public abstract void set(String s); }---- private class BooleanArgumentMarshaler extends ArgumentMarshaler { public void set(String s) { booleanValue = true; } }

69

Creating BooleanArgumentMarshaler

private void setBooleanArg(char argChar, boolean value) { booleanArgs.get(argChar).set("true"); }

The tests still pass.

And the ‘set’ function for boolean arguments is

Deployed to the BooleanArgumentMarshaler

70

The Boolean Get Function.

Polymorphically deploying ‘get’ functions is always tricky because of the return type issue.

71

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; ...

public abstract Object get(); }

private class BooleanArgumentMarshaler extends ArgumentMarshaler { public void set(String s) { booleanValue = true; }

public Object get() { return booleanValue; } }

72

Both ‘get’ and ‘set’ work for boolean

So now I can do the same for integers and strings.

Then I can remove all the cruft in ArgumentMarshaller that was supporting the argument types.

Remember, all this was done in tiny granules while keeping the tests running at all times.

73

Cleaning ArgumentMarshaller

private abstract class ArgumentMarshaler { public abstract void set(String s) throws ArgsException; public abstract Object get(); }

private class BooleanArgumentMarshaler extends ArgumentMarshaler { private boolean booleanValue = false; public void set(String s) {booleanValue = true;} public Object get() {return booleanValue;} }

private class StringArgumentMarshaler extends ArgumentMarshaler { private String stringValue = ""; public void set(String s) {stringValue = s; } public Object get() {return stringValue;} }

74

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;} }

75

And now we can clean Args.public class Args { private Map<Character, ArgumentMarshaler> marshalers; private Set<Character> argsFound; private ListIterator<String> currentArgument;

public Args(String schema, String[] args) throws ArgsException { marshalers = new HashMap<Character, ArgumentMarshaler>(); argsFound = new HashSet<Character>(); parseSchema(schema); parseArgumentStrings(Arrays.asList(args)); }

private void parseSchema(String schema) throws ArgsException { for (String element : schema.split(",")) if (element.length() > 0) parseSchemaElement(element.trim()); }

76

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); }

private void validateSchemaElementId(char elementId) throws ArgsException { if (!Character.isLetter(elementId)) throw new ArgsException(INVALID_ARGUMENT_NAME, elementId, null); }

77

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; } } }

private void parseArgumentCharacters(String argChars) throws ArgsException { for (int i = 0; i < argChars.length(); i++) parseArgumentCharacter(argChars.charAt(i)); }

78

And now we can clean Args. 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(); }

79

And now we can clean Args. public boolean getBoolean(char arg) { return BooleanArgumentMarshaler.getValue(marshalers.get(arg)); }

public String getString(char arg) { return StringArgumentMarshaler.getValue(marshalers.get(arg)); }

public int getInt(char arg) { return IntegerArgumentMarshaler.getValue(marshalers.get(arg)); }

80

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???

81

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.

82

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.

83

The “Clean Code” project.

Articles:The “Args” article.

The “Clean Code” book.

84

Contact Information

Robert C. Martinunclebob@objectmentor.com

Website:www.objectmentor.com

FitNesse:www.fitnesse.org

top related