6 Mart 2019 Çarşamba

Clean Code - Number of Arguments (Uncle Bob)

Giriş
Şu cümleye dikkat etmek lazım.
The problem with Martin is his big influence, especially on junior developers. When you work a lot with others, then you have to deal with his ideas sooner or later, and that is becoming tedious, because his arguments are usually presented as magister dixit, ...
Metodlara az parametre geçmek için metodların sınıf içindeki üye alanları kullanması doğru değil.Açıklaması şöyle.
"The ideal number of arguments for a function is zero" is plain wrong. The ideal number of arguments is exactly the number needed to enable your function to be side-effect free. Less than that and you needlessly cause your functions to be impure thus forcing you to steer away from the pit of success and climb the gradient of pain. Sometimes "Uncle Bob" is spot on with his advice. Sometimes he is spectacularly wrong. His zero arguments advice is an example of the latter.
Monadic Metod - Tek Parametre Alan Metod
"Clean Code: A Handbook of Agile Software Craftsmanship" kitabındaki açıklama şöyle. Yani 0 parametre
The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.
Örnek
Şöyle yaparız
public void searchEmployee(EmployeeSearchInput input) { 

  Address address = findAdressFromLatLong(input.getLatLong());
  Department dept = finDeptbyID(input.getDeptId());
  //TODO :: populateEmployeeSearchInput using address and dept
  query = buildSearchquery(input);
  result = executeSearch(query);  
}
Örnek - Local Field Yerine Member Field Yanlışı
Elimizde şöyle bir kod olsun. Bu kodda metodlara az parametre geçiliyor ancak metod sonuçları üye alanlara atanıyor
public class SomeBusinessProcess {
  @Inject private Router router;
  @Inject private ServiceClient serviceClient;
  @Inject private CryptoService cryptoService;

  private byte[] encodedData;
  private EncryptionInfo encryptionInfo;
  private EncryptedObject payloadOfResponse;
  private URI destinationURI;

  public EncryptedResponse process(EncryptedRequest encryptedRequest) {
    checkNotNull(encryptedRequest);

    getEncodedData(encryptedRequest);
    getEncryptionInfo();
    getDestinationURI();
    passRequestToServiceClient();

    return cryptoService.encryptResponse(payloadOfResponse);
  }

  private void getEncodedData(EncryptedRequest encryptedRequest) {
    encodedData = cryptoService.decryptRequest(encryptedRequest, byte[].class);
  }

  private void getEncryptionInfo() {
    encryptionInfo = cryptoService.getEncryptionInfoForDefaultClient();
  }

  private void getDestinationURI() {
    destinationURI = router.getDestination().getUri();
  }

  private void passRequestToServiceClient() {
    payloadOfResponse = serviceClient.handle(destinationURI, encodedData, encryptionInfo);
  }
}
Üye aları kullanmak yerine şöyle yapmak gerekir.
public EncryptedResponse process(EncryptedRequest encryptedRequest) {
  checkNotNull(encryptedRequest);

  byte[] encodedData = getEncodedData();
  EncryptionInfo encryptionInfo = getEncryptionInfo();

  //and so on...

  return cryptoService.encryptResponse(payloadOfResponse);
}
Şu şekilde kodlayanlar da var ancak bu kod debugger'da çok problem çıkarır.
public EncryptedResponse process(EncryptedRequest request) {
    checkNotNull(encryptedRequest);

  return encryptResponse(
      routeTo(
          destination(),
          requestData(request),
          destinationEncryption()
        )
      );
  }

  private byte[] requestData(EncryptedRequest encryptedRequest) {
    return cryptoService.decryptRequest(encryptedRequest, byte[].class);
  }

  private EncryptionInfo destinationEncryption() {
    return cryptoService.getEncryptionInfoForDefaultClient();
  }

  private URI destination() {
    return router.getDestination().getUri();
  }

  private EncryptedObject routeTo(URI destinationURI, byte[] encodedData,
     EncryptionInfo encryptionInfo) {
    return serviceClient.handle(destinationURI, encodedData, encryptionInfo);
  }

  private void encryptResponse(EncryptedObject payloadOfResponse) {
    return cryptoService.encryptResponse(payloadOfResponse);
  }

Hiç yorum yok:

Yorum Gönder