
On Writing Code
Writing code is a craft. Like any craft, it requires practice, patience, and a commitment to continuous improvement. After years of writing code professionally, I've learned that great code isn't just about making things work—it's about making things work well.
Code is Communication
Code is read far more often than it's written. Write for humans first, computers second. Use clear names, write comments when necessary, and structure your code in a way that tells a story.
Compare these two approaches:
// Hard to understand
function p(u) {
const d = new Date(u.c)
return d.getTime() < Date.now() - 86400000
}
// Clear and communicative
function isPostOlderThanOneDay(post) {
const postDate = new Date(post.createdAt)
const oneDayInMs = 24 * 60 * 60 * 1000
return postDate.getTime() < Date.now() - oneDayInMs
}
The second version takes a few more characters, but it's infinitely more maintainable.
Consistency Matters
Consistent code is easier to understand and maintain. Follow conventions, use linters, and establish patterns that your team can follow.
In my projects, I always set up these tools:
- ESLint — Catch errors and enforce code style
- Prettier — Automatic code formatting
- TypeScript — Type safety and better tooling
- Husky — Git hooks to run checks before commits
Write Self-Documenting Code
The best documentation is code that explains itself. Use descriptive names, break complex logic into well-named functions, and structure your code logically.
// Instead of comments explaining what code does
// Get all active users who haven't logged in for 30 days
const users = await db.user.findMany({
where: {
active: true,
lastLogin: { lt: new Date(Date.now() - 30 * 86400000) }
}
})
// Write code that explains itself
const THIRTY_DAYS_IN_MS = 30 * 24 * 60 * 60 * 1000
const thirtyDaysAgo = new Date(Date.now() - THIRTY_DAYS_IN_MS)
const inactiveUsers = await db.user.findMany({
where: {
active: true,
lastLogin: { lt: thirtyDaysAgo }
}
})
Test Your Assumptions
Write tests. Not because you have to, but because you care about your craft. Tests give you confidence to refactor and improve your code.
I follow a simple testing philosophy:
- Test behavior, not implementation
- Write tests that would fail if the feature breaks
- Keep tests simple and readable
- Don't aim for 100% coverage—aim for confidence
Refactor Ruthlessly
Good code is rewritten code. Don't be afraid to refactor when you see a better way. The best time to refactor is when you're already in the code making changes.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." — Martin Fowler
Never Stop Learning
The best developers I know are perpetual students. They're curious, humble, and always looking for ways to improve their craft.
Ways to keep learning:
- Read other people's code (especially open source projects)
- Pair program with developers better than you
- Write about what you learn
- Build side projects to experiment with new technologies
- Attend conferences and meetups
Remember: writing great code is a journey, not a destination. Keep practicing, keep learning, and keep improving.