Do routing files need to always have methods from a utils file?

Question

Do routing files need to always have methods from a utils file?

Answer

They do not need to have functions or methods from other files like utils.js, but it is good practice to create a file for certain methods that can be reused not just in one but many routing files. for example, in our refactoring expression routes lesson we are importing several methods from the utils.js file, like getElementById, we wouldn’t have that method only in expressions because as we will find out, it is also used in animals, since both sources of data are indexed (that are able to be filed and requested through an identifiable index number, like in SQL).
Let’s look at the method:

const getElementById = (id, elementList) => {
  return elementList.find((element) => {
    return element.id === Number(id);
  });
};

As we can see we would be giving ourselves extra work if we were to create the method for each route we have, because how it has been written (and its purpose) it is able to be subject agnostic, aka it can run independently of any specific data as long as it receives the arguments it needs, therefore if we create and store generic methods like that in a separate file we will only need to import them when necessary and we will be saving time for us and having cleaner files.

So, we do not need to always have methods in our routing files from an external file, but we should strive to do so.

5 Likes