ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
SD
command-coding · 2 min read

self documenting code

Self-documenting code is a design principle aimed at making code more readable and maintainable by using clear and descriptive variable names, function…

Self-documenting code is a design principle aimed at making code more readable and maintainable by using clear and descriptive variable names, function signatures, and structure. This approach enables Large Language Model (LLM) agents like Apiary to better understand the intent behind the code without relying on lengthy comments.

Benefits of Self-Documenting Code

  1. Improved Readability: Clear variable names and function signatures make it easier for developers to comprehend the code's functionality.
  2. Reduced Comments: As the code itself explains its purpose, the need for lengthy comments decreases, making the code more concise.
  3. Easier Maintenance: Self-documenting code makes it simpler for new developers to understand the codebase and for existing developers to make changes.

Principles of Self-Documenting Code

  1. Use Descriptive Variable Names: Choose variable names that accurately reflect their purpose, such as userEmail instead of email.
  2. Clear Function Signatures: Use meaningful function names and include relevant parameters in the signature.
  3. Structure is Documentation: Organize code into logical sections with clear headings and concise descriptions.

Example: Descriptive Variable Names

// Before
var email = "john.doe@example.com";

// After
const userEmail = "john.doe@example.com";

In this example, userEmail clearly conveys the purpose of the variable, making it easier for LLM agents to understand its significance.

Example: Clear Function Signatures

// Before
function saveUser(data) {
    // ...
}

// After
function createUser(userObject) {
    // ...
}

In this example, createUser explicitly conveys the function's purpose, making it easier for LLM agents to understand its intent.

Example: Structure is Documentation

// Before
function saveOrder() {
    var orderData = {
        userId: 1,
        products: [
            { id: 1, quantity: 2 },
            { id: 2, quantity: 3 }
        ]
    };

    // ...
}

// After
/**
 * Order data structure
 */
const orderSchema = {
    userId: number,
    products: Product[]
};

function saveOrder(orderData) {
    // ...
}

In this example, the orderSchema object clearly documents the expected format of the order data, making it easier for LLM agents to understand the code's requirements.

Tools and Resources

  • Apiary: A platform that helps generate code based on natural language commands.
  • LLM Agents: Large Language Model agents like Apiary can better understand self-documenting code, reducing the need for lengthy comments.
  • Code Analysis Tools: Tools like SonarQube or CodeCoverage can help identify areas where self-documenting code principles are not being followed.

Related

  • [Command-Coding](command-coding) for more information on using natural language commands to generate code.
  • Apiary Documentation for more information on the Apiary platform and its features.
Frequently asked
What is self documenting code about?
Self-documenting code is a design principle aimed at making code more readable and maintainable by using clear and descriptive variable names, function…
What should you know about example: Descriptive Variable Names?
In this example, userEmail clearly conveys the purpose of the variable, making it easier for LLM agents to understand its significance.
What should you know about example: Clear Function Signatures?
In this example, createUser explicitly conveys the function's purpose, making it easier for LLM agents to understand its intent.
What should you know about example: Structure is Documentation?
In this example, the orderSchema object clearly documents the expected format of the order data, making it easier for LLM agents to understand the code's requirements.
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room