> 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/small-and-focused.md).

# Small & Focused

ধরুন আপনি ৬ মাস পরে নিজের লেখা কোডে ফিরে গেলেন, আর নিজেই বুঝতে পারলেন না এটা কী করছে — এটা খুব কমন। কারণ আমরা অনেকেই ফাংশন লেখার সময় Small & Focused  নীতি মানি না।

### ছোট ফাংশন = ক্লিয়ার দৃষ্টিভঙ্গি

একটা ভালো ফাংশন যেন হয় একজোড়া চোখের মতো — দেখে সঙ্গে সঙ্গে বুঝে ফেলা যায়! ক্লিন কোডে ফাংশনের জন্য সবচেয়ে গুরুত্বপূর্ণ ২টি গুণ:

1. **Small Function:** একটি ফাংশনের Length হওয়া উচিত সংক্ষিপ্ত। সাধারনত ৫-১৫ লাইনের মধ্যে।
2. **Focused Function:** একটি ফাংশন শুধুমাত্র একটি কাজ করবে, এবং সেটা ভালোভাবে করবে। একাধিক কাজ এক ফাংশনে থাকলে বিভ্রান্তি ও বাগ দুই-ই বাড়ে।

***

### Small & Focused Function বুঝতে চেকলিস্ট

এখানে কিছু প্রশ্ন যা আপনি ফাংশন লেখার সময় নিজেকে করতে পারেন&#x20;

#### ১. ফাংশনের নাম কি Verb বা Phrase?

ভালো ফাংশনের নাম **verb** বা **verb phrase** দিয়ে শুরু হয় — কারণ ফাংশন “কিছু করে”।

**ভালো :**

{% code lineNumbers="true" %}

```python
def send_email(): pass
def generate_invoice_pdf(): pass
```

{% endcode %}

**খারাপ:**

{% code lineNumbers="true" %}

```python
def email(): pass
def invoice(): pass
```

{% endcode %}

***

#### ২. একটি ফাংশন কি শুধুমাত্র একটি কাজ করছে?

এক ফাংশনে যদি আপনি `and`, `then`, `also`, `as well` এসব ভাবছেন — বুঝবেন ফাংশনটি একাধিক কাজ করছে।

**খারাপ:**

{% code lineNumbers="true" %}

```python
def manage_user_account(user):
    validate(user)
    create(user)
    send_email(user)
```

{% endcode %}

**ভালো:**

{% code lineNumbers="true" %}

```python
def validate_user(user): pass
def create_user(user): pass
def send_welcome_email(user): pass
```

{% endcode %}

***

#### ৩. ফাংশন কি খুব বড় হয়ে গেছে?

ফাংশন বড় মানেই সে একাধিক Responsibility নিচ্ছে। রুল অফ থাম্ব: ১৫ লাইনের বেশি হলে রিভিউ করুন — ভাগ করা সম্ভব কিনা।

***

#### ৪. ফাংশনে কি ২টির বেশি Parameter আছে?

বেশি প্যারামিটার মানে অনেক ইনপুট, অনেক কাজ — সেটা ক্লিন ফাংশনের পরিপন্থী।

**খারাপ:**

{% code lineNumbers="true" %}

```python
def register_user(name, email, password, dob, role): pass
```

{% endcode %}

**ভালো:**

{% code lineNumbers="true" %}

```python
def register_user(user_data): pass
```

{% endcode %}

অথবা `User` মডেল/অবজেক্ট ব্যবহার করুন।

***

#### ৫. ফাংশনে কি Side Effect আছে?

Side effect মানে এমন কিছু পরিবর্তন যা ফাংশনের মূল কাজের বাইরে গিয়ে ঘটছে — যেমন:\
global variable পরিবর্তন,\
File Write করে ফেলা,\
HTTP call ইত্যাদি

এগুলো isolate করে রাখাই Best।

***

#### ৬. Return Early ব্যবহার করা হয়েছে?

ফাংশন যত তাড়াতাড়ি ভুল ইনপুট পেলেই বেরিয়ে আসবে, ততই পরিষ্কার ও ফ্ল্যাট থাকবে।

**খারাপ:**

{% code lineNumbers="true" %}

```python
def process_order(order):
    if order:
        save_order(order)
        send_invoice(order)
    else:
        return "Invalid order"
```

{% endcode %}

**ভালো:**

{% code lineNumbers="true" %}

```python
def process_order(order):
    if not order:
        return "Invalid order"
    
    save_order(order)
    send_invoice(order)
```

{% endcode %}

***

### খারাপ কোড বনাম ভালো কোড উদাহরণ

#### খারাপ ফাংশন (Big & Messy):

{% code lineNumbers="true" %}

```python
def handle_user_registration(user):
    if not user["email"]:
        print("Invalid email")
        return

    save_to_db(user)
    send_welcome_email(user["email"])
    log_activity("User registered")
    assign_default_role(user["id"])
```

{% endcode %}

এক ফাংশনে ৪টা আলাদা দায়িত্ব!

***

#### ভালো ফাংশন (Small & Focused):

```python
def handle_user_registration(user):
    if not is_valid_email(user["email"]):
        return

    register_user(user)
    notify_user(user["email"])
    assign_default_permissions(user["id"])

def is_valid_email(email): pass
def register_user(user): pass
def notify_user(email): pass
def assign_default_permissions(user_id): pass
```

✅ প্রতিটি ফাংশন নিজস্ব দায়িত্বে ফোকাসড\
✅ পড়তে ও টেস্ট করতে সহজ

***

### বাস্তব অভিজ্ঞতায় এই নিয়ম না মানলে কী হয়?

১. কোড রিভিউয়ে অন্যরা বুঝে না আপনি কী করছেন\
2\. bug ফিক্স করতে গেলে নতুন bug ঢুকে পড়ে\
3\. টেস্ট করা কঠিন হয়ে পড়ে\
4\. নতুন ফিচার যোগ করা প্রায় অসম্ভব হয়ে দাঁড়ায়

***

### ফাংশন লেখার সঠিক অভ্যাস গড়ে তুলুন

ভালো ফাংশন যেন হয়:

✔ নাম থেকেই কাজটা বোঝা যায়\
✔ শুধুমাত্র একটি কাজ করে\
✔ ছোট, রিডেবল, রিইউজেবল\
✔ side effect-free\
✔ test করতে সুবিধা হয়\
✔ return early ফলো করে

***

### Summary

* ফাংশন মানেই কর্ম, তাই নামটা **verb বা verb phrase** দিন
* ১ ফাংশন = ১ দায়িত্ব = ১ ফোকাস
* বড় ফাংশন মানেই রিফ্যাক্টরের সুযোগ
* বারবার একই স্ট্রাকচার দেখলে তা `helper function` করুন
* ‍Side effect isolate করুন (যেমন: DB, Email, API Call)

***

আপনার ফাংশনগুলোকে এমনভাবে লিখুন যেন সেটা পড়া যায় গল্পের মতো —

> When code reads like prose, bugs hide no more.
