Rules Hub

Coding Rules Library

← Back to all rules
backend ruleStack: general
clean-codereadabilitydesign-patternsfluent-interfacerefactoring

Prefer method chaining for fluent APIs

Utilize method chaining when working with fluent interfaces (like query builders) to avoid unnecessary intermediate variables.

PR: Feat/FCK-1888 - Create new articles by tag endpoint #1291Created: Dec 8, 2025

Bad example

Old codets
1const repo = new ArticleRepository();
2repo.filterByCategory('tech');
3repo.sortByDate('desc');
4
5const 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

New codets
1const 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.