In this video we're going to discuss how to write clean code as a developer, so how do you write
clean code? If you enjoy this kind of content make sure to give this video a like and subscribe to
the channel for more content. Today we're going to be talking about how to write cleaner code. We're
going to cover some of the principles that you can apply to write even better code than you do
right now. The first piece of advice that i would give is to try and use the most efficient approach
now sometimes this is easier said than done there are simple situations where you may have something
like an if else statement and you could simply use something like a ternary operator or a conditional
operator. I talked about this in a different video it's really a simple way to like write one line of
code rather than writing say five lines of code so there are little things that you can do like
that to really tidy up the code you're writing. Sometimes the first solution that you come up
with is not always the best solution to solve that problem. Sometimes you can write less code but get
the exact same response or the exact same output the second piece of advice that i would give is
try to write dry code. By dry i mean the concept of do not repeat yourself. Most programmers when
they are beginning or when they're just learning to code they are more focused on trying to
solve the problem. They're not really thinking about is this the best way to solve the problem,
is this the most efficient way to solve the problem. They're just simply trying to get
something working whereas when you're learning to code for some period of time or when you become a
little bit more advanced or a little bit more senior, then you start to think about ways in
which you can do this more efficiently. The dry approach is being able to write code in a way that
you can reuse it for different scenarios a good way to do this is to make your code more modular
so that you can import that module into different places within your application. Or even within
different applications. You don't need to keep rewriting the same code over and over again
the next piece of advice i would give is to learn how to use an mvc now most advanced developers or
most experienced developers have already used mvc within their job or within the projects that they
build mvc stands for model view and controller in most cases that means using a framework
that uses a mvc approach so it allows you to structure your code in a way that is coherent
that other developers will quickly understand how things are organized they'll understand the folder
structure how things are set up so it trains you in order to write your code and to break up your
code in a way that is easy to understand that is easy to maintain and follows best
practices following on from using a mvc approach i would recommend that you try to learn a concept
like fat models then controllers so what this basically means is that most of the processing of
your data the functionality should be within the model itself and that way that your controller is
simply processing the variables that you pass into it is processing the response that goes back to
the views so that way that the code is quite lean in your controllers but within the model you have
methods that perform different functionalities and they can simply be called from your controllers.
That way your code is more organized, it is easier to understand and more easily digested by other
developers. The final piece of advice that i would give in this video is that when it comes to views
try to keep them clean try to use something like helpers to take the logic out of your views. Now
this really cleans up the front-end code that you will output to the browser so move conditional
statements out into a helper file and simply call the helpers within your views so that way you're
separating the logic from the markup. It just really cleans up the front-end code so that's
about it for this video this is my thoughts on how to write cleaner code let me know what you
think in the comments below and as always i hope you've enjoyed this video if you've enjoyed it
make sure to give it a like and subscribe to the channel and i will see you all in the next video.