> 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/unnecessary-complexity.md).

# Unnecessary Complexity

মাঝে মাঝে আমরা নিজেরা নিজেদের কোড unnecessarily complex করে ফেলি — একটু clever হবার চেষ্টা,\
future-proof করতে গিয়ে অতিরিক্ত generic বানানো, অথবা শুধুই over-engineering! আর এতে শেষে হয় কী? বোঝা যায় না কোডটা কী করছে, debug করা কষ্টের, এবং মেইনটেইন করা দুঃস্বপ্ন

***

### অপ্রয়োজনীয় কমপ্লেক্সিটির লক্ষণগুলো:

1. জটিল লজিক যা সহজে করা যেত
2. Nested structure-এর ভিতর আরেক nested
3. Premature optimization
4. Generic করার চেষ্টা, যেখানে দরকারই নেই
5. Clever tricks → বোঝা মুশকিল!

### উদাহরণসহ ব্যাখ্যা (Python)

#### উদাহরণ: অতিরিক্ত জেনেরিক বানানো

{% code lineNumbers="true" %}

```python
def operate(operation, a, b):
    if operation == "add":
        return a + b
    elif operation == "multiply":
        return a * b
    # আরও অনেক future operation আসবে ভেবে বানানো
    elif operation == "divide":
        return a / b
    elif operation == "modulo":
        return a % b
```

{% endcode %}

➡️ এখনও হয়তো শুধু যোগ আর গুণ দরকার, কিন্তু unnecessary future-proof করতে গিয়ে কমপ্লেক্স করে ফেলেছি।

***

#### ভালো উদাহরণ (যতটুকু দরকার ততটুকুই)

{% code lineNumbers="true" %}

```python
def add(a, b):
    return a + b

def multiply(a, b):
    return a * b
```

{% endcode %}

➡️ Simple, readable, এবং কাজের জন্য যথেষ্ট।

***

#### উদাহরণ: এক লাইনে cleverness দেখানো

{% code lineNumbers="true" %}

```python
result = [x for x in data if x % 2 == 0 if x > 10 if x < 100]
```

{% endcode %}

কারো বুঝতেই সময় লেগে যাবে — কী হচ্ছে এখানে?

***

#### ভালো উদাহরণ: স্টেপ বাই স্টেপ ক্লিয়ার

{% code lineNumbers="true" %}

```python
result = []
for x in data:
    if x % 2 == 0 and 10 < x < 100:
        result.append(x)
```

{% endcode %}

➡️ সহজে বোঝা যায়, সহজে মেইনটেইন করা যায়।

***

#### উদাহরণ: Deep nesting

{% code lineNumbers="true" %}

```python
if user:
    if user.is_active:
        if user.has_permission('admin'):
            do_admin_task()
```

{% endcode %}

➡️ একের পর এক nesting। 😵

***

#### ভালো উদাহরণ: Return Early

{% code lineNumbers="true" %}

```python
if not user or not user.is_active or not user.has_permission('admin'):
    return

do_admin_task()
```

{% endcode %}

➡️ কমপ্লেক্সিটি হ্রাস পেলো, ক্লিন ও সোজা।

***

#### উদাহরণ: Abstract করা হলেও অতিরিক্ত Generic

{% code lineNumbers="true" %}

```python
def execute_task(task_type, *args, **kwargs):
    if task_type == 'email':
        send_email(*args, **kwargs)
    elif task_type == 'log':
        log_data(*args, **kwargs)
    elif task_type == 'notify':
        notify_user(*args, **kwargs)
```

{% endcode %}

➡️ অনেক কিছু এক ফাংশনে ঢোকানো — future-proof করতে গিয়ে এখনকার readability হারিয়ে গেছে।

***

#### ভালো উদাহরণ:

{% code lineNumbers="true" %}

```python
def send_email(to, message):
    pass

def log_data(data):
    pass

def notify_user(user, alert):
    pass
```

{% endcode %}

➡️ প্রতিটি কাজের জন্য একটি ছোট ফাংশন। সহজ, বুঝতে সুবিধা।

***

### জেনে রাখুন: Simplicity is not stupidity

> Simple কোড মানে হলো — বোঝার মতো সহজ, বোকার মতো নয়।

Einstein বলেছিলেন —

> Everything should be made as simple as possible, but not simpler.

***

### কীভাবে Simple রাখবেন?

| প্রশ্ন                                      | হ্যাঁ হলে চিন্তা করুন |
| ------------------------------------------- | --------------------- |
| এটা কি খুব generic করার চেষ্টা?             | ✅                     |
| future-proof করতে গিয়ে কি বর্তমান হারাচ্ছি? | ✅                     |
| clever হলেও কি কেউ বুঝবে না?                | ✅                     |
| nested levels ২টার বেশি হয়ে গেছে?           | ✅                     |
| কি হচ্ছে তা বুঝতে বারবার পড়তে হচ্ছে?        | ✅                     |

### Summary

1. Write for humans first, computer second
2. Break things down
3. Avoid unnecessary parameters / abstraction
4. Return early to reduce nesting
5. Use meaningful names
