SlideShare a Scribd company logo
1 of 78
Download to read offline
The Art
of
Clean Code
The Basics
Our Goal: Preventing
This!
Yael Zaritsky-Peretz
Developer @ CI team @ Wix
Over
90 million
users
Wix
Over
1000
employees
~400
developers
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);
}
}
}
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
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"));
}
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;
}
}
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
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;
}
}
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;
}
}
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

What's hot

Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
gekiaruj
 

What's hot (20)

Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errors
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
PDBC
PDBCPDBC
PDBC
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 
Network security
Network securityNetwork security
Network security
 
MySQL 5.7 NF – JSON Datatype 활용
MySQL 5.7 NF – JSON Datatype 활용MySQL 5.7 NF – JSON Datatype 활용
MySQL 5.7 NF – JSON Datatype 활용
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
The Ring programming language version 1.5.2 book - Part 45 of 181
The Ring programming language version 1.5.2 book - Part 45 of 181The Ring programming language version 1.5.2 book - Part 45 of 181
The Ring programming language version 1.5.2 book - Part 45 of 181
 
Java Basics - Part1
Java Basics - Part1Java Basics - Part1
Java Basics - Part1
 
The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184
 
C programs
C programsC programs
C programs
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
Dive into EXPLAIN - PostgreSql
Dive into EXPLAIN  - PostgreSqlDive into EXPLAIN  - PostgreSql
Dive into EXPLAIN - PostgreSql
 

Similar to The Art of Clean Code

Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
anandhomeneeds
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
RonnBlack
 
import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdf
manojmozy
 
Below is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdfBelow is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdf
alankarshoe84
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
anujsharmaanuj14
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
Trenton Asbury
 

Similar to The Art of Clean Code (20)

Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
 
Studyx4
Studyx4Studyx4
Studyx4
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 
import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdf
 
Ann
AnnAnn
Ann
 
Utility.ppt
Utility.pptUtility.ppt
Utility.ppt
 
Below is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdfBelow is my code for a line editor import java.io.BufferedReader;.pdf
Below is my code for a line editor import java.io.BufferedReader;.pdf
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
 
Los dskn
Los dsknLos dskn
Los dskn
 
Code javascript
Code javascriptCode javascript
Code javascript
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

The Art of Clean Code

  • 2.
  • 6.
  • 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); } } }
  • 12.
  • 13.
  • 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); } } }
  • 25.
  • 26.
  • 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")); }
  • 33.
  • 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; } }
  • 35.
  • 36.
  • 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; } }
  • 41.
  • 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; } }
  • 48.
  • 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; } }
  • 51.
  • 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
  • 76. Thank You Yael Zaritsky yaelzyaelz@wix.com yaelzaritsky@gmail.com
  • 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; } }