2 Ekim 2019 Çarşamba

Clean Code - Meaningful Names (Uncle Bob)

Giriş
Clean Code kitabındaki konuları görsel olarak özetleyen bir şekil şöyle. Burada iki madde önemli
- Prefer meaningful names over comments
- Use meaningful names
Bunun öneminin açıklaması şöyle
“…the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code.

Because this ratio is so high, we want the reading of code to be easy even if it makes the writing harder.”
Başlıklar
Kitaptaki başlıklar şöyle
Use Intention-Revealing Names
Avoid Disinformation
Make Meaningful Distinctions
Use Pronounceable Names
Use Searchable Names
Avoid Encodings
Hungarian Notation
Member Prefixes
Interfaces and Implementations
Avoid Mental Mapping
Class Names
Method Names
Don’t Be Cute
Pick One Word per Concept
Don’t Pun
Use Solution Domain Names
Use Problem Domain Names
Add Meaningful Context
Don’t Add Gratuitous Context
Sınıf İsimleri Nasıl Olmalı
Util, Helper, Manager gibi sınıf isimleri kabul edilebilir. Burada dikkat edilmesi gereken nokta bu sınıfların işlev odaklı olması. Yani her şeyin içine tıkıştırılmaması. Açıklaması şöyle
Both Java and C# have the existence proof that this works: the Math class.

The danger is the "kitchen drawer" problem, where a class simply gathers new kitchen tools and implements, many of which never get used, or only get used once.

So as long as you can keep such classes tightly focused on a theme (like the Math class is), I don't see a problem. In fact, static methods in classes of this kind tend to be easier to write, maintain and test, so long as they're always "pure" methods (i.e. referentially transparent, and you don't store static state in them).

Metod İsimleri Nasıl Olmalı
Giriş
Açıklaması şöyle.
Methods should have verb or verb phrase names like postPayment, deletePage, or save.
Örnek
Eğer metodun ne olduğu direkt belli ise o işe belirten bir isim kullanılmalı. Örneğin createPassword() gibi.

Örnek
Elimizde şöyle bir kod olsun. Bu metod setter değil. Dolayısıyla ismi yaptığı işi karşılamıyor. İsmi load() olsaydı çok daha iyi olurdu.
public void setPddocument() {
        
  PDDocument pddocument = null;
  try {
    pddocument = org.apache.pdfbox.pdmodel.PDDocument.load(this.bytearray);
  } catch (IOException e) {
    e.printStackTrace();
  }   
  this.pddocument = pddocument;
}
Örnek - Var Olma Koşulu Testi Kodları
Bir şeyin mevcut olduğunu anlamak için hasMoney() metod ismi kullanılabilir.

Örnek - Koşul Testi Kodları
Bir koşulu test etmek için canFire()shouldFire(), isApproved() gibi metod isimleri kullanılabilir.

Örnek - Koşula Bağlı Kodlar
Bir koşulu test etmek ve koşul sağlanıyorsa arkasında bir işlem yapmak için shouldSwitchOff()canSwitchOff() gibi bir isim kullanılabilir. Şu tarz metod isimleri bence iyi değil.
switchOffIfNecessary()
shoudSaveIfRequired()
Örnek - Processor Kodlar
Genel işlemler için processX()handleX() gibi isimler kullanılabilir.

Örnek - Event Source Kodlar
Event source kodlarda senkron/asenkron ayırımını yapmaya gerek varsa notifyX()informX() gibi metod isimleri iyi değil. Bence postX() daha iyi bir isim. Metodun asenkron olduğunu belli ediyor.

Örnek - Event İşleyen Kodlar
onX() gibi isimler bence iyi

Örnek
Eğer hata olma veya başarısız olma ihtimaline dikkat çekmek istersek şöyle yaparız. Buradaki try başarısız olma ihtimalini vurgula.
if (tryCreateFreshSnapshot())
{
  // ...
}

Hiç yorum yok:

Yorum Gönder