Learn the importance of continuously improving the nonfunctional features of a project to add more clarity and structure, and how that makes it easier to add functional features later.
- [Instructor] Refactoring means improving the non-functional features of code. This means we won't add new functionality but simply improve the code we have to be more consistent and maintainable. Actually, developing an application is a constant process of change and adaption. We should always try to find ways to make our code better. While we are refactoring, we will also write tests for each module we refactor. This way, we can make sure that the overall behavior of our application stays consistent. So let's get started with the Iris main application.
And I open run.js here. So bin, run.js. I already want to add a little change here. Currently, we simply request service here in line six. And this is actually the express app providing the subscription route for services. I want this module to provide a function so that they can pass parameters like the configuration into it. It should work basically like that. Here I'm requiring service and I want to pass the config we required here in line three into service.
Like that. You may ask why we won't simply require config and service.js again. I found out that it's way better to require your own modules, like the configuration, only at one single place. Imagine that you want to change the way that config is passed in. Maybe it comes from some database or you want to provide a dedicated configuration for testing. If you make your modules dependent of the fact that there has to be a config file that at the specific location, you lose a lot of flexibility.
Then as soon as you change anything, you'll have to walk through your code to find all occurrences of require config to fix this. This also shows the impact of violating the don't-repeat-yourself principle we discussed in the beginning. The configuration should be required in the start script and from there passed down the module change. To make everything work, we have to change service.js now as well. So I open server, service.js. And there now, I will change everything I have here to module.exports equals and that's an yes.
Six error function config passed in. And then the fat arrow, curly brackets. Then I will copy and paste everything in here. Let's reformat that. And very important, we also have to return something from this functional and we will return service. So now service.js provides a function that can take a configuration config as parameter.
Now let's try running this and see if it still works. So I run npm start. And it still works.
- Adding debugging
- Getting started with refactoring
- Transforming slack-client into a class
- Creating a test infrastructure with Mocha, Should, and Istanbul
- Creating tests for the service registry
- Setting up continuous integration with Codeship
- Adding automated security audits with Snyk
- Adding endpoint security
- Setting up your servers for deployment