Object Calisthenics.instructions
<!-- source: awesome-copilot-object-calisthenics — https://raw.githubusercontent.com/github/awesome-copilot/main/instructions/object-calisthenics.instructions.md --> --- applyTo: '**/*.{cs,ts,java}' description: Enforces Object Calisthenics principles for business domain code to ensure clean, maintainable, and robust code --- # Object Calisthenics Rules > ⚠️ **Warning:** This file contains the 9 original Object Calisthenics rules. No additional rules must be added, and none of these rules should be replaced or removed. > Examples may be added later if needed. ## Objective This rule enforces the principles of Object Calisthenics to ensure clean, maintainable, and robust code in the backend, **primarily for business domain code**. ## Scope and Application - **Primary focus**: Business domain classes (aggregates, entities, value objects, domain services) - **Secondary focus**: Application layer services and use case handlers - **Exemptions**: - DTOs (Data Transfer Objects) - API models/contracts - Configuration classes - Simple data containers without business logic - Infrastructure code where flexibility is needed ## Key Principles 1. **One Level of Indentation per Method**: - Ensure methods are simple and do not exceed one level of indentation. ```csharp // Bad Example - this method has multiple levels of indentation public void SendNewsletter() { foreach (var user in users) { if (user.IsActive) { // Do something mailer.Send(user.Email); } } } // Good Example - Extracted method to reduce indentation public void SendNewsletter() { foreach (var user in users) { SendEmail(user); } } private void SendEmail(User user) { if (user.IsActive) { mailer.Send(user.Email); } } // Good Example - Filtering users before sending emails public void SendNewsletter() { var activeUsers = users.Where(user => user.IsActive); foreach (var user in activeUsers) { mailer.Send(user.Email); } } ``` 2. **Don't Use the ELSE Keyword**: - Avoid using the `else` keyword to reduce complexity and improve readability. - Use early returns to handle conditions instead. - Use Fail Fast principle - Use Guard Clauses to validate inputs and conditions at the beginning of methods. ```csharp // Bad Example - Using else public void ProcessOrder(Order order) { if (order.IsValid) { // Process order } else { // Handle invalid order } } // Good Example - Avoiding else public void ProcessOrder(Order order) { if (!order.IsValid) return; // Process order } ``` Sample Fail fast principle: ```csharp public void ProcessOrder(Order order) { if (order == null) throw new ArgumentNullException(nameof(order)); if (!order.IsValid) throw new InvalidOperationException("Invalid order"); // Process order } ``` 3. **Wrapping All Primitives and Strings**: - Avoid using primitive types directly in your code. - Wrap them in classes to provide meaningful context and behavior. ```csharp // Bad Example - Using primitive types directly public class User { public string Name { get; set; } public int Age { get; set; } } // Good Example - Wrapping primitives public class User { private string name; private Age age; public User(string name, Age age) { this.name = name; this.age = age; } } public class Age { private int value; public Age(int value) { if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), "Age cannot be negative"); this.value = value; } } ``` 4. **First Class Collections**: - Use collections to encapsulate data and behavior, rather than exposing raw data structures. First Class Collections: a class that contains an array as an attribute should not contain any other attributes
fill the variables
This prompt has 8 variables. Pro fills them into a ready-to-paste prompt for you — no manual find-and-replace.
{cs,ts,java}{foreach (var user in users) {
SendEmail(user);}{if (user.IsActive) {
mailer.Send(user.Email);}{if (order.IsValid) {
// Process order}{// Handle invalid order}{if (!order.IsValid) return;
// Process order}{public string Name { get; set;}{get; set;}
Unlock with Pro →when to use it
Community prompt sourced from the open-source GitHub repo ItamarZand88/awesome-agent-conventions (MIT). A "Object Calisthenics.instructions" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
roleplaycommunitygeneral
source
ItamarZand88/awesome-agent-conventions · MIT