2016年12月13日 星期二

Laracasts Tips

Request::exists('popular')
?popular  => true
?nopopular => false

Request::has('popular')
?popular => false
?popular=hasValue => true

Filter Model
$lesson = (new Lesson)->newQuery();
if($request->exists('popular')){
    $lesson->orderBy('views','desc');
}

if($request->has('difficulty')){
    $lesson->where('difficulty', $request->difficulty);
}

return $lesson->get();

Simpler Code
1. No Abbreviations
2. Don't use else
    - Defensive code
    - Guard
    - Reflection
3. One Level of Indentation
4. Limit you instance variables
    - One responsibility for one class ( No more that 4 instance variables)
5. Wrap  Primitives
    - Doest it bring clarity
    - Is there behavior?
      class EmailAdress {
          public function __construct($email){
              if ( ! filter_var($email, FILTER_VALIDATE_EMAIL)){
                  throw new InvalidArgumentException;
              }
              $this->email = $email;
          }
      }
    - Consistency
    - Important domain concept
      class Location {
          public function __construct($latitude, $longtitude){
          }
      }

沒有留言:

wibiya widget