Skip to main content
In this lesson we explore “comment-driven development” — the practice of writing descriptive comments to guide code generation tools to scaffold classes, functions, and tests. This workflow accelerates the creation of boilerplate code so you can iterate faster: generate, review, and refine.
A presentation slide with the title "Comment-driven development" on the left and the word "Demo" displayed on a dark blue curved shape on the right. A small "© Copyright KodeKloud" appears in the lower-left corner.
Typical flow
  • Write a concise, descriptive comment describing the class, function, or test you need.
  • Let a code-generation tool produce an initial implementation.
  • Inspect, validate, and refine the generated code (add type checks, edge-case handling, and tests).
Quick summary table Relevant links and references

Example: Generate an Employee class from comments

Start with a clear comment:
A consolidated, corrected implementation (saved as main.py) that includes property-backed validation:
Usage example (executable snippet):
Expected output:
Why this pattern?
  • Property setters centralize validation and keep the constructor simple.
  • Explicit type checks help catch invalid runtime inputs early.
  • Converting salary to float ensures a consistent public API.

Example: Generate a method that processes stock market data

You can request more complex behavior using comments:
Below is a robust implementation included as a method on Employee (keeps the example compact). It demonstrates common data-processing best practices: input validation, handling missing values, outlier clipping, and creating derived features.
Practical tips
  • Use min_periods=1 to avoid long initial NaNs during testing or short series; adjust as needed for production.
  • Avoid chained assignment warnings by using .loc.
  • Validate inputs and document assumptions in the docstring.

Example: Generate validation helpers and tests

Use comments to scaffold small helper functions and tests. An example helper to validate trading parameters:
A corresponding, focused test file (test_main.py) that checks structure and key properties instead of brittle numeric expectations:
Testing guidance
  • Prefer assertions about structure, invariants, and data types rather than exact numeric equality for rolling-window statistics.
  • Use small, deterministic datasets in unit tests to keep them fast and reliable.
  • Reserve longer integration tests for end-to-end validation.
Use generated code as a starting point. Always review and refine: validate assumptions, add edge-case handling, and write tests that assert meaningful properties rather than brittle exact values whenever possible.

Takeaways

  • Comment-driven development speeds up scaffolding for classes, methods, validation helpers, and tests.
  • Generated code is a draft — review it for types, edge cases, and performance (for example, rolling window sizes and outlier-handling strategies).
  • Use generated tests as scaffolding; expand them to cover realistic scenarios and invariants.
  • This approach is ideal for repetitive boilerplate; critical business logic still needs design, code review, and careful testing.
Next lesson preview
  • We’ll build a small project (a fake data generator) to demonstrate these techniques across a full repository: project layout, CI-friendly tests, and iterative feature development.

Watch Video