TranslateProject/sources/tech/20190108 Avoid package names like base, util, or common.md
darksun 1f10664638 选题: 20190108 Avoid package names like base, util, or common
sources/tech/20190108 Avoid package names like base, util, or common.md
2019-06-20 17:42:07 +08:00

3.8 KiB
Raw Blame History

Avoid package names like base, util, or common

Writing a good Go package starts with its name. Think of your packages name as an elevator pitch, you have to describe what it does using just one word.

A common cause of poor package names are utility packages. These are packages where helpers and utility code congeal. These packages contain an assortment of unrelated functions, as such their utility is hard to describe in terms of what the package provides. This often leads to a packages name being derived from what the package contains —utilities.

Package names like utils or helpers are commonly found in projects which have developed deep package hierarchies and want to share helper functions without introducing import loops. Extracting utility functions to new package breaks the import loop, but as the package stems from a design problem in the project, its name doesnt reflect its purpose, only its function in breaking the import cycle.

[A little] duplication is far cheaper than the wrong abstraction.

Sandy Metz

My recommendation to improve the name of utils or helpers packages is to analyse where they are imported and move the relevant functions into the calling package. Even if this results in some code duplication this is preferable to introducing an import dependency between two packages. In the case where utility functions are used in many places, prefer multiple packages, each focused on a single aspect with a correspondingly descriptive name.

Packages with names like base or common are often found when functionality common to two or more related facilities, for example common types between a client and server or a server and its mock, has been refactored into a separate package. Instead the solution is to reduce the number of packages by combining client, server, and common code into a single package named after the facility the package provides.

For example, the net/http package does not have client and server packages, instead it has client.go and server.go files, each holding their respective types. transport.go holds for the common message transport code used by both HTTP clients and servers.

Name your packages after what they provide , not what they contain.

  1. Simple profiling package moved, updated
  2. The package level logger anti pattern
  3. How to include C code in your Go package
  4. Why I think Go package management is important

via: https://dave.cheney.net/2019/01/08/avoid-package-names-like-base-util-or-common

作者:Dave Cheney 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出