---
# System prepended metadata

title: Better Code
tags: [' coding', ' examples', ' WIP', common]

---

---
title: Better Code
tags: common, coding, examples, WIP
---

#  Better Coding

##### Unnecessary If Statements
# 1 Returning a Boolean value
**Bad Code**
```
public function valid_feedback_id(Request $request) {
    $Response = DB::table('feedbacks')->where('employee_id', $request['employee_id'])->where('id', $request['feedback_id'])->get();
    if(count($Response) > 0) {
        return true;
    }
    else {
        return false;
    }
}
```
**Better Code**
```
public function valid_feedback_id(Request $request) {
    $Response = DB::table('feedbacks')->where('employee_id', $request['employee_id'])->where('id', $request['feedback_id'])->get();
    return count($Response) > 0;
}
```

**Explanation**
The code above uses if statement just to return a specific boolean value

Unnecessary Else statement
**Bad**
```
if ($status != Password::PASSWORD_RESET) {
    throw ValidationException::withMessages([
        'status' => __($status),
    ]);
} else {
    return __($status);
}
```
**Better**

```
if ($status != Password::PASSWORD_RESET) throw ValidationException::withMessages([ 'status' => __($status), ]);
return __($status);
```

**Explanation**


Unnecessary assignment of variable

**Bad**
```
protected function createToken($user)
{
    $token = NULL;

    if (is_null($user->password_updated_at)) {
        $token = \Illuminate\Support\Facades\Password::broker()->createToken($user);
    }

    return $token;
}
```

**Better**

```
protected function createToken($user)
{
    if (is_null($user->password_updated_at)) return \Illuminate\Support\Facades\Password::broker()->createToken($user);
}
```

###### Using Literal Values or Magic Number
**Bad**
```
public function getRequestStatusTextAttribute(){

    if($this->request_status === '0'){
        return '依頼中';
    }
    return '完了';
}
```

**Better**
```
public function getRequestStatusTextAttribute(){
    if(isset(RequestStatus::TEXTS[$this->request_status])) return RequestStatus::TEXTS[$this->request_status];
    return RequestStatus::NON_REQUEST_TEXT;
}
```

**Explanation**


##### Nested if inside else statements

**Bad**
```
public function dashboard() {
    $page = ['title' => 'トップメニュー'];
    $user = Auth::user();
    $action = 'dashboard';
    $company = $user->company;

    session()->forget('order');

    if ($user->isAdmin || $user->isSemiAdmin) {
        return view('dashboard',compact('page','user', 'company'));
    } else {
        if ($user->first_login == null) {
            return redirect('terms-of-service');
        } else {
            $hasExamResultsfromExaminers = false;
            if ($user->isExamManager) {
                $manager = ExamManager::where('id', $user->id)->first();
                foreach($manager->examiners as $examiner) {
                    if ($examiner->hasExamResults) {
                        $hasExamResultsfromExaminers = true;
                    }
                }
            }
            return view('dashboard',compact('page','user', 'company', 'hasExamResultsfromExaminers'));
        }
    }
}
```
**Better**
```
public function dashboard() {
    $page = ['title' => 'トップメニュー'];
    $user = Auth::user();
    $action = 'dashboard';
    $company = $user->company;

    session()->forget('order');

    if ($user->isAdmin || $user->isSemiAdmin) { return view('dashboard',compact('page','user', 'company')); }
    if ($user->first_login == null) { return redirect('terms-of-service'); }

    $hasExamResultsfromExaminers = false;
    if ($user->isExamManager) {
        $manager = ExamManager::where('id', $user->id)->first();
        foreach($manager->examiners as $examiner) {
            if ($examiner->hasExamResults) { $hasExamResultsfromExaminers = true; }
        }
    }
    return view('dashboard',compact('page','user', 'company', 'hasExamResultsfromExaminers'));
}
    ```