Loading...
Loading...
Comprehensive coding standards and best practices for maintainable, consistent software development across multiple languages and paradigms
npx skill4agent add williamzujkowski/standards coding-standards# ✅ Good: Clear naming, single responsibility
def calculate_user_discount(user: User, order: Order) -> Decimal:
"""Calculate discount based on user tier and order total."""
if user.tier == "premium":
return order.total * Decimal("0.15")
return order.total * Decimal("0.05")
# ❌ Bad: Unclear naming, mixed concerns
def calc(u, o):
d = 0.15 if u.t == "p" else 0.05
save_to_db(u, o, d) # Side effect!
return o.t * d// TypeScript/JavaScript
class UserService {} // PascalCase for classes
const getUserById = () => {} // camelCase for functions
const API_ENDPOINT = "..." // UPPER_SNAKE_CASE for constants# Python
class UserService: # PascalCase for classes
def get_user_by_id(): # snake_case for functions
API_ENDPOINT = "..." # UPPER_SNAKE_CASE for constantssrc/
├── models/ # Data models and types
├── services/ # Business logic (max 500 lines/file)
├── controllers/ # Request handlers
├── utils/ # Shared utilities
└── config/ # Configuration/**
* Calculates the final price after applying discounts and tax.
*
* @param basePrice - The original price before any adjustments
* @param discountRate - Discount as a decimal (0.1 = 10%)
* @param taxRate - Tax rate as a decimal (0.08 = 8%)
* @returns The final price rounded to 2 decimal places
* @throws {ValidationError} If basePrice is negative
*
* @example
* const finalPrice = calculateFinalPrice(100, 0.1, 0.08);
* // Returns 97.20 (100 - 10% discount + 8% tax)
*/
function calculateFinalPrice(
basePrice: number,
discountRate: number,
taxRate: number
): number {
if (basePrice < 0) {
throw new ValidationError("Base price cannot be negative");
}
const discounted = basePrice * (1 - discountRate);
return Math.round(discounted * (1 + taxRate) * 100) / 100;
}// Single Responsibility Principle
class UserRepository {
async findById(id: string): Promise<User> {}
async save(user: User): Promise<void> {}
}
class UserValidator {
validate(user: User): ValidationResult {}
}
// Dependency Injection
class UserService {
constructor(
private readonly repository: UserRepository,
private readonly validator: UserValidator
) {}
async createUser(userData: UserData): Promise<User> {
const validation = this.validator.validate(userData);
if (!validation.isValid) {
throw new ValidationError(validation.errors);
}
return this.repository.save(new User(userData));
}
}// Custom error hierarchy
class ApplicationError extends Error {
constructor(
message: string,
public code: string,
public statusCode: number = 500
) {
super(message);
this.name = this.constructor.name;
}
}
class ValidationError extends ApplicationError {
constructor(message: string, public fields: string[]) {
super(message, "VALIDATION_ERROR", 400);
}
}
// Usage with proper error handling
async function processPayment(payment: Payment): Promise<Result> {
try {
validatePaymentData(payment);
const result = await paymentGateway.charge(payment);
await auditLog.record("payment.success", { paymentId: result.id });
return result;
} catch (error) {
if (error instanceof ValidationError) {
logger.warn("Invalid payment data", { fields: error.fields });
throw error;
}
logger.error("Payment processing failed", { error, payment });
throw new ApplicationError(
"Payment processing failed",
"PAYMENT_ERROR",
502
);
}
}// ❌ Bad: Repeated logic
function calculateEmployeeSalary(employee: Employee): number {
if (employee.type === "full-time") {
return employee.baseSalary * 1.2 + 5000;
}
return employee.baseSalary * 1.2;
}
function calculateContractorPay(contractor: Contractor): number {
return contractor.baseSalary * 1.2;
}
// ✅ Good: Extracted common logic
function applyStandardBonus(baseSalary: number): number {
return baseSalary * 1.2;
}
function calculateEmployeeSalary(employee: Employee): number {
const withBonus = applyStandardBonus(employee.baseSalary);
return employee.type === "full-time" ? withBonus + 5000 : withBonus;
}// package.json - Automation setup
{
"scripts": {
"lint": "eslint . --ext .ts,.tsx",
"format": "prettier --write \"**/*.{ts,tsx,json,md}\"",
"type-check": "tsc --noEmit",
"validate": "npm run lint && npm run type-check && npm run test"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{json,md}": ["prettier --write"]
}
}function memoize<T extends (...args: any[]) => any>(fn: T): T {
const cache = new Map<string, ReturnType<T>>();
return ((...args: Parameters<T>) => {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn(...args);
cache.set(key, result);
return result;
}) as T;
}
// Usage
const expensiveCalculation = memoize((n: number) => {
// Complex computation
return Array.from({ length: n }, (_, i) => i * i).reduce((a, b) => a + b);
});// Parallel execution with error handling
async function processItemsConcurrently<T, R>(
items: T[],
processor: (item: T) => Promise<R>,
concurrency: number = 5
): Promise<Array<R | Error>> {
const results: Array<R | Error> = [];
const executing: Promise<void>[] = [];
for (const item of items) {
const promise = processor(item)
.then((result) => {
results.push(result);
})
.catch((error) => {
results.push(error);
});
executing.push(promise);
if (executing.length >= concurrency) {
await Promise.race(executing);
executing.splice(
executing.findIndex((p) => p === promise),
1
);
}
}
await Promise.all(executing);
return results;
}// RESTful API design
interface ApiResponse<T> {
data: T;
meta: {
timestamp: string;
requestId: string;
};
errors?: ApiError[];
}
// Versioning strategy
app.use("/api/v1", v1Routes);
app.use("/api/v2", v2Routes);
// Pagination standard
interface PaginatedResponse<T> extends ApiResponse<T[]> {
pagination: {
page: number;
pageSize: number;
totalItems: number;
totalPages: number;
};
}// Before
function generateReport(data: Data[]): Report {
// 100+ lines of complex logic
const filtered = data.filter(/* complex condition */);
const transformed = filtered.map(/* complex transformation */);
const aggregated = transformed.reduce(/* complex aggregation */);
// More complex logic...
}
// After
function generateReport(data: Data[]): Report {
const filtered = filterRelevantData(data);
const transformed = transformDataForReport(filtered);
const aggregated = aggregateMetrics(transformed);
return formatReport(aggregated);
}## Code Review Checklist
### Functionality
- [ ] Code meets requirements
- [ ] Edge cases handled
- [ ] Error handling comprehensive
### Code Quality
- [ ] Follows style guide
- [ ] Names are clear and descriptive
- [ ] Functions are focused (<50 lines)
- [ ] No code duplication
### Testing
- [ ] Unit tests included
- [ ] Tests cover edge cases
- [ ] Tests are maintainable
### Documentation
- [ ] Public APIs documented
- [ ] Complex logic explained
- [ ] README updated if needed
### Security
- [ ] No hardcoded secrets
- [ ] Input validation present
- [ ] SQL injection prevented#!/usr/bin/env python3
"""Check cyclomatic complexity of Python files."""
import sys
from radon.complexity import cc_visit
def check_complexity(filename: str, max_complexity: int = 10) -> bool:
"""Check if file exceeds complexity threshold."""
with open(filename) as f:
code = f.read()
results = cc_visit(code)
violations = [r for r in results if r.complexity > max_complexity]
if violations:
print(f"❌ {filename}: Complexity violations found")
for v in violations:
print(f" - {v.name}: complexity {v.complexity} (max {max_complexity})")
return False
print(f"✅ {filename}: All functions within complexity limit")
return True
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: check_complexity.py <file>")
sys.exit(1)
passed = check_complexity(sys.argv[1])
sys.exit(0 if passed else 1)// TODO: Add basic example for coding-standards
// This example demonstrates core functionality// TODO: Add advanced example for coding-standards
// This example shows production-ready patterns// TODO: Add integration example showing how coding-standards
// works with other systems and servicesexamples/coding-standards/./resources/./templates/./scripts/