Now which problems do we have
in this demo project?
The biggest problem can be seen here in the blog.js file
in the routes folder.
This is a huge code file with lots of code in there,
and the code that we have in there,
does pretty different things.
We got routes that are dealing with user authentication,
and we got routes that deal with blog posts.
And these are two tasks which are not directly related.
Of course, you have to be authenticated
to create blog posts,
but that's the only connection.
Other than that, these are totally different tasks.
Now, of course, this file is still
relatively easy to digest and manage,
but if you come back to this website
after three or four weeks,
it will take a couple of minutes
until you understand this file again.
And if you're looking for a specific route,
let's say the post route for editing posts,
you have to search a bit until you find it.
It's all not horrible, don't get me wrong,
but these are still things that can add up
the bigger your website gets.
And if you're working in a team,
or if you're taking pauses whilst working on a project,
it can get pretty hard to manage such code basis over time.
Now, in the views,
we actually already are using a feature called Includes,
to extract certain common HTML snippets,
and reuse them in different views.
Like the post-form,
which I'm using in both the admin view here,
I'm including it here.
And in a single post view, I'm including it here as well.
So there, we already are splitting up our bigger code files
into smaller ones, to make these code files more manageable,
and of course, to avoid unnecessary copy and pasting.
If we have the same code for the same form
in multiple views,
extracting it into such a include, simply saves us time.
If I ever want to edit this form now,
I only have to do it in one place
instead of two or more places.
So here in the templates,
we are already using some Includes.
And of course, you might be able to find more
extractable snippets in there.
I didn't want to overdo it here,
to still make it fairly easy to understand this project
and this approach.
But we are splitting our code here.
We're not splitting it in the routes file.
And the app.js file also grew quite a bit.
It's not that large, it's still okay,
but we're doing very different things in there as well.
And the more custom middleware we add and so on,
the more difficult it will get to keep that file
maintainable as well.
And these are things we want to fix as a developer.
We wanna keep our code maintainable
and not work with large code files
as we're doing it at the moment.
Instead, as a developer,
no matter if you're a web developer,
or any other kind of developer,
it's typically a good idea to split your large code files
into smaller code files.
And of course, don't split them randomly
by just taking a bunch of lines
and putting that into a separate file.
But instead split your code files by logic.
For example,
we could break up this blog.js routes file here,
by splitting it into two routes files,
one for the authentication related routes,
and one for the post related routes.
We then might be able to extract more logic
from these files, but we'll get there step-by-step
throughout this section.
Now, the process I'm describing here is called refactoring.
We have a certain code base,
and now we're going to rewrite this code a little bit.
We're going to move code around.
We're going to split code files into smaller code files.
And to a lot of newcomer developers,
this refactoring process looks very scary,
because you're moving around a lot of code.
And I know from my other course is that,
a lot of students don't like this.
Well, as it turns out,
if you're serious about becoming a developer,
you need to know how to refactor,
because you'll do it all the time. Believe me.
You're going to do it in existing projects
you're dropped into,
but you're also going to do it on your own projects,
because it's very common that you just start writing code.
Then your code files grow,
and to keep them maintainable again, you then split them up.
So it's a super important process.
And in this course section,
we're going to go through it together.