Rules Hub
Coding Rules Library
Prefer method chaining for fluent APIs
Utilize method chaining when working with fluent interfaces (like query builders) to avoid unnecessary intermediate variables.
Bad example
| 1 | const repo = new ArticleRepository(); |
| 2 | repo.filterByCategory('tech'); |
| 3 | repo.sortByDate('desc'); |
| 4 |
|
| 5 | const articles = await repo.findAll(); |
Explanation (EN)
This code creates an intermediate variable solely to mutate its state step-by-step. It breaks the visual flow and adds imperative noise to what should be a declarative query definition.
Objašnjenje (HR)
Ovaj kod stvara privremenu varijablu isključivo kako bi mijenjao njezino stanje korak po korak. To prekida vizualni tok i dodaje imperativni šum onome što bi trebala biti deklarativna definicija upita.
Good example
| 1 | const articles = await new ArticleRepository() |
| 2 | .filterByCategory('tech') |
| 3 | .sortByDate('desc') |
| 4 | .findAll(); |
Explanation (EN)
Method chaining creates a single, readable pipeline that expresses the intent clearly. It eliminates the need for temporary variables and groups the configuration and execution into one cohesive unit.
Objašnjenje (HR)
Ulančavanje metoda stvara jedan čitljiv cjevovod koji jasno izražava namjeru. Eliminira potrebu za privremenim varijablama i grupira konfiguraciju i izvršavanje u jednu povezanu cjelinu.