Member-only story

All Basic Principles for Standardized Code: Code Like a Pro — with Examples 🚀

Lakhveer Singh Rajput
4 min readSep 23, 2024

--

In the world of software development, writing clean, standardized, and professional code isn’t just an ideal — it’s essential. Following coding principles not only improves readability but also makes debugging easier, ensures maintainability, and promotes team collaboration. In this blog, we’ll explore the key principles every developer should follow to write code like a pro! 💻

1. DRY (Don’t Repeat Yourself) 📝

Principle: Avoid code duplication. Every piece of knowledge should have a single, unambiguous representation within a system.

Example: Instead of repeating similar blocks of code, extract them into a function:

# Bad: Repeated logic
def calculate_discount(price)
discount = price * 0.1
price - discount
end

def apply_discount_to_cart(cart_items)
cart_items.each do |item|
discount = item.price * 0.1
item.price -= discount
end
end

# Good: DRY principle
def apply_discount(price)
discount = price * 0.1
price - discount
end

def apply_discount_to_cart(cart_items)
cart_items.each do |item|
item.price = apply_discount(item.price)
end
end

This makes the code easier to maintain, as changes to the discount logic only need to be updated in one place.

--

--

Lakhveer Singh Rajput
Lakhveer Singh Rajput

Written by Lakhveer Singh Rajput

Ruby on Rails enthusiast, book lover and DevOps explorer. Follow me for insights on coding, book recommendations, and bridging development with operations.🚀📚

No responses yet