If You Know These 7 Principles, You Are a Decent Coder
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -Martin Golding Learning some programming principles and using them in your code makes you a better developer. Fact. It improves the quality of the code and makes it easier for everyone to add functionality or make changes later. Many interviewees will also ask you about it. So let’s discuss some basic principles of programming and the benefits of using it. And I promise: I won’t make it as boring as a high school class. 1. KISS (Keep It Simple, Stupid) Simple code is better. Complicated code breaks more and is harder to read. Bad example: function calculatePrice(price, tax) { let total = price + (price * tax / 100); return total; } Better: const calculatePrice = (price, tax) => price * (1 + tax / 100); Less code. Same result. Easy to read! --- 2. DRY (Don’t Repeat Yourself) Repeating code is bad. If you need to change something, you have to do it in multiple places. That’s extra work! Bad example: const getUserFullName = (user) => user.firstName + ' ' + user.lastName; console.log(getUserFullName(user)); console.log(getUserFullName(user)); // Repeating the same call Better: const fullName = getUserFullName(user); console.log(fullName); console.log(fullName); Less repetition = less trouble. 3. YAGNI (You Aren’t Gonna Need It) Don’t write code you don’t need yet. It’s a waste of time and might never be used. Bad example: function spaceshipLaunch(speed, fuel, hasHyperdrive) { if (hasHyperdrive) { console.log('Engaging hyperdrive...'); } console.log('Launching at', speed, 'with', fuel, 'fuel'); } What if we never use hyperdrive? Keep it simple: function spaceshipLaunch(speed, fuel) { console.log('Launching at', speed, 'with', fuel, 'fuel'); } 4. SOLID This is a set of five rules for good object-oriented programming. Single Responsibility Principle: One function = One job. Open-Closed Principle: Code should be easy to extend, but not modified. Liskov Substitution Principle: A subclass should work like its parent class. Interface Segregation Principle: Don’t force a class to use methods it doesn’t need. Dependency Inversion Principle: High-level modules should not depend on low-level modules. Example: class Animal { speak() { console.log("Animal speaks"); } } class Dog extends Animal { speak() { console.log("Bark!"); } } const dog = new Dog(); dog.speak(); // Bark! 5. Separation of Concerns (SoC) Each part of your code should do one thing. No messy mix of logic! Bad example: function fetchDataAndRenderUI() { fetch('https://api.example.com') .then(response => response.json()) .then(data => { document.getElementById('content').innerText = data.message; }); } Better: function fetchData(url) { return fetch(url).then(response => response.json()); } function renderUI(data) { document.getElementById('content').innerText = data.message; } fetchData('https://api.example.com').then(renderUI); 6. Avoid Premature Optimization Don’t try to make your code super fast before it works correctly. First, make it work. Then, make it fast if needed. Bad example: const numbers = [1, 2, 3, 4, 5]; let sum = 0; for (let i = 0; i < numbers.length; i++) { sum += numbers[i]; } Better: const sum = numbers.reduce((acc, num) => acc + num, 0); Shorter and easier to read! 7. Law of Demeter A function should only talk to its closest friends. Don’t reach too deep into objects. Bad example: console.log(user.profile.details.address.city); Better: console.log(user.getCity()); Make a function inside user that returns city. This makes your code safer and easier to change. Conclusion These 7 principles will help you write better code. Keep it simple. Don’t repeat yourself. Don’t build things you don’t need. Follow SOLID rules. Keep concerns separate. Optimize later. Keep objects from talking too much. Follow these, and you’ll be a decent coder!

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
-Martin Golding
Learning some programming principles and using them in your code makes you a better developer.
Fact.
It improves the quality of the code and makes it easier for everyone to add functionality or make changes later. Many interviewees will also ask you about it.
So let’s discuss some basic principles of programming and the benefits of using it.
And I promise: I won’t make it as boring as a high school class.
1. KISS (Keep It Simple, Stupid)
Simple code is better. Complicated code breaks more and is harder to read.
Bad example:
function calculatePrice(price, tax) {
let total = price + (price * tax / 100);
return total;
}
Better:
const calculatePrice = (price, tax) => price * (1 + tax / 100);
Less code. Same result. Easy to read!
---
2. DRY (Don’t Repeat Yourself)
Repeating code is bad. If you need to change something, you have to do it in multiple places.
That’s extra work!
Bad example:
const getUserFullName = (user) => user.firstName + ' ' + user.lastName;
console.log(getUserFullName(user));
console.log(getUserFullName(user)); // Repeating the same call
Better:
const fullName = getUserFullName(user);
console.log(fullName);
console.log(fullName);
Less repetition = less trouble.
3. YAGNI (You Aren’t Gonna Need It)
Don’t write code you don’t need yet. It’s a waste of time and might never be used.
Bad example:
function spaceshipLaunch(speed, fuel, hasHyperdrive) {
if (hasHyperdrive) {
console.log('Engaging hyperdrive...');
}
console.log('Launching at', speed, 'with', fuel, 'fuel');
}
What if we never use hyperdrive? Keep it simple:
function spaceshipLaunch(speed, fuel) {
console.log('Launching at', speed, 'with', fuel, 'fuel');
}
4. SOLID
This is a set of five rules for good object-oriented programming.
- Single Responsibility Principle: One function = One job.
- Open-Closed Principle: Code should be easy to extend, but not modified.
- Liskov Substitution Principle: A subclass should work like its parent class.
- Interface Segregation Principle: Don’t force a class to use methods it doesn’t need.
- Dependency Inversion Principle: High-level modules should not depend on low-level modules.
Example:
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Bark!");
}
}
const dog = new Dog();
dog.speak(); // Bark!
5. Separation of Concerns (SoC)
Each part of your code should do one thing. No messy mix of logic!
Bad example:
function fetchDataAndRenderUI() {
fetch('https://api.example.com')
.then(response => response.json())
.then(data => {
document.getElementById('content').innerText = data.message;
});
}
Better:
function fetchData(url) {
return fetch(url).then(response => response.json());
}
function renderUI(data) {
document.getElementById('content').innerText = data.message;
}
fetchData('https://api.example.com').then(renderUI);
6. Avoid Premature Optimization
Don’t try to make your code super fast before it works correctly. First, make it work. Then, make it fast if needed.
Bad example:
const numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
Better:
const sum = numbers.reduce((acc, num) => acc + num, 0);
Shorter and easier to read!
7. Law of Demeter
A function should only talk to its closest friends. Don’t reach too deep into objects.
Bad example:
console.log(user.profile.details.address.city);
Better:
console.log(user.getCity());
Make a function inside user that returns city. This makes your code safer and easier to change.
Conclusion
These 7 principles will help you write better code.
- Keep it simple.
- Don’t repeat yourself.
- Don’t build things you don’t need.
- Follow SOLID rules.
- Keep concerns separate.
- Optimize later.
- Keep objects from talking too much.
Follow these, and you’ll be a decent coder!