Comments (5)
Most of them. I think my real question is: what are the standards to be considered as "best practices"? I mean, everyone has his/her own way and style of writing codes. What makes some of the styles be "best practices"?
Example: Single responsibility principle.
Currently, I am writing with the "bad" practice. I don't see how the separating the logic into several small functions is a better way. For me, it seems like there are more functions scattered around and makes longer codes, which could be hard to maintain in future.
- I am trying to understand the benefits of each best practices shared here, and slowly to change my code to use the best practice.
Thank you.
from laravel-best-practices.
The best practices should help you to reduce the in-mind-programming and make your code so clear, that you and others can read it as is would be englisch language.
Taking your example of SRP:
The bad variant is how programming is taught. You see this style in most of the 101 programming tutorials (offline and online) and in university where beginners get introduced into the concepts of programming. And this is fine because at the beginning you have to learn so much concepts at once, so that its not important or overwhelming to think in this direction. Your code works and you are happy :-)
You have one method and with all the stuff inside what should happen.
public function getFullNameAttribute()
{
if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) {
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
} else {
return $this->first_name[0] . '. ' . $this->last_name;
}
}
Lets break it down:
if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) {
To understand this if statement you need a deep understanding of Laravel because you have to know that there is an auth()
method, a method to get the role and one to figure out if the user is verified.
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
In the first case of the if statement, the name is appended with Mr. (which is a bit weird, because a user could also be female) and the middlename is included.
return $this->first_name[0] . '. ' . $this->last_name;
In the 2nd case you have to know that $this->first_name
is an array and the firstname is at index 0
, (which seems weird to me as I am reading this).
So a lot of in-depth knowledge is necessary to grasp this little method which does nothing else than outputting a different user name depending on some internal states of the user.
Lets have a look of the good example at the same method:
public function getFullNameAttribute()
{
return $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort();
}
- its only one line -> less to read
- its clear to any programmer what this code does without having much knowledge of the implementation details of Laravel
- even the programmer who wrote that code can benefit:
- he doesn't need to know at that point what the fullname or the shortname is. These are some details defined elsewhere.
Imagine you need a verified client multiple times in your project. With the bad example you have to write the same stuff over and over again, while with the good example you have it defined in one place and if you want to change it later you have to do it only once.
And don't get worried about too much methods in your class. There is no limit of methods a programming language or computer can handle, but a limit of lines per method a human can handle.
My methods are mostly 5-10 lines long. If they grow longer it is a sign to extract something from it into a dedicated method.
from laravel-best-practices.
Do you have any particular best practice in mind you need an explanation for?
from laravel-best-practices.
Please read about Solid principles and also the Laravel documentation to better understand why this best practices are in place. Laracasts has a great lesson about solid.
from laravel-best-practices.
@Konafets I dont see how using the auth() helper in a model can be a best practice. it's a hidden dependency.
I mean, session level should not interfere in the model level. it's a logic to be put in the controller.
from laravel-best-practices.
Related Issues (20)
- What PHP to JS package is recommended? HOT 4
- How about throw exceptions? HOT 5
- Do not get data from the .env file directly, why? HOT 2
- What about laravel Resources? HOT 3
- Wrong return type? HOT 1
- PSR-12 HOT 3
- What about translation variables? HOT 2
- Best
- Interface best practice HOT 5
- Converting from and back to json HOT 1
- Validation FormRequest HOT 1
- Looking for the repo owner HOT 8
- Typo Issue in example (Don't repeat yourself (DRY)) HOT 2
- Store dates in the standard format. Use accessors and mutators to modify date format HOT 11
- Seeder in singular -> Good, Seeder in plural -> bad (naming-conventions) HOT 2
- Recommended route name conflict?
- A tool that enforces all these best practices HOT 1
- Change test case methods to snake_case HOT 1
- Query Parameter Validation HOT 1
- La
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-best-practices.