Clarify 'One Dot per Line' instruction (#499)

Clarify the 'One Dot per Line' rule to include Law of Demeter.
This commit is contained in:
Søren Trudsø Mahon
2025-12-18 23:09:43 +01:00
committed by GitHub
parent bbb5117235
commit c3913bc6db

View File

@@ -151,7 +151,7 @@ First Class Collections: a class that contains an array as an attribute should n
``` ```
5. **One Dot per Line**: 5. **One Dot per Line**:
- Limit the number of method calls in a single line to improve readability and maintainability. - Avoid violating Law of Demeter by only having a single dot per line.
```csharp ```csharp
// Bad Example - Multiple dots in a single line // Bad Example - Multiple dots in a single line
@@ -160,11 +160,20 @@ First Class Collections: a class that contains an array as an attribute should n
// Do something with userEmail // Do something with userEmail
} }
// Good Example - One dot per line // Good Example - One dot per line
public class User {
public NormalizedEmail GetEmail() {
return NormalizedEmail.Create(/*...*/);
}
}
public class Order {
/*...*/
public NormalizedEmail ConfirmationEmail() {
return User.GetEmail();
}
}
public void ProcessOrder(Order order) { public void ProcessOrder(Order order) {
var user = order.User; var confirmationEmail = order.ConfirmationEmail();
var email = user.GetEmail(); // Do something with confirmationEmail
var userEmail = email.ToUpper().Trim();
// Do something with userEmail
} }
``` ```