Codigo Limpo — Epub
<h3>One level of abstraction per function</h3> <p>Mixing high-level business logic with low-level file I/O is a common smell.</p> <div class="bad"> <pre>void processOrder(Order order) { // high level validateOrder(order); // low level Files.writeString(Path.of("log.txt"), "Processing"); // high level again applyDiscount(order); }</pre> </div>
<h3>Small! Really small</h3> <p>An entire function should rarely exceed 20 lines. If you need a comment to explain a block inside a function, extract that block into a new function.</p>
<ul> <li><strong>Vertical density</strong>: Related concepts should be close. Local variables at the top of a function, helper functions directly below the calling function.</li> <li><strong>Horizontal spacing</strong>: Use spaces around operators (<code>a + b</code>), not tabs or chaotic mixtures. One indentation level = 4 spaces.</li> <li><strong>Team rules</strong>: If you work in a team, agree on an automatic formatter (Prettier, Black, gofmt).</li> </ul>
<h2>6. Error Handling: Separate Logic from Errors</h2> <p>Error handling is one thing. Your business logic is another. Don’t mix them.</p> codigo limpo epub
<h2>Conclusion: Professionalism</h2> <p>Clean code is not perfectionism—it’s respect for your future self and your teammates. Leave the codebase better than you found it. Refactor continuously, review each other’s work, and never accept “it works” as a substitute for “it’s clean.”</p>
<div class="bad"> <code>// increment i by 1<br />i++;</code> </div> <div class="good"> <code>// Retry up to 3 times due to eventual consistency in payment service<br />for (int attempt = 0; attempt < 3; attempt++) { ... }</code> </div>
<h2>7. Boundaries: Keep Third-Party Code at Arm’s Length</h2> <p>Wrap external APIs (libraries, frameworks) to isolate changes. Do not let a specific JSON library leak into your core domain.</p> Local variables at the top of a function,
<div class="good"> <pre>// Instead of using Gson directly everywhere: public interface JsonParser { <T> T fromJson(String json, Class<T> type); } // Implement with Gson, Jackson, or System.Text.Json later.</pre> </div>
<ul> <li><strong>Law of Demeter</strong>: Don’t chain deeply: <code>customer.getAddress().getCity().toString()</code> is fragile. Prefer <code>customer.getCityAsString()</code>.</li> <li><strong>Hybrids (half-object, half-structure)</strong>: Avoid adding business logic inside getters/setters. Getters should not perform complex calculations.</li> </ul>
<h2>1. Meaningful Names</h2> <p>Names are the smallest building blocks of code clarity. Every variable, function, or class name should reveal intent.</p> Your business logic is another
<h2>4. Formatting: Vertical Density and Horizontal Flow</h2> <p>Code formatting is communication. Use consistent rules.</p>
<h3>Rule 1: Use intention-revealing names</h3> <div class="bad"> <code>int d; // elapsed time in days</code> </div> <div class="good"> <code>int elapsedTimeInDays;</code> </div>