YouTubevideo· Aug 30, 2024· 14:52
whether you're a solo developer or you're a developer within a team are there benefits to having clean code does it make it more maintainable what does it even look like is it just throwing it in the bath and you know scrubbing it down how do you know when you've written clean
code I've recorded this episode ahead of time since I'm probably traveling back from LaRon right now and hopefully there's been some fantastic information and video that have been put out since then but today we're going to be reading benefits of writing clean maintainable code by Jamie Peters you can find Jam's
website j- p.c. and then this article is on the jump 24. co.uk site so benefits of writing clean maintainable code Jamie Peters I have been writing code down for close to 20 years but for the vast majority of that I never really paid much attention to how my code looks how maintainable it was long term I was only really interested in it if it worked and did what it was supposed to but it was full of bad practices and I had a lot of
bad habits a few years ago when I discovered the laravel framework and started really getting into it I started taking pride in the code I wrote actually thinking about the code how it looks and how easy it is for someone else to maintain and being proud of the code I write I always try to think of that Infamous developer quote always
code as if the person who ends up maintaining your code will be violent psychopath who knows where you live some of the techniques I try to remember when writing code is primarily to not just write code for a computer to understand this is especially important for teams you want the next person to pick up your code to know instantly what that code is doing and remember there's no character limit if your variable name is 20 characters long but accurately described
then what does it matter if another person can look at a block of code and work out what it's doing then you know you've got great clean and maintainable code why write cleaner code so why should you write cleaner code even if you're just code you're writing for yourself with a few reasons one Higher
code quality this is an obvious one if you sat and thought about your code and keeping it as clean as possible then automatically that code is a higher quality than it would have been had you just written the shortest amount of code to get the job done two more readable
again another obvious side effect of writing clean code is that your code will be far more readable and easier for a human to understand which means it's easier for the next person to start working in the same code base three easier to maintain if your code is now cleaner
easier to read then it will be easier to understand what that code is doing and then it is far easier to maintain with less risks involved with not understanding any side effects of editing a specific block self-documenting remember the days where it was encouraged to write huge dock blocks in your code describing what it does if your code is readable and easy to follow then you don't need them your code has become self-documenting easier
to test this is a bit more advanced but if your code is well structured and sticks to Common conventions such as separation of concerns and single responsibility then that code is then far easier to test in isolation giving even more peace of mind about that block of code lastly easier for new developers to
understand bringing new developers into an existing established codebase can be but if that code base sticks to Common conventions and the code is clean high quality and easy to read SL understand then it will be a lot easier and quicker for that new developer to get started which means they will spend less time getting on board and more time working on your product simple techniques for
writing cleaner code so now we've covered why you should write clean code the next step is to look at some simple techniques you can start using to try and help you make the change to start writing cleaner code stick to standards conventions and any guidelines in your team this is a simple one stick to standards such as psr2 and stick to Common conventions such as the standard file structure in a Layel app they exist for a reason and the more you stick to them the easier it is for you and others to work in your code base there's some tools such as larvo pint phpcs fixer
that can check and update your code so it follows PSR standards and can even be customized to follow your own team's coding guidelines use types from a personal point of view I'm Pro strict types which from writing PHP for 20 years isn't something that crosses most people's minds but they make code so much easier to read write and maintain and wherever possible every class property every method parameter and Method return type is fully and strictly typed and coupled with lar Stan PHP Stan and generic types
it just makes code so much more readable and clean and for me more enjoyable to write use descriptive names for variables and methods this one is key for me and one of the best things you can start doing to improve your code quality and also directly leads into self-documenting code mentioned above and is a very simple quick change you can make to any code you write take the following example on the left is some
code I might have wrote in the past you might look at it in a few weeks or months and wonder what dollar sign U or dollar sign p is especially if there's quite a chunk of logic in the middle compared to the code on the right which is a lot more readable and understandable so the left is dollar send you user find1 two3 dollar sign P equals new plan file true dollar sign U start dollar sign P whereas the right dollar sign user equals user find by ID
one two three plan new plan trial true user start plan so uh user instead of dollar sign U and plan instead of dollar sign P space in another key one for me and one that jumps out for me in code reviews please give your code room to breathe put a line break between blocks of code just like a new paragraph in a book or an article it makes things so much easier to read again using the following example from a line break
before and after the if block the code hasn't changed but notice how much more readable the second example is turn early no else blocks start negative now you've started using descriptive names for your variables methods and giv your code a bit of breathing room try to slightly rethink how you look at conditional block of code it's three different techniques but they all work well together first return early if
you're in a function and you can return early because the next block of code is skipped over or doesn't matter then do it this will often go hand inand with avoiding else blocks it's a tricky one to get your head around first but if you're returning from your truthy block in your if statement there's no need for the else block because you know if your code execution reached that block then your conditional was falsy and Fin finally start negative do the falsy checks first any verification and validation and then at the end of the method you know everything checks out in
the first example that we see here we first check that the user authored the post if they did and the post is popular they can't delete it but if it isn't popular they can delete it otherwise if the user is an admin they can delete it no matter what in the refactor uh also
with additional breathing room as mentioned above we first check that the user is an admin then just return true we don't care about any other logic next if the user didn't author the post then return false they can never delete that post we don't care about any other checks but if the user did author that post then finally just return whether the post is not popular leading to the same logic as the first example but over less number of lines but more readable
and less complex avoid single use temporary variables there's little point in assigning a variable that's only ever going to be used once even if it's welln named just adds the mental capacity needed to read and understand that code so avoid them where possible any good ide will highlight any single use variables to you and suggest to remove
them indentation to a minimum if you've got a lot of nested if blocks or nested Loops then your code can start to get very messy especially with the indentation level so it's one to look out for I once saw it described as watching out for the Flying V duck formation I try not to go above two or three levels of indentation if I can avoid it it's not always possible but it's a good rule of thumb to have otherwise your code might end up looking like this like a Flying V as you can see
even without any actual logic it just looks a mess what brace is closing which block considered going through the techniques discussed above early returns no else blocks maybe extract some of the more complex logic into its own method to make this method so much more readable and easy to maintain summary in
this blog I've covered why you should write clean maintainable easy to read code along with the benefits it brings and some very simple examples of changes you could make to start making your code better there are lots more that I haven't covered such as avoiding magic numbers or strings hint use e nums and constants extracting complex logic or conditionals into its own method and employing the techniques above early return Etc which also helps with the single responsibility principle mentioned above and many more one of the
questions I think comes up a lot when thinking okay I know all of these practices of how to write cleaner code or how I can write better code or how can I can make it more readable or maintainable but a lot of these practice and I think these are all great I I I agree with them and I I think I use or try to use most of these where I can a
lot of them it's really hard to to learn as a new Cod or as a new beginner or even someone who's just hasn't been building within that particular language whether that's PHP or particular framework whether that's laravel or not of course yes you do have those standards such as like psr2 or you have things like Lille pint that make things easier for you but one of the things I think makes things easier as a beginner
to learn these things and just get better is yes you have to practice so building things just makes you better at writing code mentioned it several times on learning driven development series it's like the more you build something even there's projects and you simple applications for demos that I've built five six times before I even make the video and all of a sudden I can make that video cleaner I can make the code
better because it's just kind of right to my head okay I know this is what I need to do in this particular example it doesn't mean that how I process and how I GA without an application is perfect or or even well advised but the more you do it the better you get at it so I think practicing is one of them but the second thing that I would say when it takes to kind of all these techniques that we read about you how do we write cleaner code why do we actually do the things that this article talks about you the
more you use other people's packages and the more you use you conventions within a framework and the more you whether that's contributing to open source or maybe even just you following along with open source issues and repos and PRS and everything like that the better you get at them there's a bunch of packages that I use let's say filament for example where I understand why types are so important
within a PHP package and I know package development is completely different from like you writing your own code but it kind of you this idea of okay if I am using someone else's code and if someone else is going to be my using my code in the future whether that's just kind of paining it or maybe picking up where I've left off how do I want them to to you know up and maintain this and if you use packages you're like hey this is a really a joy to use I know exactly what
arguments this accepts and I know how this should look and I think for me not only using additional packages but but like Jamie mentioned here I think I've gotten better at writing cleaner looking code at least and hopefully more maintainable code since I moved into LEL
because I have all of that kind of built into the framework where I know oh I'm not just going to create this random file in a lib directory and call that something regarding what a model should be or a migration or name your pick whether that's just directory conventions and file name conventions is one thing but it also just helps you focus on how you're actually scaffolding and building out an application I take more time to think now of okay how does
this look and how am I going if I'm going to even if it's just me and I'm going to kind of jump back into the project three plus weeks later will I be able to find what I'm doing naming convention starts with that but the more you take other people's projects the more you learn the more you you know read The More You observe the more you kind of collect information from others the better your projects start to become
and I think that's really what it comes down to as a developer as you're learning to code whether you're a developer of 10 plus years or developer of OnePlus months the better you get at just learning how to get better it's sounds a little cliche but the better you get at learning how to write code
faster and the better you get at just doing the thing of putting in repetitions of saying okay I'm just going to make this to do app and I'm going to make it the same but I'm just going to improve something different next week I'm going to do that maybe 3 four 5 weeks all a sudden you have these ideas of oh this would be a cool little feature to add oh I don't necessarily
need to replicate all this code in these five or six different files I can can add that as a component all of a sudden you start to have these ideas that start to make sense whether that's in PHP and larl or whether that's in any other programming language or framework repetition helps you make clean code you
have the practices if you're consistently kinding in information consistently reading things consistently using other people's packages consistently reading documentation you have the resources it's putting into practice so keep practicing keep building keep making keep creating \[Music\]