@mohitksahu_•

Programming Principles

Writing code that survives contact with real users, changing requirements, and other developers takes principles. These aren't abstract theory. They're hard-won lessons from decades of software engineering, distilled into mental shortcuts that help you make better decisions faster.

DRY: Don't Repeat Yourself

Every piece of knowledge should have a single, unambiguous representation in a system. When you duplicate logic, a fix in one place becomes a problem in every other copy you forgot to update.

DRY isn't just about avoiding copy-paste. It's about ensuring that when a rule changes, you change it in one place. If your pricing calculation lives in three different places, you don't have three features; you have three liabilities.

But DRY has a trap: premature abstraction. Sometimes two pieces of logic look similar but represent different concerns. Forcing them into a shared place creates coupling where there should be none. Duplication is cheaper than the wrong abstraction.

YAGNI: You Aren't Gonna Need It

Don't build features until you actually need them. That clever plugin system, the configurable workflow engine, the support for formats nobody has asked for: skip it. Everything you build has a maintenance cost. Things you don't build have zero problems.

YAGNI fights the instinct to "future-proof" your design. The future is unknowable. Building for imagined requirements wastes time now and creates complexity that slows you down later. Solve today's problem today. Tomorrow's problem might never arrive.

KISS: Keep It Simple, Stupid

The simplest solution that works is usually the best one. Complexity is the enemy of reliability. Every layer of abstraction, every pattern, every tool you add is a decision that future you (or your teammate) has to understand.

If a straightforward approach solves the problem, don't reach for something more elaborate. Complexity should be earned by real requirements, not by the desire to write "elegant" solutions.

Separation of Concerns

Each part of a system should handle one distinct aspect of the functionality. The data layer shouldn't know about presentation. The interface shouldn't contain business rules. The authentication module shouldn't format notifications.

This principle is the foundation of well-structured systems. When concerns are properly separated, changes stay contained. When they bleed together, changes ripple unpredictably across the entire system.

Fail Fast

If something is going to fail, make it fail immediately and loudly. Don't let bad data silently propagate through layers until it causes a confusing problem somewhere far from the source.

Validate inputs at the boundary. Reject invalid state early. A clear, immediate failure is infinitely more useful than a corrupted system discovered three days later.

Principle of Least Astonishment

Things should behave the way people expect them to. A function called getUser() should not modify data. A button labeled "Save" should not silently discard your work. A search bar should not trigger unrelated side effects.

Name things accurately. Follow conventions. When your system surprises people, it creates problems. Predictable behavior builds trust.

Premature Optimization

"Premature optimization is the root of all evil," as Donald Knuth famously observed. Don't optimize before you've measured where the bottleneck actually is. People consistently guess wrong about what's slow.

Build something clear and correct first. Measure it. Find the actual bottleneck. Then optimize that specific part. Most of your system handles modest workloads. The small fraction that runs on a hot path is where optimization matters.

The Boy Scout Rule

Leave things better than you found them. Every time you touch something, clean up a little. Rename something confusing. Simplify a convoluted section. Remove what's no longer needed. Add a missing explanation.

Small, continuous improvements compound over time. A system that gets slightly better with every change stays healthy. One that accumulates "I'll fix it later" debt eventually collapses under its own weight.

Coupling and Cohesion

Low coupling means parts of your system don't depend heavily on each other. Change one part, and others remain unaffected. High coupling means touching one thing triggers cascading changes everywhere.

High cohesion means everything inside a module is related and works toward the same purpose. A payment system that also manages user sessions has low cohesion. Keep related things together and unrelated things apart.

The goal is always: high cohesion within modules, low coupling between them.

Convention Over Configuration

Reduce decisions by providing sensible defaults. If every project follows the same structure and naming patterns, don't force people to configure those paths. Let them override when needed, but make the common case effortless.

Frameworks like Rails and Next.js use this heavily through file-based routing and naming conventions that map to behavior. Less configuration means fewer mistakes and faster onboarding.

Defensive Programming

Assume your inputs are wrong, your dependencies are unreliable, and your users are creative. Validate everything. Handle edge cases. Don't trust data from external systems.

This doesn't mean guarding every single operation. It means thinking about what happens when the network is slow, the file doesn't exist, the input is empty, or a value overflows. Handle the failure case, not just the happy path.

Summary

These principles aren't rules to follow blindly. They're tools for thinking. Sometimes DRY conflicts with KISS. Sometimes YAGNI means skipping a pattern that good design would recommend. The skill isn't memorizing principles; it's knowing when each one applies and when to break it.

Great engineers don't write clever solutions. They build things that are easy to understand, easy to change, and hard to break. These principles are how.

Sprite