Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Utils file good practice? [closed]

Tags:

java

c++

c#

I saw lots of projects that use that kind of file. It usually contains functions that are required from lots of places in the code. It seems logicality to save code from the number of classes and namespaces that contain one or two functions. But on the other hand it makes code less separated into logical parts that can make harder understanding of structure of project. So what is my question about. Is it good or bad to have such file in project?

The language I use is c++ but it seems the question refers to any programming language.

like image 496
Nolan Avatar asked Apr 24 '15 20:04

Nolan


1 Answers

Whether or not a utils file is bad practice is depending on many factors. Other factors are (and the list is not exhaustive and likely subject to opinions of different individuals):

  • Does it contain functions that logically belong together. Or are there functions in there that don't. The latter is bad practice of course.
  • Is it part of a well named namespace to make clear what the purpose of the utils is.
  • alongside with the namespace structure is the also the directory structure
  • unit tests that show how the utils are working and how the api is supposed to be used by users.
  • if you can find a better name than simply utils, by all means do.
  • these utils file can show up in many forms, service is just as good.
  • Well named classes and function names, variables , ...

Conclusion : if it is good or bad is heavily depending on the broader context and on the content of the file in question. The rules of clean code always apply. The idea of putting utility functions in separate files is common practice, but they are only useful if they are structured very well so people find there way to them, and in them easily. If this is ok, people will use them. Otherwise people will not use them, and you will still end up with many utils files that still contain a lot of duplication with slightly varying implementations.

like image 106
Philip Stuyck Avatar answered Sep 28 '22 23:09

Philip Stuyck