← Back to Blog

Contract Mechanism Design in Cangjie and Its Application in LLM Code Generation

How can contracts improve LLM-generated code quality? Dr. Bohua Zhan from Huawei 2012 Programming Language Lab presents groundbreaking research on integrating Design by Contract principles with large language model code generation using the Cangjie programming language.

πŸ”— Video Link: https://youtu.be/jvPmcEIelSU

Key Topics Covered

  • Contract programming fundamentals and its history from Eiffel to modern languages
  • Cangjie contract language design using macros for runtime verification
  • Preconditions, postconditions, invariants, and abstract state descriptions
  • Contract-based automated testing workflow for LLM code generation

Research Highlights

  • Implemented contract language prototype using Cangjie’s macro system
  • Developed RAG-based approach for Cangjie code generation with LLMs
  • Demonstrated that contracts are more reliable than code (easier for LLMs to generate correctly)
  • Contract-based testing outperforms traditional I/O-based test case methods

Experimental Results

  • DeepSeek v3.1: +4.81% accuracy improvement with contract testing
  • Doubao 1.5-pro 32k: +6.18% accuracy improvement with contract testing
  • Class-level generation: up to 10.9% improvement with contract-guided repair

Learn More

πŸ”— Cangjie Language

CCF ChinaSoft 2025