agentleFS
emvnuelemvnuel/skills/cdd-design-pillars/SKILL.md

cdd-design-pillars

Skill2 starsChanged 11 months agoMITView on GitHub

Measures and limits cognitive load in Jakarta EE/MicroProfile code. Use when reviewing code complexity, designing services/entities, or identifying when to extract abstractions.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never bind external request parameters directly to domain objects

    line 47
  2. Distinctive:

    Never serialize domain objects directly to API responses

    line 48
  3. Distinctive:

    Use DTOs for input/output

    line 49
  4. Distinctive:

    Every object must be in a valid state from creation.

    line 53
  5. Distinctive:

    Don't modify references you didn't create.

    line 69

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

CDD Design Pillars for Jakarta EE

Design principles based on Cognitive Driven Development (CDD) and Alberto Souza's "Pilares Design Código", adapted for Jakarta EE/MicroProfile applications.

Core Philosophy

The best code is code that doesn't need to be written. When we must write code, it should function correctly through the simplest possible implementation that requires minimal mental effort to understand, maintain, and evolve.

Cognitive Load Theory

Code complexity is measured by intrinsic cognitive load — the mental effort required to understand code. Research suggests working memory holds ~7 items simultaneously.

Cognitive Load Points

ElementPoints
Custom class dependency+1
Branch (if, else, switch case)+1
Loop (for, while, forEach)+1
Try-catch block+1 per catch
Lambda/function transformation+1
Nested branch+1 additional

Load Limits by Component Type

ComponentMax PointsRationale
Domain Service / Application Service7Business flow must be crystal clear
Entity / Value Object9Can hold more complex state logic
Controller / Resource7Entry points should be simple
Repository implementation5Should be straightforward data access

Core Pillars

1. Quality is Non-Negotiable

Design proportional to complexity with current knowledge. Execute your code as fast as possible — implement outside-in.

2. Protect System Boundaries

  • External boundaries get maximum protection
  • Never bind external request parameters directly to domain objects
  • Never serialize domain objects directly to API responses
  • Use DTOs for input/output

3. Create Only Valid References

Every object must be in a valid state from creation. Use constructors and factory methods to enforce invariants.

4. Keep Operations in the Owning Class

Operations on data whose type is a language primitive should live inside the class that owns the data.

5. Every Indirection Must Earn Its Place

Each new class/file increases system complexity. Only create abstractions when cognitive load exceeds limits.

6. Explicit Business Rules

Business rules must be declared explicitly in code, not hidden in conditionals or scattered across layers.

7. Favor Encapsulation for Cohesion

Don't modify references you didn't create. Keep state manipulation inside the owning class.

Jakarta EE Quick Reference

JAX-RS Resource (≤7 points)

@Path("/orders")
@RequestScoped
public class OrderResource {

    @Inject
    private OrderService orderService;  // +1

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response create(@Valid CreateOrderRequest request) {  // +1
        Order order = orderService.create(request);  // +1
        return Response.created(/*uri*/).entity(OrderResponse.from(order)).build();
    }
    // Total: 3 points ✓
}

CDI Service (≤7 points)

@ApplicationScoped
public class PaymentService {

    @Inject
    private PaymentGateway gateway;  // +1

    @Inject
    private TransactionRepository transactions;  // +1

    public Transaction process(PaymentRequest request) {
        if (!request.isValid()) {  // +1 branch
            throw new InvalidPaymentException();
        }

        Payment payment = gateway.charge(request);  // +1
        Transaction tx = Transaction.from(payment);  // +1
        return transactions.save(tx);
    }
    // Total: 5 points ✓
}

JPA Entity with Encapsulated Logic (≤9 points)

@Entity
public class Order {

    @Id @GeneratedValue
    private Long id;

    private Instant expiresAt;

    @Enumerated(EnumType.STRING)
    private OrderStatus status;

    // ✓ Operation on owned data stays in the class
    public boolean acceptsModification() {
        return this.expiresAt.isAfter(Instant.now())
            && this.status == OrderStatus.PENDING;
    }

    // ✓ State change is encapsulated
    public void cancel() {
        if (!acceptsModification()) {
            throw new OrderCannotBeCancelledException();
        }
        this.status = OrderStatus.CANCELLED;
    }
}

Boundary Separation with DTOs

// Request DTO - external boundary
public record CreateOrderRequest(
    @NotBlank String customerId,
    @NotEmpty List<OrderItemRequest> items
) {
    public Order toEntity(Customer customer) {
        return new Order(customer, items.stream()
            .map(OrderItemRequest::toEntity)
            .toList());
    }
}

// Response DTO - external boundary
public record OrderResponse(
    String orderId,
    String status,
    BigDecimal total
) {
    public static OrderResponse from(Order order) {
        return new OrderResponse(
            order.getId().toString(),
            order.getStatus().name(),
            order.getTotal()
        );
    }
}

Anti-Patterns

❌ Logic Outside Owning Class

// BAD: Logic on Order's data lives outside Order
if (order.getExpiresAt().isBefore(Instant.now())) {
    return "Expired";
}

// GOOD: Ask the object
if (order.isExpired()) {
    return "Expired";
}

❌ Exceeding Cognitive Load

// BAD: Too many points for a service (~11 points)
public Result process(Request req) {
    List<Handler> handlers = this.handlers.stream()  // +1
        .map(h -> h.accepts(req))  // +1
        .filter(Optional::isPresent)  // +2 (filter + branch)
        .map(Optional::get)  // +1
        .collect(toList());  // +1

    for (Handler h : handlers) {  // +1
        try {  // +1
            return h.handle();
        } catch (NetworkException e) {  // +1
            log.error(...);
        } catch (Exception e) {  // +1
            log.error(...);
        }
    }
    return Result.failure();
}

// GOOD: Extract to reduce points
@Inject
private HandlerSelector handlerSelector;  // Extracted abstraction

public Result process(Request req) {
    List<Handler> handlers = handlerSelector.selectFor(req);  // +1
    return executeWithFallback(handlers, req);  // Extracted
}

Cookbook Index

Core Pillars

  • protect-boundaries - Protecting system boundaries
  • valid-state-only - Creating only valid references
  • explicit-business-rules - Making business rules explicit
  • favor-encapsulation - Encapsulation for cohesion

Cognitive Load Patterns

  • cognitive-load-limits - Measuring and limiting load
  • concentrate-operations - Keep operations in owning class
  • extract-abstractions - When to create new abstractions

Jakarta EE Application

  • boundary-separation - DTOs and boundary protection
  • rest-resource-design - JAX-RS with CDD
  • cdi-bean-design - CDI beans with cognitive limits
  • jpa-entity-design - Entities with encapsulated logic
  • validation-patterns - Bean Validation patterns

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.