SlideShare a Scribd company logo
The Art
of
Clean Code
The Basics
The Art of Clean Code
Our Goal: Preventing
This!
Yael Zaritsky-Peretz
Developer @ CI team @ Wix
Over
90 million
users
Wix
Over
1000
employees
~400
developers
The Art of Clean Code
First Feature - Product
First Feature - Code
public class MyConsole {
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
System.out.print("> ");
System.out.println(inputReader.readLine());
}
}
}
public class MyConsole {
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
System.out.print("> ");
System.out.println(inputReader.readLine());
}
}
}
public class MyConsole {
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
System.out.print("> ");
System.out.println(inputReader.readLine());
}
}
}
Second Feature - Product
public static void go() throws IOException {
BufferedReader inputReader =
new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of ... sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
System.out.print(">");
System.out.println(inputReader.readLine());
}
public static void go() throws IOException {
BufferedReader inputReader =
new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of ... sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
System.out.print(">");
System.out.println(inputReader.readLine());
}
Second Feature - Code
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}…}
public static void go() throws IOException {
BufferedReader inputReader =
new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of … sum/copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
}
else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
}
}
The Art of Clean Code
The Art of Clean Code
Agenda
● What is Clean Code?
● Example
● Some Basic Rules
● Culture
What is Clean
Code?
KISS
KISS
Test Coverage
public class NumberTest {
@Test
public void testMultiplyByTwo() {
assertThat(Number.multiplyByTwo(2),is(4));
}
}
public class Number {
public static int multiplyByTwo(int number) {
return number;
}
}
public class NumberTest {
@Test
public void testMultiplyByTwo() {
assertThat(Number.multiplyByTwo(2),is(4));
}
}
public class Number {
public static int multiplyByTwo(int number) {
return number;
}
}
public class NumberTest {
@Test
public void testMultiplyByTwo() {
assertThat(Number.multiplyByTwo(2),is(4));
}
}
public class Number {
public static int multiplyByTwo(int number) {
return number;
}
}
public class NumberTest {
@Test
public void testMultiplyByTwo() {
assertThat(Number.multiplyByTwo(2),is(4));
}
}
public class Number {
public static int multiplyByTwo(int number) {
return number * 2;
}
}
public class NumberTest {
@Test
public void testMultiplyByTwo() {
assertThat(Number.multiplyByTwo(2),is(4));
}
}
public class Number {
public static int multiplyByTwo(int number) {
return number * 2;
}
}
Test Coverage
Example
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
}
}
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
}
}
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
}
}
BUG found
The Art of Clean Code
The Art of Clean Code
TDD
Test Driven Design
else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
Make sure that func("3 3") returns "Sum of 3, 3 is 6"
else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String str = NumbersToSum.getOutputString(nToSIn);
System.out.println(str);
}
public class NumbersToSum {
public static String getOutputString(String … nToSIn) {
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
return str;
}}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String str = NumbersToSum.getOutputString(nToSIn);
System.out.println(str);
}
@Test
public void testGetOutputStringForSingleNumber() {
assertThat(NumbersToSum.getOutputString("3 3"),
is("Sum of 3, 3 is 6"));
}
The Art of Clean Code
public class NumbersToSum {
public static String getOutputString(String … nToSIn) {
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … nToSIn) {
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
The Art of Clean Code
The Art of Clean Code
BUG FIXED ! ! ! Test is GREEN
public class NumbersToSum {
public static String getOutputString(String … nToSIn) {
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String nToSIn = inputReader.readLine();
String str = NumbersToSum.getOutputString(nToSIn);
System.out.println(str);
}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSum.getOutputString(numbersToSumInput);
System.out.println(str);
}
else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSum.getOutputString(numbersToSumInput);
System.out.println(str);
}
public class NumbersToSum {
public static String getOutputString(String … nToSIn) {
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] nToS = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] nToS = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
Another BUG found
The Art of Clean Code
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
@Test
public void testGetOutputString() {
assertThat(NumbersToSumPrinter.getOutputString("3 231"),
is("Sum of 3, 231 is 234"));
}
@Test
public void testSumOfMoreThanOneNumber() {
assertThat(NumbersToSum.sum(3, 231),
is(234));
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSumPrinter {
public static String getOutputString(String numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
}
str = str.substring(0, str.length() - 2);
int sum = NumbersToSum.sum(intArrayFromStrings(numbersToSum));
str += " is " + sum;
return str;
}
}
public class NumbersToSumPrinter {
public static String getOutputString(String numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
}
str = str.substring(0, str.length() - 2);
int sum = NumbersToSum.sum(intArrayFromStrings(numbersToSum));
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static int sum(int ... numbersToSum) {
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
sum += numbersToSum[0];
}
return sum;
}
}
The Art of Clean Code
public class NumbersToSum {
public static int sum(int ... numbersToSum) {
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
sum += numbersToSum[0];
}
return sum;
}
}
public class NumbersToSum {
public static int sum(int ... numbersToSum) {
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
sum += numbersToSum[i];
}
return sum;
}
}
The Art of Clean Code
BUG FIXED ! ! ! Test is GREEN
Reminder
public static void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String nToSIn = inputReader.readLine();
String[] nToS = nToSIn.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
str = str.substring(0, str.length() - 2);
str += "is " + sum;
System.out.println(str);
}
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one … sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one … sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one … sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one … sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
// print a line from the user
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
waitForInputAndCopy(inputReader);
} else if ("sum".equals(input)) {
// read numbers separated by space and print their sum
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
}
private void waitForInputAndCopy(BufferedReader inputReader) throws IOException {
System.out.print(">");
System.out.println(inputReader.readLine());
}
}
public class MyConsole {
public void go() throws IOException {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Please enter one of the following actions: sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
waitForInputAndCopy(inputReader);
} else if ("sum".equals(input)) {
waitForInputAndSumNumbers(inputReader);
}
}
}
private void waitForInputAndSumNumbers(BufferedReader inputReader) throws IOException {
System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
private void waitForInputAndCopy(BufferedReader inputReader) throws IOException {
System.out.print(">");
System.out.println(inputReader.readLine());
}
}
Some Basic
Rules
Use Pronounceable, Meaningful Names
String numbersToSumInput = inputReader.readLine();
String[] nToS = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < nToS.length; i++) {
str += nToS[i] + ", ";
sum += Integer.parseInt(nToS[0]);
}
vs.
String numbersToSumInput = inputReader.readLine();
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
Do Only One Thing
FUNCTIONS / CLASSES / MODULES SHOULD DO ONE THING.
THEY SHOULD DO IT WELL.
THEY SHOULD DO IT ONLY.
while (true) {
System.out.println("Please enter one … sum / copycat");
String input = inputReader.readLine();
if ("copycat".equals(input)) {
System.out.print(">");
System.out.println(inputReader.readLine());
} else if ("sum".equals(input)) {
System.out.println("Enter numbers to sum ...");
String numbersToSumInput = inputReader.readLine();
String str = NumbersToSumPrinter.getOutputString(numbersToSumInput);
System.out.println(str);
}
}
No Need for Comments
Formatting
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
public class NumbersToSum {
public static String getOutputString(String … numbersToSumInput) {
String[] numbersToSum = numbersToSumInput.split(" ");
String str = "Sum of ";
int sum = 0;
for (int i = 0; i < numbersToSum.length; i++) {
str += numbersToSum[i] + ", ";
sum += Integer.parseInt(numbersToSum[0]);
}
str = str.substring(0, str.length() - 2);
str += " is " + sum;
return str;
}
}
DO NOT Treat Test Code Differently
Culture
Insights
● Reading Code is Hard
● Code Rots
Refactor Until Your Code is Clean
● Hard to Get Done Right on First Try
● Reading clean code should make you smile
the way a well-crafted music box or well-designed car would
Leave the Campground
Cleaner Than You’ve Found it
● Change one variable name for the better,
● Break up one function that’s a little too large,
● Eliminate one small bit of duplication
The cleanup doesn’t have to be something big:
No Ego
I’m here to learn
Smart Ass
(Don’t be one)
How YOU can take it from here
● Make it a priority
● It seems to take longer
● Read / see about it
● Talk to your peers
Questions?
Yael Zaritsky yaelzyaelz@wix.com
yaelzaritsky@gmail.com
Thank You
Yael Zaritsky yaelzyaelz@wix.com
yaelzaritsky@gmail.com
Sources
Clean Code (pdf)
GOOS style TDD by Example - Sagy Rozman
Cultural Learning of Testing - Gil Tayar
Finding Your Organization’s Code Deodorant - Ittai Zeidman
private static int[] intArrayFromStrings(String[] numbersToSum) {
int[] intArray = new int[numbersToSum.length];
for(int i=0 ; i<numbersToSum.length ; i++) {
intArray[i]=Integer.parseInt(numbersToSum[i]);
}
return intArray;
}
}

More Related Content

DOCX
Java binary subtraction
Charm Sasi
 
PDF
[WELC] 21. I’m Changing the Same Code All Over the Place
종빈 오
 
PPTX
String in .net
Larry Nung
 
PDF
Python Cheat Sheet
Muthu Vinayagam
 
PDF
Binary Reading in C#
Yoshifumi Kawai
 
PDF
The Ring programming language version 1.9 book - Part 53 of 210
Mahmoud Samir Fayed
 
PDF
Go vs C++ - CppRussia 2019 Piter BoF
Timur Safin
 
PPTX
Blockchain - a simple implementation
Commit Software Sh.p.k.
 
Java binary subtraction
Charm Sasi
 
[WELC] 21. I’m Changing the Same Code All Over the Place
종빈 오
 
String in .net
Larry Nung
 
Python Cheat Sheet
Muthu Vinayagam
 
Binary Reading in C#
Yoshifumi Kawai
 
The Ring programming language version 1.9 book - Part 53 of 210
Mahmoud Samir Fayed
 
Go vs C++ - CppRussia 2019 Piter BoF
Timur Safin
 
Blockchain - a simple implementation
Commit Software Sh.p.k.
 

What's hot (20)

PDF
Application-Specific Models and Pointcuts using a Logic Meta Language
ESUG
 
PDF
Python 2.5 reference card (2009)
gekiaruj
 
PDF
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
PDF
Python3 cheatsheet
Gil Cohen
 
PDF
Some examples of the 64-bit code errors
PVS-Studio
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PDF
Cheat sheet python3
sxw2k
 
PPT
PDBC
Sunil OS
 
KEY
Haskellで学ぶ関数型言語
ikdysfm
 
PDF
Beginners python cheat sheet - Basic knowledge
O T
 
PDF
Network security
babyangle
 
PDF
MySQL 5.7 NF – JSON Datatype 활용
I Goo Lee
 
PDF
Introducción a Elixir
Svet Ivantchev
 
PDF
The Ring programming language version 1.5.2 book - Part 45 of 181
Mahmoud Samir Fayed
 
PDF
Java Basics - Part1
Vani Kandhasamy
 
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
DOCX
C programs
Azaj Khan
 
PDF
Python Programming: Data Structure
Chan Shik Lim
 
DOCX
Mcq cpup
tahir_ali786
 
PPTX
Dive into EXPLAIN - PostgreSql
Dmytro Shylovskyi
 
Application-Specific Models and Pointcuts using a Logic Meta Language
ESUG
 
Python 2.5 reference card (2009)
gekiaruj
 
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
Python3 cheatsheet
Gil Cohen
 
Some examples of the 64-bit code errors
PVS-Studio
 
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Cheat sheet python3
sxw2k
 
PDBC
Sunil OS
 
Haskellで学ぶ関数型言語
ikdysfm
 
Beginners python cheat sheet - Basic knowledge
O T
 
Network security
babyangle
 
MySQL 5.7 NF – JSON Datatype 활용
I Goo Lee
 
Introducción a Elixir
Svet Ivantchev
 
The Ring programming language version 1.5.2 book - Part 45 of 181
Mahmoud Samir Fayed
 
Java Basics - Part1
Vani Kandhasamy
 
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
C programs
Azaj Khan
 
Python Programming: Data Structure
Chan Shik Lim
 
Mcq cpup
tahir_ali786
 
Dive into EXPLAIN - PostgreSql
Dmytro Shylovskyi
 
Ad

Similar to The Art of Clean Code (20)

DOCX
Main Java[All of the Base Concepts}.docx
adhitya5119
 
DOCX
Java Program
Sudeep Singh
 
PDF
Microsoft word java
Ravi Purohit
 
PPTX
Presentation1 computer shaan
walia Shaan
 
PDF
Simple Java Program for beginner with easy method.pdf
ashwinibhosale27
 
PDF
Sam wd programs
Soumya Behera
 
DOCX
All Of My Java Codes With A Sample Output.docx
adhitya5119
 
DOCX
Java Practical1 based on Basic assignment
ashwinibhosale27
 
DOCX
Programs of java
shafiq sangi
 
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
PPTX
บทที่ 3 พื้นฐานภาษา Java
Itslvle Parin
 
DOCX
Example of JAVA Program
Trenton Asbury
 
PDF
Simple 27 Java Program on basic java syntax
ashwinibhosale27
 
PPTX
Reading and writting
andreeamolnar
 
DOC
Find the output of the following code (Java for ICSE)
Mokshya Priyadarshee
 
PDF
Java Simple Programs
Upender Upr
 
PPTX
Java basics variables
JoeReddieMedia
 
PDF
Accepts number and base and returns result in integerimport jav.pdf
asif1401
 
PPT
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
جامعة القدس المفتوحة
 
PDF
CODEimport java.util.; public class test { public static voi.pdf
anurag1231
 
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Java Program
Sudeep Singh
 
Microsoft word java
Ravi Purohit
 
Presentation1 computer shaan
walia Shaan
 
Simple Java Program for beginner with easy method.pdf
ashwinibhosale27
 
Sam wd programs
Soumya Behera
 
All Of My Java Codes With A Sample Output.docx
adhitya5119
 
Java Practical1 based on Basic assignment
ashwinibhosale27
 
Programs of java
shafiq sangi
 
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
บทที่ 3 พื้นฐานภาษา Java
Itslvle Parin
 
Example of JAVA Program
Trenton Asbury
 
Simple 27 Java Program on basic java syntax
ashwinibhosale27
 
Reading and writting
andreeamolnar
 
Find the output of the following code (Java for ICSE)
Mokshya Priyadarshee
 
Java Simple Programs
Upender Upr
 
Java basics variables
JoeReddieMedia
 
Accepts number and base and returns result in integerimport jav.pdf
asif1401
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
جامعة القدس المفتوحة
 
CODEimport java.util.; public class test { public static voi.pdf
anurag1231
 
Ad

Recently uploaded (20)

PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Presentation about variables and constant.pptx
kr2589474
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Presentation about variables and constant.pptx
safalsingh810
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Bandai Playdia The Book - David Glotz
BluePanther6
 

The Art of Clean Code

  • 7. First Feature - Product
  • 8. First Feature - Code public class MyConsole { public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("> "); System.out.println(inputReader.readLine()); } } } public class MyConsole { public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("> "); System.out.println(inputReader.readLine()); } } } public class MyConsole { public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("> "); System.out.println(inputReader.readLine()); } } }
  • 10. public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of ... sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { System.out.print(">"); System.out.println(inputReader.readLine()); } public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of ... sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { System.out.print(">"); System.out.println(inputReader.readLine()); } Second Feature - Code else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }…}
  • 11. public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of … sum/copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } } }
  • 14. Agenda ● What is Clean Code? ● Example ● Some Basic Rules ● Culture
  • 16. KISS
  • 17. KISS
  • 19. public class NumberTest { @Test public void testMultiplyByTwo() { assertThat(Number.multiplyByTwo(2),is(4)); } } public class Number { public static int multiplyByTwo(int number) { return number; } } public class NumberTest { @Test public void testMultiplyByTwo() { assertThat(Number.multiplyByTwo(2),is(4)); } } public class Number { public static int multiplyByTwo(int number) { return number; } } public class NumberTest { @Test public void testMultiplyByTwo() { assertThat(Number.multiplyByTwo(2),is(4)); } } public class Number { public static int multiplyByTwo(int number) { return number; } }
  • 20. public class NumberTest { @Test public void testMultiplyByTwo() { assertThat(Number.multiplyByTwo(2),is(4)); } } public class Number { public static int multiplyByTwo(int number) { return number * 2; } } public class NumberTest { @Test public void testMultiplyByTwo() { assertThat(Number.multiplyByTwo(2),is(4)); } } public class Number { public static int multiplyByTwo(int number) { return number * 2; } }
  • 23. public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'"); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } } } public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'"); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } } } public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'"); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } } }
  • 28. else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); }
  • 29. Make sure that func("3 3") returns "Sum of 3, 3 is 6"
  • 30. else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String str = NumbersToSum.getOutputString(nToSIn); System.out.println(str); }
  • 31. public class NumbersToSum { public static String getOutputString(String … nToSIn) { String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; return str; }} else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String str = NumbersToSum.getOutputString(nToSIn); System.out.println(str); }
  • 32. @Test public void testGetOutputStringForSingleNumber() { assertThat(NumbersToSum.getOutputString("3 3"), is("Sum of 3, 3 is 6")); }
  • 34. public class NumbersToSum { public static String getOutputString(String … nToSIn) { String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … nToSIn) { String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 37. BUG FIXED ! ! ! Test is GREEN
  • 38. public class NumbersToSum { public static String getOutputString(String … nToSIn) { String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String nToSIn = inputReader.readLine(); String str = NumbersToSum.getOutputString(nToSIn); System.out.println(str); } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSum.getOutputString(numbersToSumInput); System.out.println(str); } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSum.getOutputString(numbersToSumInput); System.out.println(str); } public class NumbersToSum { public static String getOutputString(String … nToSIn) { String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] nToS = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] nToS = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 39. public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 42. public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 43. @Test public void testGetOutputString() { assertThat(NumbersToSumPrinter.getOutputString("3 231"), is("Sum of 3, 231 is 234")); } @Test public void testSumOfMoreThanOneNumber() { assertThat(NumbersToSum.sum(3, 231), is(234)); }
  • 44. public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 45. public class NumbersToSumPrinter { public static String getOutputString(String numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; } str = str.substring(0, str.length() - 2); int sum = NumbersToSum.sum(intArrayFromStrings(numbersToSum)); str += " is " + sum; return str; } }
  • 46. public class NumbersToSumPrinter { public static String getOutputString(String numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; } str = str.substring(0, str.length() - 2); int sum = NumbersToSum.sum(intArrayFromStrings(numbersToSum)); str += " is " + sum; return str; } }
  • 47. public class NumbersToSum { public static int sum(int ... numbersToSum) { int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { sum += numbersToSum[0]; } return sum; } }
  • 49. public class NumbersToSum { public static int sum(int ... numbersToSum) { int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { sum += numbersToSum[0]; } return sum; } }
  • 50. public class NumbersToSum { public static int sum(int ... numbersToSum) { int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { sum += numbersToSum[i]; } return sum; } }
  • 52. BUG FIXED ! ! ! Test is GREEN
  • 54. public static void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String nToSIn = inputReader.readLine(); String[] nToS = nToSIn.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } str = str.substring(0, str.length() - 2); str += "is " + sum; System.out.println(str); } } }
  • 55. public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one … sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } } } }
  • 56. public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one … sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } } } }
  • 57. public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one … sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } } } } public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one … sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { // print a line from the user System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } } } }
  • 58. public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { waitForInputAndCopy(inputReader); } else if ("sum".equals(input)) { // read numbers separated by space and print their sum System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } } } private void waitForInputAndCopy(BufferedReader inputReader) throws IOException { System.out.print(">"); System.out.println(inputReader.readLine()); } }
  • 59. public class MyConsole { public void go() throws IOException { BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Please enter one of the following actions: sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { waitForInputAndCopy(inputReader); } else if ("sum".equals(input)) { waitForInputAndSumNumbers(inputReader); } } } private void waitForInputAndSumNumbers(BufferedReader inputReader) throws IOException { System.out.println("Enter numbers to sum, separated by space. For example: '233 67 8 456086'"); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } private void waitForInputAndCopy(BufferedReader inputReader) throws IOException { System.out.print(">"); System.out.println(inputReader.readLine()); } }
  • 61. Use Pronounceable, Meaningful Names String numbersToSumInput = inputReader.readLine(); String[] nToS = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < nToS.length; i++) { str += nToS[i] + ", "; sum += Integer.parseInt(nToS[0]); } vs. String numbersToSumInput = inputReader.readLine(); String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); }
  • 62. Do Only One Thing FUNCTIONS / CLASSES / MODULES SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY. while (true) { System.out.println("Please enter one … sum / copycat"); String input = inputReader.readLine(); if ("copycat".equals(input)) { System.out.print(">"); System.out.println(inputReader.readLine()); } else if ("sum".equals(input)) { System.out.println("Enter numbers to sum ..."); String numbersToSumInput = inputReader.readLine(); String str = NumbersToSumPrinter.getOutputString(numbersToSumInput); System.out.println(str); } }
  • 63. No Need for Comments
  • 64. Formatting public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } } public class NumbersToSum { public static String getOutputString(String … numbersToSumInput) { String[] numbersToSum = numbersToSumInput.split(" "); String str = "Sum of "; int sum = 0; for (int i = 0; i < numbersToSum.length; i++) { str += numbersToSum[i] + ", "; sum += Integer.parseInt(numbersToSum[0]); } str = str.substring(0, str.length() - 2); str += " is " + sum; return str; } }
  • 65. DO NOT Treat Test Code Differently
  • 67. Insights ● Reading Code is Hard ● Code Rots
  • 68. Refactor Until Your Code is Clean ● Hard to Get Done Right on First Try ● Reading clean code should make you smile the way a well-crafted music box or well-designed car would
  • 69. Leave the Campground Cleaner Than You’ve Found it
  • 70. ● Change one variable name for the better, ● Break up one function that’s a little too large, ● Eliminate one small bit of duplication The cleanup doesn’t have to be something big:
  • 71. No Ego I’m here to learn
  • 73. How YOU can take it from here ● Make it a priority ● It seems to take longer ● Read / see about it ● Talk to your peers
  • 77. Sources Clean Code (pdf) GOOS style TDD by Example - Sagy Rozman Cultural Learning of Testing - Gil Tayar Finding Your Organization’s Code Deodorant - Ittai Zeidman
  • 78. private static int[] intArrayFromStrings(String[] numbersToSum) { int[] intArray = new int[numbersToSum.length]; for(int i=0 ; i<numbersToSum.length ; i++) { intArray[i]=Integer.parseInt(numbersToSum[i]); } return intArray; } }