DEV Community

Viswaprasath
Viswaprasath

Posted on

Util files are Important

Most of the developers know the importance of Util files. I would like to share my story about the Util files I have developed for my work or personal projects during last 5 years.

During development I found out I will be needing two types of Util files.

  • First is common to Language I use (can be useful for cross product)
  • Second is for the project specific Util files

Common Util Files

I started writing this util file mainly to reduce (or to maintain) the common checks we do during development.

For example in Java think of checking the String is valid of not. Valid string can be mentioned as String which has atleast one Character in it.

To Check this we will be following the below steps in Java

Approach 1

  • Check whether the String is null or not, if it is null then it cannot be valid
  • Check the String not equals to "", if its equal to "" then its not valid
  • If both cases are satisfied, i.e., not null and is not equal to "" then it is valid case.

Approach 2

  • Check whether the String is null or not, if it is null then it cannot be valid
  • Check the String is not empty using isEmpty() in built Java method
  • If both cases are satisfied, i.e., not null and is not empty then it is valid case.

Even when a single developer is able to write two approaches think of a team with 10 backend developers. It is simple to find more than 5 approaches for simple checks like this.

Benefits of Common Util files

  • We can use it with other projects. Think of writing a small part as Library and giving it to customers.
  • Standard code can be maintained across the project. We need not have different standards inside same project
  • Testing can be done easily.

Project Util Files

Like Common Util file, it is very important to have project based util file also. This is generally specific to the project and is not exported. Usually it is bundled and used only in that project.

Consider an HashMap (or Dictionary) which will have the relationship between the ID and the Search Engine names.

Example :

  • 1 -> Google
  • 2 -> Bing
  • 3 -> DuckduckGo

It is very easy if we can have this details in single file. Generally over the period of time, some people tend to write their own implementation and write the code; instead its good to make developers refer a single method which can give the ID of search engine if name is given and vice versa.

Similarly, there are lot of learning I would like to share about maintaining the constants, Loggers, Exception handler as we go on.

Thanks

Top comments (0)