My Most Basic Coding Styleguide
Over the years, I've distilled my coding practices into a set of simple rules. These aren't revolutionary – they're basic principles that make code readable and maintainable.
Naming Things
The hardest problem in computer science, they say. Here's my approach:
- Variables: Describe what they contain, not how they're used
- Functions: Start with a verb describing what they do
- Booleans: Prefix with
is,has,can, orshould
// ❌ Bad const data = fetchUsers(); const flag = user.age >= 18; // ✅ Good const activeUsers = fetchUsers(); const isAdult = user.age >= 18;
Keep Functions Small
If a function does more than one thing, split it. A good rule of thumb: if you need to add "and" when describing what a function does, it's doing too much.
// ❌ Does too much function processUserAndSendEmail(user) { // validate user // save to database // send welcome email } // ✅ Single responsibility function validateUser(user) { /* ... */ } function saveUser(user) { /* ... */ } function sendWelcomeEmail(user) { /* ... */ }
Early Returns
Avoid deep nesting by returning early. It makes code easier to follow.
// ❌ Deep nesting function getDiscount(user) { if (user) { if (user.isPremium) { if (user.yearsActive > 2) { return 0.2; } } } return 0; } // ✅ Early returns function getDiscount(user) { if (!user) return 0; if (!user.isPremium) return 0; if (user.yearsActive <= 2) return 0; return 0.2; }
Comments
Write code that explains itself. Use comments for the "why", not the "what".
// ❌ Describes what (obvious from code) // Loop through users users.forEach(user => process(user)); // ✅ Explains why // Process oldest users first to prioritize long-term customers users.sort((a, b) => a.createdAt - b.createdAt) .forEach(user => process(user));
Conclusion
These rules are simple but effective. The goal isn't perfection – it's consistency and readability. When your future self (or a colleague) reads your code, they should understand it without needing you to explain it.