Skip to content

Commit

Permalink
refactor: add BCondition and nested ternary (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolluset authored Jan 16, 2025
1 parent c0476d9 commit d40f3bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
15 changes: 5 additions & 10 deletions code/examples/ternary-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

## 📝 코드 예시

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

```typescript
const status =
A조건 && B조건 ? "BOTH" : 둘다아닌경우 ? "NONE" : A조건 ? "A" : undefined;
(A조건 && B조건) ? "BOTH" : (A조건 || B조건) ? (A조건 ? "A" : "B") : "NONE";
```

## 👃 코드 냄새 맡아보기
Expand All @@ -27,14 +27,9 @@ const status =

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

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

if (A조건 && B조건) return "BOTH";
if (A조건) return "A";
if (B조건) return "B";
return "NONE";
})();
```
25 changes: 10 additions & 15 deletions en/code/examples/ternary-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +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
(ACondition && BCondition)
? "BOTH"
: NotBoth
? "NONE"
: ACondition
? "A"
: undefined;
: (ACondition || BCondition)
? (ACondition
? "A"
: "B")
: "NONE";
```

## 👃 Smell the Code
Expand All @@ -33,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";
}

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

0 comments on commit d40f3bb

Please sign in to comment.