Skip to content

Commit

Permalink
refactor: add BCondition and nested ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
tolluset committed Jan 15, 2025
1 parent 5a8da97 commit 04bdc46
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
18 changes: 7 additions & 11 deletions code/examples/ternary-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

## 📝 코드 예시

다음 코드는 `A조건``B조건`에 따라서 `'BOTH'`, `'A'`, 또는 `'NONE'` 중 하나를 `status`에 지정하는 코드예요.
다음 코드는 `A조건``B조건`에 따라서 `"BOTH"`, `"A"`, `"B"` 또는 `"NONE"` 중 하나를 `status`에 지정하는 코드예요.

```typescript
const status = A조건 && B조건 ? "BOTH" : A조건 ? "A" : "NONE";
const status =
(A조건 && B조건) ? "BOTH" : (A조건 || B조건) ? (A조건 ? "A" : "B") : "NONE";
```

## 👃 코드 냄새 맡아보기
Expand All @@ -26,14 +27,9 @@ const status = A조건 && B조건 ? "BOTH" : A조건 ? "A" : "NONE";

```typescript
const status = (() => {
if (A조건 && B조건) {
return "BOTH";
}

if (A조건) {
return "A조건";
}

return "NONE";
if (A조건 && B조건) return "BOTH";
if (A조건) return "A";
if (B조건) return "B";
return "NONE";
})();
```
24 changes: 13 additions & 11 deletions en/code/examples/ternary-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ Using complex ternary operators can obscure the structure of the conditions, mak

## 📝 Code Example

The following code assigns `'BOTH'`, `'A'`, or `'NONE'` to `status` based on `ACondition` and `BCondition`.
The following code assigns `"BOTH"`, `"A"`, `"B"`, or `"NONE"` to `status` based on `ACondition` and `BCondition`.

```typescript
const status = ACondition && BCondition ? "BOTH" : ACondition ? "A" : "NONE";
const status =
(ACondition && BCondition)
? "BOTH"
: (ACondition || BCondition)
? (ACondition
? "A"
: "B")
: "NONE";
```

## 👃 Smell the Code
Expand All @@ -26,14 +33,9 @@ You can rewrite the conditions using `if` statements, as shown below, to make th

```typescript
const status = (() => {
if (ACondition && BCondition) {
return "BOTH";
}

if (ACondition) {
return "A";
}

return "NONE";
if (ACondition && BCondition) return "BOTH";
if (ACondition) return "A";
if (BCondition) return "B";
return "NONE";
})();
```

0 comments on commit 04bdc46

Please sign in to comment.