> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/random-topics/clean-code/formatting-and-structure.md).

# Formatting & Structure

একজন প্রোগ্রামার যখন কোড লেখেন, তখন একবার লেখেন — কিন্তু সেই কোড অনেক বার পড়া হয়।\
সেজন্যই কোড এমনভাবে ফরম্যাট করা উচিত, যেন চোখে পড়া, মাথায় ঢোকা আর বোঝা — সবটাই সহজ হয়। আর কোডে Structure ঠিক না থাকলে, সেটা রেঁধে রাখা মেসির মতো লাগতে পারে

***

### Structure ও Formatting মানে কী?

* কোডের লেআউট
* ইনডেন্টেশন
* লাইন ব্রেক
* ব্লক ভাঙ্গা
* ফাংশন অর্ডার
* import structuring
* এবং সবশেষে — Consistency!

### Best Practices

#### ১. **Consistent Indentation**

Python এ indentation মানেই structure।

**খারাপ:**

```python
def greet():
  print("Hello")
   print("World")
```

**ভালো:**

```python
def greet():
    print("Hello")
    print("World")
```

***

#### ২. **Logical Grouping of Code**

একই responsibility-এর কোড একসাথে রাখুন।

**খারাপ:**

```python
def calculate_tax():
    log_transaction()
    base = get_income()
    tax = base * 0.15
    return tax
```

**ভালো:**

```python
def calculate_tax():
    base = get_income()
    tax = base * 0.15
    log_transaction()
    return tax
```

➡️ লগিং আলাদা concern, তাই শেষে বা আলাদা হ্যান্ডল করা যায়।

***

#### ৩. **Blank Line ব্যাবহার করুন Section Break হিসেবে**

**খারাপ:**

```python
def calculate():
    x = 1
    y = 2
    z = x + y
    print(z)
    return z
```

**ভালো:**

```python
def calculate():
    x = 1
    y = 2
    z = x + y

    print(z)
    return z
```

➡️ **Blank line =** নিঃশ্বাস নেওয়ার সময়। চোখও একটু বিশ্রাম পায়।

***

#### ৪. **Similar ফাংশন একসাথে রাখুন**

যেমন: getter-setter, validator/helper, অথবা model-related functions।

***

#### ৫. **Import গুলো ঠিকমতো সাজান**

Python convention:

```python
# Standard library
import os
import datetime

# Third-party
import requests

# Local app
from .models import User
```

***

#### ৬. **ফাইল ভাঙুন বড় হলে**

একটি ফাইলে যখন অনেক বেশি ফাংশন বা ক্লাস হয়ে যায়, সেটা একসময় বোঝা কঠিন হয়ে যায়।\
তখন ভাগ করে ফেলুন:

* `utils/validation.py`
* `utils/formatting.py`

➡️ Clean Structure মানে Easy Maintenance!

***

### Structure Example: খারাপ বনাম ভালো

#### খারাপ:

```python
import requests
import os
from .models import User
def create_user():
    user = User()
    user.save()
    print("Created")

def validate():
    pass
```

***

#### ভালো:

```python
# 📁 user_handler.py

import os
import requests
from .models import User

def validate_user(user_data):
    # Validation logic here
    pass

def create_user():
    user = User()
    user.save()
    print("User created successfully")
```

***

### Formatting Tips Summary

| নিয়ম              | ভালো অভ্যাস                               |
| ----------------- | ----------------------------------------- |
| Indentation       | সর্বত্র ৪ স্পেস (Python standard)         |
| Imports           | Standard → Third-party → Local            |
| Blank lines       | Section break হিসেবে ব্যবহার করুন         |
| Function ordering | Similar কাজের ফাংশন একসাথে                |
| File structure    | বড় ফাইল ভেঙে ছোট করুন                     |
| লজিক ব্লক ভাগ     | কোড গুছিয়ে লিখুন, এক নিঃশ্বাসে বুঝে ফেলুন |
| Consistency       | এক স্টাইল, এক ধরণ                         |

Structure & Formatting কেবল স্টাইল নয়, এটা Team এর সবাইকে বোঝার পথ খুলে দেয়।\
যেখানে কেউ কোড পড়েও বোঝে — কী হচ্ছে, কেন হচ্ছে, এবং কোথায় হচ্ছে।

***
