TRUST 4 principles - Test First, Readable, Unified, Secured
Foundation of Code Quality
Principles: Test First, Readable, Unified, Secured
Standards: ≥85% Coverage, Pylint ≥8.0, OWASP Top 10
TRUST 4 defines non-negotiable quality requirements for all code.
# 1. RED: Write test (fails)
def test_password_hash_unique():
hash1 = hash_password("TestPass123")
hash2 = hash_password("TestPass123")
assert hash1 != hash2
# 2. GREEN: Implement
def hash_password(plaintext: str) -> str:
import bcrypt
salt = bcrypt.gensalt(rounds=12)
return bcrypt.hashpw(plaintext.encode(), salt).decode()
# 3. REFACTOR: Add security docs
validate_password not vp)# Bad
def f(x, y):
return sum([i*x for i in y if i]) / len(y) if y else 0
# Good
def calculate_weighted_average(weight: float, values: List[float]) -> float:
"""Calculate weighted average, excluding None values."""
valid = [v for v in values if v is not None]
if not valid:
return 0.0
return sum(v * weight for v in valid) / len(valid)
user_id everywhere, not uid)# Unified error handling
try:
result = process_payment(order)
except PaymentError as e:
logger.error(f"Payment failed: {e}", extra={"order_id": order.id})
raise ApplicationError("Payment processing failed") from e
def hash_password(plaintext: str) -> str:
"""
OWASP A02:2021 compliant password hashing.
Uses bcrypt with 12 rounds (2025 standard).
"""
import bcrypt
if not plaintext:
raise ValueError("Password required")
salt = bcrypt.gensalt(rounds=12)
return bcrypt.hashpw(plaintext.encode(), salt).decode()
# Automated quality gate
pytest --cov=src --cov-fail-under=85 # T: Test
pylint src/ --fail-under=8.0 # R: Readable
bandit -r src/ -ll # S: Secured
moai-core-rules: Core development guidelinesmoai-security-devsecops: Security testingmoai-project-documentation: Documentation standardsLast Updated: 2025-11-20