
- 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).
Relevant links and references
Example: Generate an Employee class from comments
Start with a clear comment:main.py) that includes property-backed validation:
- Property setters centralize validation and keep the constructor simple.
- Explicit type checks help catch invalid runtime inputs early.
- Converting salary to
floatensures a consistent public API.
Example: Generate a method that processes stock market data
You can request more complex behavior using comments:Employee (keeps the example compact). It demonstrates common data-processing best practices: input validation, handling missing values, outlier clipping, and creating derived features.
- Use
min_periods=1to 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:test_main.py) that checks structure and key properties instead of brittle numeric expectations:
- 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.
- 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.