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