---
title: "تفكيك التعبيرات الشرطية"
type: "refactoring-technique"
slug: "decompose-conditional"
url: "http://localhost:3000/ar/decompose-conditional.md"
category: "تبسيط التعبيرات الشرطية"
description: "المشكلة: لديك تعبير شرطي معقد (if-then/else أو switch). الحل: قم بتفكيك الأجزاء المعقدة من التعبير الشرطي إلى دوال منفصلة: الشرط (condition)، وثم (then)، وإلا (else)."
languages: ["java", "csharp", "php", "python", "typescript"]
---
# تفكيك التعبيرات الشرطية

> المشكلة: لديك تعبير شرطي معقد (if-then/else أو switch). الحل: قم بتفكيك الأجزاء المعقدة من التعبير الشرطي إلى دوال منفصلة: الشرط (condition)، وثم (then)، وإلا (else).

## المشكلة

لديك تعبير شرطي معقد (`if-then`/`else` أو `switch`).

## الحل

قم بتفكيك الأجزاء المعقدة من التعبير الشرطي إلى دوال منفصلة: الشرط (condition)، و `then` و `else`.

## دواعي إعادة الهيكلة

كلما زاد طول كود برمجى، زادت صعوبة فهمه. وتصبح الأمور أكثر صعوبة عندما يمتلئ الكود بالشروط:

* بينما تكون مشغولاً بمعرفة ما يفعله الكود في كتلة `then`، فإنك تنسى ما كان عليه الشرط ذو الصلة.
* بينما تكون مشغولاً بتحليل `else`، فإنك تنسى ما يفعله الكود في `then`.

## الفوائد

* من خلال استخراج كود الشرط إلى دوال ذات أسماء واضحة، فإنك تجعل الحياة أسهل للشخص الذي سيقوم بصيانة الكود لاحقاً (مثل نفسك، بعد شهرين من الآن!).
* تقنية إعادة الهيكلة هذه قابلة للتطبيق أيضاً على التعبيرات القصيرة في الشروط. النص `isSalaryDay()` أكثر جمالاً ووصفاً من كود مقارنة التواريخ.

## كيفية إعادة الهيكلة

1. استخرج الشرط إلى دالة منفصلة عبر طريقة [استخراج الدالة](/extract-method).
2. كرر العملية لكتلتي `then` و `else`.
## Relations

**Eliminates smells**

- [الدالة الطويلة](/ar/smells/long-method.md)

## Code Examples

### java

```java
// Before
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
  charge = quantity * winterRate + winterServiceCharge;
}
else {
  charge = quantity * summerRate;
}

// After
if (isSummer(date)) {
  charge = summerCharge(quantity);
}
else {
  charge = winterCharge(quantity);
}
```

### csharp

```csharp
// Before
if (date < SUMMER_START || date > SUMMER_END) 
{
  charge = quantity * winterRate + winterServiceCharge;
}
else 
{
  charge = quantity * summerRate;
}

// After
if (isSummer(date))
{
  charge = SummerCharge(quantity);
}
else 
{
  charge = WinterCharge(quantity);
}
```

### php

```php
// Before
if ($date->before(SUMMER_START) || $date->after(SUMMER_END)) {
  $charge = $quantity * $winterRate + $winterServiceCharge;
} else {
  $charge = $quantity * $summerRate;
}

// After
if (isSummer($date)) {
  $charge = summerCharge($quantity);
} else {
  $charge = winterCharge($quantity);
}
```

### python

```python
// Before
if date.before(SUMMER_START) or date.after(SUMMER_END):
    charge = quantity * winterRate + winterServiceCharge
else:
    charge = quantity * summerRate

// After
if isSummer(date):
    charge = summerCharge(quantity)
else:
    charge = winterCharge(quantity)
```

### typescript

```typescript
// Before
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
  charge = quantity * winterRate + winterServiceCharge;
}
else {
  charge = quantity * summerRate;
}

// After
if (isSummer(date)) {
  charge = summerCharge(quantity);
}
else {
  charge = winterCharge(quantity);
}
```

