SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Adegoke Obasa
Clean Code
What is Clean Code
“Clean code is a code that is written by
someone who cares” - Micheal Feathers
It is written by someone who has treated it as
an art and paid attention to all details.
Focused
- SRP (Single Responsibility Principle)
- Must do one thing only
Not Redundant
- DRY rule (Don’t Repeat Yourself)
Pleasant
- Harry Potter :)
- Lord Of The Rings
- Asa’s Tunes
- KISS principle (Keep It Simple, Stupid!)
- YAGNI principle (You Ain’t Gonna Need It)
Easily Extended
- You write code for other developers
- You could be that other developer
Smaller is Better
- There should be a standard for max number of lines in a method.
- Example
- Methods - Avg. 30 code lines
- Class - < 30 Methods
- Packages - <= 30 Classes
Others
- Unit Tests
- Loose Coupling
- High Cohesion
- Design Patterns
- Frameworks
Demo
public class Xbps {
public void go(String s) {
List l = prm.ld(s);
List l2 = copy(l);
for (int i = 0; i < l.size(); i++) {
String p = (String) l.get(i);
if (p.substring(5, 9).equals("9982")) {
l2.remove(p);
}
}
prm.sv(s, l2);
}...
public class ParametersValidator {
public void go(String s) {
List l = prm.ld(s);
List l2 = copy(l);
for (int i = 0; i < l.size(); i++) {
String p = (String) l.get(i);
if (!p.substring(5, 9).equals("9982")) {
l.remove(p);
}
}
prm.sv(s, l2);
}
...
public class ParametersValidator {
public void go(String s) {
List l = parameterStore.load(s);
List l2 = copy(l);
for (int i = 0; i < l.size(); i++) {
String p = (String) l.get(i);
if (!p.substring(5, 9).equals("9982")) {
l.remove(p);
}
}
parameterStore.save(s, l2);
}
...
public class ParametersValidator {
public void go(String webServiceName) {
List parameters =
parameterStore.load(webServiceName);
List validParams = copy(parameters);
for (int i = 0; i < parameters.size(); i++) {
String parameter = (String) parameters.get(i);
if (!parameter.substring(5, 9).equals("9982"))
{
validParams.remove(parameter);
}
}
parameterStore.save(webServiceName, validParams);
}
...
}
public class ParametersValidator {
public void removeInvalidParameters(String
webServiceName) {
List parameters =
parameterStore.load(webServiceName);
List validParams = copy(parameters);
for (int i = 0; i < parameters.size(); i++) {
String parameter = (String) parameters.get(i);
if (isInvalid(parameter)) {
validParams.remove(parameter);
}
}
parameterStore.save(webServiceName, validParams);
}
Lessons
- Good Naming Conventions
- PascalCasing
- camelCasing
- snake_casing
- Good Code Formatting
- Coding Standards
- Good Comments
- Not too Much
Q & A
Further Reading
http://blog.goyello.com/2013/01/21/top-9-
principles-clean-code/
..on Software
<http://bodtoki.blogspot.com/2010/12/1-good-
names-good-code-series.html>
Thanks :)
Adegoke Obasa
@goke_epapa

Weitere ähnliche Inhalte

Ähnlich wie Clean code

1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basics
retronym
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
OXUS 20
 
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
Spark Summit
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная обертка
Vsevolod Dyomkin
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
agnonchik
 

Ähnlich wie Clean code (20)

Presentation about RSA
Presentation about RSAPresentation about RSA
Presentation about RSA
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp language
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
 
Frsa
FrsaFrsa
Frsa
 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basics
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 
TRICK
TRICKTRICK
TRICK
 
What's new in Apache SystemML - Declarative Machine Learning
What's new in Apache SystemML  - Declarative Machine LearningWhat's new in Apache SystemML  - Declarative Machine Learning
What's new in Apache SystemML - Declarative Machine Learning
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
 
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
Spark as the Gateway Drug to Typed Functional Programming: Spark Summit East ...
 
Beyond Design Principles and Patterns
Beyond Design Principles and PatternsBeyond Design Principles and Patterns
Beyond Design Principles and Patterns
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
[DSC 2016] 系列活動:李泳泉 / 星火燎原 - Spark 機器學習初探
[DSC 2016] 系列活動:李泳泉 / 星火燎原 - Spark 機器學習初探[DSC 2016] 系列活動:李泳泉 / 星火燎原 - Spark 機器學習初探
[DSC 2016] 系列活動:李泳泉 / 星火燎原 - Spark 機器學習初探
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная обертка
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Xm lparsers
Xm lparsersXm lparsers
Xm lparsers
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
RSA-W7(rsa) d1-d2
RSA-W7(rsa) d1-d2RSA-W7(rsa) d1-d2
RSA-W7(rsa) d1-d2
 

Clean code

  • 2. What is Clean Code “Clean code is a code that is written by someone who cares” - Micheal Feathers It is written by someone who has treated it as an art and paid attention to all details.
  • 3. Focused - SRP (Single Responsibility Principle) - Must do one thing only
  • 4. Not Redundant - DRY rule (Don’t Repeat Yourself)
  • 5. Pleasant - Harry Potter :) - Lord Of The Rings - Asa’s Tunes - KISS principle (Keep It Simple, Stupid!) - YAGNI principle (You Ain’t Gonna Need It)
  • 6. Easily Extended - You write code for other developers - You could be that other developer
  • 7. Smaller is Better - There should be a standard for max number of lines in a method. - Example - Methods - Avg. 30 code lines - Class - < 30 Methods - Packages - <= 30 Classes
  • 8. Others - Unit Tests - Loose Coupling - High Cohesion - Design Patterns - Frameworks
  • 10. public class Xbps { public void go(String s) { List l = prm.ld(s); List l2 = copy(l); for (int i = 0; i < l.size(); i++) { String p = (String) l.get(i); if (p.substring(5, 9).equals("9982")) { l2.remove(p); } } prm.sv(s, l2); }...
  • 11. public class ParametersValidator { public void go(String s) { List l = prm.ld(s); List l2 = copy(l); for (int i = 0; i < l.size(); i++) { String p = (String) l.get(i); if (!p.substring(5, 9).equals("9982")) { l.remove(p); } } prm.sv(s, l2); } ...
  • 12. public class ParametersValidator { public void go(String s) { List l = parameterStore.load(s); List l2 = copy(l); for (int i = 0; i < l.size(); i++) { String p = (String) l.get(i); if (!p.substring(5, 9).equals("9982")) { l.remove(p); } } parameterStore.save(s, l2); } ...
  • 13. public class ParametersValidator { public void go(String webServiceName) { List parameters = parameterStore.load(webServiceName); List validParams = copy(parameters); for (int i = 0; i < parameters.size(); i++) { String parameter = (String) parameters.get(i); if (!parameter.substring(5, 9).equals("9982")) { validParams.remove(parameter); } } parameterStore.save(webServiceName, validParams); } ... }
  • 14. public class ParametersValidator { public void removeInvalidParameters(String webServiceName) { List parameters = parameterStore.load(webServiceName); List validParams = copy(parameters); for (int i = 0; i < parameters.size(); i++) { String parameter = (String) parameters.get(i); if (isInvalid(parameter)) { validParams.remove(parameter); } } parameterStore.save(webServiceName, validParams); }
  • 15. Lessons - Good Naming Conventions - PascalCasing - camelCasing - snake_casing - Good Code Formatting - Coding Standards - Good Comments - Not too Much
  • 16. Q & A

Hinweis der Redaktion

  1. Shortly speaking, we can say that SRP (according to some well-known definitions) is about making sure that if you can think of the reason for changing a class you should not be able to come up with more than one.
  2. When the DRY principle has successfully been applied, the modification of any single element of a system doesn’t require a change in any other logically unrelated elements.
  3. To achieve this you should try to comply with the KISS principle (Keep It Simple, Stupid!) and YAGNI principle (You Ain’t Gonna Need It). The KISS principle states that most systems work best if they are kept simple rather than made complex. Therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. YAGNI is a practice encouraging to purely focus on the simplest things that make your software work.
  4. You don’t write code for yourself , or worse - for a compiler. You write code for other developers. Don’t be selfish – think about the others. Don’t torture other developers by producing a hardly maintainable and extendable code. Besides, in some months time you could be that “other developer” yourself.
  5. Code should be minimal. Both classes and methods should be short, preferably just a few lines of code. It should be well divided (also within one class). The better you divide your code the easier it becomes to read it. This principle might positively influence point 4 – it will make it easier for other developers to understand your code.