Better Laravel/PHP Code: 5 Guidelines from Spatie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to share some guidelines how to write a better code when working in a team so it would be consistent readable and pleasant to use but these guidelines are not my own these are from belgium companies party well known in laravel community and recently i found out they have guidelines written for themselves for their own team but there's plenty of things that we can learn each and every one of us and even if you're not working in a team maybe you will in the future or maybe your code will be maintained and taken over by someone else so well-written and well-structured code is a goal for everyone and in here i've picked five tips five guidelines from laravel and php repository from spati and i will add my own comments discuss that and share a few tips what to read and what to watch afterwards so let's go laravel and php is the section of their guidelines it's free it's open source and you can even actually contribute to that so there's a link here to edit that github so if we click here there is a repository so there are pages that you can contribute in markdown language but in this video i've picked five topics to talk about and let's dive in topic number one is dog blocks and comments and it's an interesting almost philosophical discussion in my development career i've been always hearing that comment your code and then it will be understandable by everyone but what spati team is saying here and i agree with them don't use dog blocks for methods that can be fully type hinted so php now allows a lot of things to make the parameters clearer so type hint the parameters what the method returns the naming of the method so instead of writing dog blocks and comments you can use the php functions the php syntax to make it clear and understandable and similar thing related to comments so if we click on the comments here's the sentence comments should be avoided as much as possible by writing expressive code so basically the naming of your things in laravel or in php like variables methods class names and all of that like job names they should be understandable from their names not from the comments and also i like the idea by them that you can create a private method of that class or that controller for example so if you have a comment like start calculating loans or start some job and then there are five lines of code related to that so why don't you take those five lines and refactor into separate method with one name calculate loans and then it's readable without any comments i quite like this idea section number two that i've picked to discuss here in this video is if statements and there are three subsections so bracket position these curly brackets they should be always present especially in junior developers code i see they're trying to make their code shorter and avoid these and write the actual statement it could be one statement on the same line or on the next line below but in my opinion shorter code is not always better so if you have curly brackets you see visually that it's a block of code so even if it's one sentence i would totally vote for using curly brackets at all times and also with that if statements there are two things that i've personally learned in the past from jeffrey way from laracast its happy path means that a lot of developers focus on the good condition something that is good that is validated and correct and all of that and only then if something goes wrong they throw an exception but a better thing in my opinion and i agree with spicy guys is that first you focus on everything bad that could happen if something is not right you throw an exception if something is wrong you redirect back with error if something goes wrong then do something and only then otherwise if the validation passes then do all the work and related to that avoid else statements by doing so-called early returns so again same thing if your function can return false or return exception or something it should be in the beginning of the function and then if the conditions passed then you do the main thing instead of having this so if then if then else it is harder to read would you agree and it's not only about readable code it's about the mind thinking you focus on what could go wrong and this is one of the things that i lack in junior developers i think they don't think enough about exceptions about validations about something that could be inputted invalid about security issues and all of that and by doing those conditions first you can actually focus on what could go wrong and this would make your application more stable in the long run it's kind of like a habit that you can grow with every example and if you focus on bad things first it would actually be good for your application third topic i want to discuss is white space so when to use white space between the sentences and between the blocks of code piety team has a take on that that in general always add blank lines between statements but it depends on the context what is a statement this is a statement so it's kind of block of code around some condition and that should be white spaced before and after so compare this code to the code where everything is together would you agree that it is harder to read and understand what is what so again shorter code is not always better and fewer lines of code is not always better readability comes first but of course it depends on the context so for example in here in migrations you shouldn't do any spaces and too many spaces like in this case is also bad what i would say is that you should treat your code like paragraphs of text like you would write a book this is a paragraph with one thought and then there's another thought with a different paragraph paragraph may contain one sentence only or maybe more sentences and a few shorter topics towards the end of this video so validation spati team advises not to use separator for multiple validation rules instead use array laravel allows both ways but if you use array all the time even if for one rule then later it's easier to add custom rule and what is a custom rule i've opened laravel validation for that if you want to create your own custom validation rule for example with your own rule of what passes or fails then you can add that rule easily if you use array syntax for validation rules if you use separator then you cannot easily add that and in general array syntax is readable just because of those spaces that's my personal opinion so this is not that readable because you need to visually see the separator and if there are four or five rules then it's even harder if it's an array then the rules are separated and visible and readable and the last topic to discuss is controllers and the naming of the things so in here i don't necessarily agree with spotty guys in their opinion it's plural resource name if the controller manages the resource so resource controller and it should be named posts controller but in my video in my popular video on how to name various things i've shown that by default in laravel if you create make model book for example then the controller is automatically created with singular book controller so if it is done by default in laravel i'm not sure it makes sense to override that default condition and make the controller differently from default laravel but that's a personal preference both should work and generally throughout those guidelines you can find the naming things so camel case for views for example or artisan commands should be dash here and it is a preference of their team of spitey team but there are things that are kind of standards for the whole market so if you go to the very top about laravel there are psr standards so i totally recommend to read those psr 1 2 and 12 and configure your id whether it's php storm or vs code to automatically format the code according to those guidelines and if you want more information on how to name things in laravel the video is in the top left corner now i will link that my own video that i have just shown a minute ago and if you want to support me to shoot more videos like this one discussing my own best practices and from the team likes party you can support the channel financially by checking one of the three products you can see on the screen now and see you guys in other videos
Info
Channel: Laravel Daily
Views: 13,242
Rating: undefined out of 5
Keywords:
Id: woW00J0oEqw
Channel Id: undefined
Length: 8min 20sec (500 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.