Yesterday I had a mentoring session and got to walk through my GFT code with someone who knows a lot more than me…it helped a lot!
The biggest thing was helping me get closer to writing tests for the app. It gave me a lightbulb about using controllers, and now I understand why they are helpful!
Controllers are the C in the MVC design pattern. Some languages and frameworks follow a pretty strict MVC architecture so you don’t really have a choice about how to set up a project. In Express on the other hand, you can do pretty much whatever you want with the structure. One thing that can be a bit annoying about this is when there are a lot of files…you find yourself going from one folder to the next to the next looking for a specific file, because oftentimes (with good reason) the filenames tend to be the same.
Because of this, I always thought it was better to put all the routing logic in my
routes files…less clicking around to do when coding and troubleshooting.
But then when I started to think about how to write tests for the app as a whole, it’s like where should I start? Because of how it’s written with the views routers and routing logic all bundled together, I’d basically need to write integration tests to make sure everything’s covered. But since I haven’t even started working on the front end yet that doesn’t may any sense—all the tests would need to be re-written as soon as a new front end is in place. It kind of defeats the point of tests!
So it’s controllers to the rescue. When just the logic is on its own away from the route and away from the view, then you can write a test just for the logic, and that test won’t change if the routing or views change later on down the line. Which they inevitably will.
So now I have been working out on breaking the code out into these smaller pieces so that I can get these tests written!
I also got some good tips in the session about refactoring. I did a lot of unnecessary error handling, and some roundabout logic that I’ll be able to clear up while I’m separating the controllers.
Not sure if I wrote it here before but I signed up for a half marathon! So been training for that, loooooooooots of running for the next few weeks.