Why Code Generation Quality Varies: An Engineering Perspective

    This article explains why AI code generation that works in a demo often requires heavy rework in real repositories, and what engineering teams can do about it. It breaks the differences down into three dimensions—code taste aligned with existing conventions, restraint that follows the minimal-change principle, and awareness of migration history—then outlines four practices for keeping output maintainable: context first, explicit constraints, test-driven acceptance, and change-surface-focused review. It also notes how standard engineering infrastructure supports adoption at the team or enterprise level.

    1. The Symptom: Why “It Runs” Is Not “It’s Good”

    Many teams notice a gap after adopting AI coding tools: the demo looks impressive, but once the tool touches a real business codebase, rework becomes frequent. Developer feedback usually falls into three categories: the generated code does not match the existing engineering style; fixing one defect also modifies a large number of unrelated files; and when facing legacy logic, the tool covers the problem with new conditional branches instead of addressing the root cause.

    It is worth stressing that these differences do not necessarily reflect the quality of a specific product. The same tool can produce very different output quality depending on context, prompting style, and repository conventions. A more useful question is therefore: how do you define “good code generation” in engineering terms, and how do you use context and process constraints to pull the output back onto a maintainable track?

    2. Three Key Dimensions

    2.1 Code Taste: Aligning with Existing Conventions

    “Taste” is measurable in an engineering context. It typically shows up in naming habits, directory layering, error handling, logging and comment density, and preferences for dependencies. Output that fits team habits reads like code that already exists in the repository; output that does not requires extensive manual polishing.

    A practical way to constrain this is to maintain a short conventions document in the repository and have the tool read it before generating:

    1. Project conventions (excerpt)
    2. - Layering: the interface layer only validates parameters and converts protocols; business logic lives in the domain layer
    3. - Error handling: always use the project's wrapped business exceptions; never throw generic exceptions directly
    4. - Logging: log structured entries at entry points and key branches; never print inside loop bodies
    5. - Dependencies: prefer existing utility libraries in the repository; do not add third-party packages with overlapping functionality

    2.2 Change Restraint: The Minimal-Change Principle

    A typical trait of “patch-style” output is stacking new logic on top of old logic, forming layer after layer of conditionals. As iterations accumulate, cyclomatic complexity rises, branches mask one another, and eventually no one dares to touch the code. Restrained output instead tries to locate the real cause first, then replaces the implementation within the smallest possible scope.

    Comparison:

    1. # Patch style: keep appending branches
    2. def calc_fee(order):
    3. if order.is_vip and not order.is_promo:
    4. return base * 0.9
    5. if order.is_vip and order.is_promo:
    6. return base * 0.85
    7. if (not order.is_vip) and order.is_promo and order.region == "A":
    8. return base * 0.8
    9. return base
    1. # Refactored style: converge discount policies into composable rules
    2. def calc_fee(order, rules):
    3. price = order.base_price
    4. for rule in rules:
    5. if rule.matches(order):
    6. price = rule.apply(price)
    7. return max(price, order.floor_price)

    The second version appears to touch the structure, but adding new rules later no longer requires modifying the main flow, which lowers long-term maintenance cost. In engineering terms, “minimal change” can be stated as an explicit constraint: modify the location of the root cause first, no unrelated reformatting, and no opportunistic dependency upgrades.

    2.3 Understanding Evolution History: Compatibility and Migration Awareness

    Mature codebases often contain multiple generations of implementations at once: old interfaces are not yet retired, new interfaces are in gradual rollout, and data sits in a dual-write window. Good generated output recognizes this transitional state, chooses compatible implementations, and explains the migration path. Poorer output satisfies only the current use case and ignores the knock-on effects on callers and the data side.

    It helps to add explicit time-dimension information to the prompt, such as an interface’s deprecation plan, data backfill status, and rollout percentage, and to require the output to include an impact list: which callers are involved, whether dual writes are needed, and how to roll back.

    3. Four Practices for Pulling Output Back on Track

    1. Context first: provide relevant files, type definitions, and existing tests, not just a one-line requirement. Context quality usually affects results more than subtle differences between tools.
    2. Make constraints explicit: write layering, exception, logging, and dependency rules into a readable conventions file, and require the tool to restate the key constraints before generating.
    3. Test-driven acceptance: add or designate test cases first, and make passing tests the delivery gate for generated output, reducing “looks correct” false positives.
    4. Focus review on the change surface: look first at the number of changed files and the impact on the call chain, then at the implementation itself; if the change surface is out of control, send it back for a redo.

    4. Additional Notes for Enterprise Adoption

    In team or enterprise settings, general engineering infrastructure can reduce uncertainty: use code repositories to host conventions and templates, use continuous integration to run static checks and tests, use logging services and monitoring alerts to observe behavioral deviations after release, and use container platforms to keep build and runtime environments consistent. Baidu AI Cloud provides standard-category support for these general capabilities; specific choices should still be evaluated against the team’s existing technology stack.

    5. Conclusion

    The difference in code generation quality is not fundamentally about “who is smarter,” but about “who understands constraints better.” Write taste into conventions, write restraint into process, and write evolution history into context, and most tools can produce maintainable results. For developers, the real capability to build is expressing requirements, constraints, and acceptance criteria clearly, and letting review and automated tests hold the line.