👨💻Simple rules for simple code
No abbreviation
Bad:
class Trnsltr
{
}
foreach ($x as $people) {
}
class UserRepository
{
public function fetch($billingId)
{
}
}
class Order
{
public function prepAndShipAndNotifyUser()
{
}
}
Good:
class Translator
{
}
foreach ($person as $people) {
}
class UserRepository
{
public function fetchByBillingId($id)
{
}
}
class Order
{
public function process()
{
}
}
Don't use else
Bad:
public function store()
{
$input = Request::all();
$validation = Validator::make($input, ['username' => 'required']);
if (date('l') != 'Friday') {
if ($validation->passes()) {
Post::create($input);
return Redirect::home();
} else {
return Redirect::back()->withInput()->withErrors($validation);
}
} else {
throw new Exception('We do not work on Friday!!!');
}
}
public function signup(subscription $subscription)
{
if ($subscription) {
$this->createmonthlysubscription();
} else {
$this->createforeversubscription();
}
}
Good:
public function store()
{
$input = Request::all();
$validation = Validator::make($input, ['username' => 'required']);
if (date('l') == 'Friday') {
throw new Exception('We do not work on Friday!!!');
}
if ($validation->failed()) {
return Redirect::back()->withInput()->withErrors($validation);
}
Post::create($input);
return Redirect::home();
}
public function signup(subscription $subscription)
{
$subscription->create();
}
function getSubscription($type)
{
if ($type == 'forever') {
return new ForeverSubscription();
}
return new MonthlySubsctiprion();
}
signUp(getSubscription($type));
One level of indentation
Bad:
class BankAccount
{
protected $accounts;
public function __construct($accounts)
{
$this->accounts = $accounts;
}
public function filterBy($accountType)
{
$filtered = [];
foreach($this->accounts as $account) {
if ($account->type() == $accountType) {
if ($account->isActive()) {
$filtered[] = $account;
}
}
}
return $filtered;
}
}
class Account
{
protected $type;
public function __construct($type)
{
$this->type = $type;
}
public static function open($type)
{
return new static($type);
}
public function type()
{
return $this->type;
}
public function isActive()
{
return true;
}
}
$accounts = [
Account::open('checking'),
Account::open('saving'),
Account::open('checking'),
Account::open('savings'),
];
$accounts = new BankAccounts($accounts);
Good:
class BankAccount
{
protected $accounts;
public function __construct($accounts)
{
$this->accounts = $accounts;
}
public function filterBy($accountType)
{
return array_filter($this->account, function ($account) use ($accountType) {
return $account->isOfType($accountType);
});
}
}
class Account
{
protected $type;
public function __construct($type)
{
$this->type = $type;
}
public static function open($type)
{
return new static($type);
}
public function isOfType($accountType)
{
return $this->type() == $accountType && $this->isActive();
}
protected function type()
{
return $this->type;
}
protected function isActive()
{
return true;
}
}
$accounts = [
Account::open('checking'),
Account::open('saving'),
Account::open('checking'),
Account::open('savings'),
];
$accounts = new BankAccounts($accounts);
Limit your instance variable
Bad:
class UsersController
{
protected $userService;
protected $registrationService;
protected $userRepository;
protected $stripe;
protected $mailer;
protected $userEventRepository;
protected $logger;
public function __construct(
UserService $userService,
RegistrationService $registrationService,
UserRepository $userRepository,
Stripe $stripe,
Mailer $mailer,
UserEventRepository $userEventRepository,
Logger $logger
)
{
}
}
Good:
<?php
class UsersController
{
protected $userService;
protected $mailer;
protected $logger;
public function __construct(UserService $userService)
{
}
}
Last updated