27 Kasım 2020 Cuma

Clean Code - Functions (Uncle Bob)

Giriş
Şeklen şöyle. Buradaki başlıklar
- General Rules
- Design Rules
- Understandability tips
- Name Rules
- Function Rules
- Comment Rules
- Source code structure
- Object and data structures
- Tests
- Code smells
Function için kitaptaki başlıklar şöyle
Small
Do One Thing
One Level of Abstraction per Function
Reading Code from Top to Bottom: The Stepdown Rule
Switch Statements
Use Descriptive Names
Function Arguments : Flag Arguments, Dyadic Functions, Triads, Argument Objects
Have No Side Effects : Output Arguments
Command Query Separation
Prefer Exceptions to Returning Error Codes
Don’t Repeat Yourself
Structured Programming
Top to Bottom
Açıklaması şöyle
In a crux, your logic should be written as a hierarchy of methods, like an article, let’s say you are reading an article by the headline you can understand what is all about? after reading the first paragraph you will have a crisp understanding of the topic and if you want details then go the other paragraphs.

The same way your methods must be structured. The top-level method must say about the feature, if you go to the next level it will tell about the algorithm, lastly, it will give the implementation details. I like to compose complex logic as a Template method pattern. 
Örnek
Şöyle yaparız
public void displayEvenNumberFromInputSource(){         
  List<Integer> numberList =  processInputSource();
  List<Integer> evenList = findEven(numberList);
  display(evenList);
}
Switch Statements
Örnek
Elimizde şöyle bir kod olsun
public Money calculatePay(Employee e) throws InvalidEmployeeType {
  switch (e.type) {
    case COMMISSIONED:
      return calculateCommissionedPay(e);
    case HOURLY:
      return calculateHourlyPay(e);
    case SALARIED:
      return calculateSalariedPay(e);
    default: 
      throw new InvalidEmployeeType(e.type);
  }
}
Açıklaması şöyle. Burada birden fazla "iş" yapma cümlesine itiraz ediliyor.
There are several problems with this function. First, it's large, and when new employee types are added, it will grow. Second, it very clear does more than more thing. Third, it violates the Single Responsibility Principle(SRP) because there is more than one reason for it to change. Fourth, it violates the Open Closed Principle(OCP) because it must change whenever new types are added. But possibly the worst problem with this function is that there are an unlimited number of other functions that will have the same structure.
Bu kod şu hale getirilebilir. Ancak yine birden fazla "iş" yapma devam ediyor. Aslında "birden" fazla iş yapma durumu anlayışa bağlı.
public Employee createEmployee(int employeeType) throws InvalidEmployeeType {
  switch (employeeType) {
    case COMMISSIONED:
      return new CommissionEmployee(e);
    case HOURLY:
      return new HourlyEmployee(e);
    case SALARIED:
      return new SalariedEmployee(e);
    default: 
      throw new InvalidEmployeeType(e.type);
  }
}

public Money calculatePay(int employeeType) throws InvalidEmployeeType {
  Employee e = createEmployee(e.type);
  return e.calculatePay();
}
Function Arguments
Number of Arguments yazısına taşıdım

Hiç yorum yok:

Yorum Gönder