An open API service indexing awesome lists of open source software.

https://github.com/aliezzahn/clean-code-principles

Writing clean code is essential for creating maintainable, readable, and professional software.
https://github.com/aliezzahn/clean-code-principles

Last synced: about 2 months ago
JSON representation

Writing clean code is essential for creating maintainable, readable, and professional software.

Awesome Lists containing this project

README

        

# 5 Clean Code Principles Every Software Engineer Should Know

Writing clean code is essential for creating maintainable, readable, and professional software. Whether you're preparing for a coding interview or working on a large-scale project, adhering to clean code principles can make a significant difference. In this blog post, we'll explore **five clean code principles** with practical examples in **C**, **C++**, **Rust**, **Java**, **PHP**, **JavaScript**, and **Python**.

---

## 1. Reduce Unnecessary Nesting

Deeply nested code is hard to read and maintain. Flatten your code by using early exits (`continue`, `return`, or `break`) to simplify logic.

### **Example**

#### C

```c
// Bad: Nested if statements
if (condition1) {
if (condition2) {
if (condition3) {
do_something();
}
}
}

// Good: Flattened with early exit
if (!condition1) return;
if (!condition2) return;
if (!condition3) return;
do_something();
```

#### C++

```cpp
// Bad: Nested if statements
if (condition1) {
if (condition2) {
if (condition3) {
doSomething();
}
}
}

// Good: Flattened with early exit
if (!condition1) return;
if (!condition2) return;
if (!condition3) return;
doSomething();
```

#### Rust

```rust
// Bad: Nested if statements
if condition1 {
if condition2 {
if condition3 {
do_something();
}
}
}

// Good: Flattened with early exit
if !condition1 { return; }
if !condition2 { return; }
if !condition3 { return; }
do_something();
```

#### Java

```java
// Bad: Nested if statements
if (condition1) {
if (condition2) {
if (condition3) {
doSomething();
}
}
}

// Good: Flattened with early exit
if (!condition1) return;
if (!condition2) return;
if (!condition3) return;
doSomething();
```

#### PHP

```php
// Bad: Nested if statements
if ($condition1) {
if ($condition2) {
if ($condition3) {
doSomething();
}
}
}

// Good: Flattened with early exit
if (!$condition1) return;
if (!$condition2) return;
if (!$condition3) return;
doSomething();
```

#### JavaScript

```javascript
// Bad: Nested if statements
if (condition1) {
if (condition2) {
if (condition3) {
doSomething();
}
}
}

// Good: Flattened with early exit
if (!condition1) return;
if (!condition2) return;
if (!condition3) return;
doSomething();
```

#### Python

```python
# Bad: Nested if statements
if condition1:
if condition2:
if condition3:
do_something()

# Good: Flattened with early exit
if not condition1:
return
if not condition2:
return
if not condition3:
return
do_something()
```

---

## 2. Use Descriptive Variable Names

Clear and descriptive variable names make your code self-explanatory. Avoid ambiguous names like `i`, `temp`, or `x`.

### **Example**

#### C

```c
// Bad: Ambiguous variable names
int x = 10;
int y = 5;
int z = x + y;

// Good: Descriptive variable names
int total_price = 10;
int discount = 5;
int final_price = total_price - discount;
```

#### C++

```cpp
// Bad: Ambiguous variable names
int x = 10;
int y = 5;
int z = x + y;

// Good: Descriptive variable names
int totalPrice = 10;
int discount = 5;
int finalPrice = totalPrice - discount;
```

#### Rust

```rust
// Bad: Ambiguous variable names
let x = 10;
let y = 5;
let z = x + y;

// Good: Descriptive variable names
let total_price = 10;
let discount = 5;
let final_price = total_price - discount;
```

#### Java

```java
// Bad: Ambiguous variable names
int x = 10;
int y = 5;
int z = x + y;

// Good: Descriptive variable names
int totalPrice = 10;
int discount = 5;
int finalPrice = totalPrice - discount;
```

#### PHP

```php
// Bad: Ambiguous variable names
$x = 10;
$y = 5;
$z = $x + $y;

// Good: Descriptive variable names
$total_price = 10;
$discount = 5;
$final_price = $total_price - $discount;
```

#### JavaScript

```javascript
// Bad: Ambiguous variable names
let x = 10;
let y = 5;
let z = x + y;

// Good: Descriptive variable names
let totalPrice = 10;
let discount = 5;
let finalPrice = totalPrice - discount;
```

#### Python

```python
# Bad: Ambiguous variable names
x = 10
y = 5
z = x + y

# Good: Descriptive variable names
total_price = 10
discount = 5
final_price = total_price - discount
```

---

## 3. Single Responsibility Principle (SRP)

Each function or class should have a single responsibility. Break down large functions into smaller, focused ones.

### **Example**

#### C

```c
// Bad: Monolithic function
void process_order(Order order) {
if (!order.is_valid()) return;
update_inventory(order);
generate_receipt(order);
send_notification(order);
}

// Good: Split into smaller functions
int is_order_valid(Order order) {
return order.is_valid();
}

void process_order(Order order) {
if (!is_order_valid(order)) return;
update_inventory(order);
generate_receipt(order);
send_notification(order);
}
```

#### C++

```cpp
// Bad: Monolithic function
void processOrder(Order order) {
if (!order.isValid()) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}

// Good: Split into smaller functions
bool isOrderValid(Order order) {
return order.isValid();
}

void processOrder(Order order) {
if (!isOrderValid(order)) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}
```

#### Rust

```rust
// Bad: Monolithic function
fn process_order(order: Order) {
if !order.is_valid() { return; }
update_inventory(order);
generate_receipt(order);
send_notification(order);
}

// Good: Split into smaller functions
fn is_order_valid(order: &Order) -> bool {
order.is_valid()
}

fn process_order(order: Order) {
if !is_order_valid(&order) { return; }
update_inventory(order);
generate_receipt(order);
send_notification(order);
}
```

#### Java

```java
// Bad: Monolithic function
public void processOrder(Order order) {
if (!order.isValid()) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}

// Good: Split into smaller functions
public boolean isOrderValid(Order order) {
return order.isValid();
}

public void processOrder(Order order) {
if (!isOrderValid(order)) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}
```

#### PHP

```php
// Bad: Monolithic function
function processOrder($order) {
if (!$order->isValid()) return;
updateInventory($order);
generateReceipt($order);
sendNotification($order);
}

// Good: Split into smaller functions
function isOrderValid($order) {
return $order->isValid();
}

function processOrder($order) {
if (!isOrderValid($order)) return;
updateInventory($order);
generateReceipt($order);
sendNotification($order);
}
```

#### JavaScript

```javascript
// Bad: Monolithic function
function processOrder(order) {
if (!order.isValid()) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}

// Good: Split into smaller functions
function isOrderValid(order) {
return order.isValid();
}

function processOrder(order) {
if (!isOrderValid(order)) return;
updateInventory(order);
generateReceipt(order);
sendNotification(order);
}
```

#### Python

```python
# Bad: Monolithic function
def process_order(order):
if not order.is_valid():
return
update_inventory(order)
generate_receipt(order)
send_notification(order)

# Good: Split into smaller functions
def is_order_valid(order):
return order.is_valid()

def process_order(order):
if not is_order_valid(order):
return
update_inventory(order)
generate_receipt(order)
send_notification(order)
```

---

## 4. Avoid Magic Numbers

Magic numbers are hardcoded values without context. Replace them with named constants.

### **Example**

#### C

```c
// Bad: Magic numbers
int shipping_cost = weight * 5;

// Good: Named constants
#define SHIPPING_RATE 5
int shipping_cost = weight * SHIPPING_RATE;
```

#### C++

```cpp
// Bad: Magic numbers
int shippingCost = weight * 5;

// Good: Named constants
const int SHIPPING_RATE = 5;
int shippingCost = weight * SHIPPING_RATE;
```

#### Rust

```rust
// Bad: Magic numbers
let shipping_cost = weight * 5;

// Good: Named constants
const SHIPPING_RATE: i32 = 5;
let shipping_cost = weight * SHIPPING_RATE;
```

#### Java

```java
// Bad: Magic numbers
int shippingCost = weight * 5;

// Good: Named constants
final int SHIPPING_RATE = 5;
int shippingCost = weight * SHIPPING_RATE;
```

#### PHP

```php
// Bad: Magic numbers
$shipping_cost = $weight * 5;

// Good: Named constants
define('SHIPPING_RATE', 5);
$shipping_cost = $weight * SHIPPING_RATE;
```

#### JavaScript

```javascript
// Bad: Magic numbers
let shippingCost = weight * 5;

// Good: Named constants
const SHIPPING_RATE = 5;
let shippingCost = weight * SHIPPING_RATE;
```

#### Python

```python
# Bad: Magic numbers
shipping_cost = weight * 5

# Good: Named constants
SHIPPING_RATE = 5
shipping_cost = weight * SHIPPING_RATE
```

---

## 5. Minimize Comments

Write self-documenting code to reduce the need for comments. Use comments only for complex or non-obvious logic.

### **Example**

#### C

```c
// Bad: Excessive comments
// Check if the user is eligible for a discount
if (user.age > 65) { // Senior citizen discount
apply_discount(user);
}

// Good: Self-documenting code
#define SENIOR_CITIZEN_AGE 65
if (user.age > SENIOR_CITIZEN_AGE) {
apply_discount(user);
}
```

#### C++

```cpp
// Bad: Excessive comments
// Check if the user is eligible for a discount
if (user.age > 65) { // Senior citizen discount
applyDiscount(user);
}

// Good: Self-documenting code
const int SENIOR_CITIZEN_AGE = 65;
if (user.age > SENIOR_CITIZEN_AGE) {
applyDiscount(user);
}
```

#### Rust

```rust
// Bad: Excessive comments
// Check if the user is eligible for a discount
if user.age > 65 { // Senior citizen discount
apply_discount(user);
}

// Good: Self-documenting code
const SENIOR_CITIZEN_AGE: i32 = 65;
if user.age > SENIOR_CITIZEN_AGE {
apply_discount(user);
}
```

#### Java

```java
// Bad: Excessive comments
// Check if the user is eligible for a discount
if (user.getAge() > 65) { // Senior citizen discount
applyDiscount(user);
}

// Good: Self-documenting code
final int SENIOR_CITIZEN_AGE = 65;
if (user.getAge() > SENIOR_CITIZEN_AGE) {
applyDiscount(user);
}
```

#### PHP

```php
// Bad: Excessive comments
// Check if the user is eligible for a discount
if ($user->age > 65) { // Senior citizen discount
applyDiscount($user);
}

// Good: Self-documenting code
define('SENIOR_CITIZEN_AGE', 65);
if ($user->age > SENIOR_CITIZEN_AGE) {
applyDiscount($user);
}
```

#### JavaScript

```javascript
// Bad: Excessive comments
// Check if the user is eligible for a discount
if (user.age > 65) {
// Senior citizen discount
applyDiscount(user);
}

// Good: Self-documenting code
const SENIOR_CITIZEN_AGE = 65;
if (user.age > SENIOR_CITIZEN_AGE) {
applyDiscount(user);
}
```

#### Python

```python
# Bad: Excessive comments
# Check if the user is eligible for a discount
if user.age > 65: # Senior citizen discount
apply_discount(user)

# Good: Self-documenting code
SENIOR_CITIZEN_AGE = 65
if user.age > SENIOR_CITIZEN_AGE:
apply_discount(user)
```

---

## Conclusion

By following these **five clean code principles**, you can write code that is:

- **Readable**: Easy for others (and your future self) to understand.
- **Maintainable**: Simplifies debugging, testing, and updating.
- **Professional**: Demonstrates good coding practices, especially in interviews.

Remember, clean code is not just about functionality—it's about creating a positive experience for everyone who interacts with your code. Start applying these principles today, and watch your coding skills improve!

Happy coding! 🚀