-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can we have reasons or explanation on the best practices? #35
Comments
Do you have any particular best practice in mind you need an explanation for? |
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.
Thank you. |
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. |
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: You have one method and with all the stuff inside what should happen.
Lets break it down:
To understand this if statement you need a deep understanding of Laravel because you have to know that there is an
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.
In the 2nd case you have to know that 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:
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. |
@Konafets I dont see how using the auth() helper in a model can be a best practice. it's a hidden dependency. |
No description provided.
The text was updated successfully, but these errors were encountered: