- Hands-On Dependency Injection in Go
- Corey Scott
- 274字
- 2021-06-10 19:17:51
How do they expect to use it?
While the ATM example was clear, it was a system, and so you may be wondering how that could possibly apply to low-level concepts, such as functions. Let's look at an example:
// PetFetcher searches the data store for pets whose name matches
// the search string.
// Limit is optional (default is 100). Offset is optional (default 0).
// sortBy is optional (default name). sortAscending is optional
func PetFetcher(search string, limit int, offset int, sortBy string, sortAscending bool) []Pet {
return []Pet{}
}
That probably looks OK, right? The problem is that most of the usage looks like the following:
results := PetFetcher("Fido", 0, 0, "", true)
As you can see, most of the time we don't need all of those return values, and many of the inputs are ignored.
The first step to addressing this sort of situation is to look at the under-used parts of the code and ask yourself, do we really need them? If they exist only for testing, then it means they are test-induced damage, which we will look at later in this chapter.
If they exist for some infrequently used but compelling use case, then we can address it another way. The first option would be to split the function into multiple parts; this would allow users to adopt only the complexity they need. The second option is to merge the configuration into an object, allowing users to ignore the parts they don't use.
In both approaches, we are providing reasonable defaults, reducing the mental burden of the function by allowing users to only worry about what they need.
- 編程卓越之道(卷3):軟件工程化
- 零基礎(chǔ)玩轉(zhuǎn)區(qū)塊鏈
- Reactive Programming with Swift
- Java Web及其框架技術(shù)
- MATLAB 2020 從入門到精通
- C/C++常用算法手冊(第3版)
- Python程序設(shè)計案例教程
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- 詳解MATLAB圖形繪制技術(shù)
- Scala for Machine Learning(Second Edition)
- R語言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- SQL Server 入門很輕松(微課超值版)
- Java EE 7 with GlassFish 4 Application Server
- WildFly Cookbook
- 進(jìn)入IT企業(yè)必讀的324個Java面試題