课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing

Robust code

- [Instructor] In this video, you'll become familiar with code robustness and the slightly divisive topic of solid principles in Go. We'll discuss the characteristics of code robustness and why it's important to prioritize writing readable and maintainable code. Robust code is a style of code which handles edge cases and unexpected inputs gracefully, avoiding panics, and producing meaningful errors in the case of unusable inputs. Robust code is easy to write in Go, which provides explicit error handling and makes it clear to programmers which functions could produce errors through multiple return values. There are four main characteristics of robust code. Let us consider the ones presented in this diagram going clockwise. First, robust code is easy to change and maintain. It handles inputs and uses abstractions. It's easy to change its dependencies and inner workings without necessarily making those changes visible to clients or users. Second, robust code provides a clear API. In Go, packages provide their own mini API of exported functions, methods, and types. Robust code provides clear signatures for all these exported names as well as clear client APIs. Third, robust code is easy to test. Tests are the first consumers of Go package APIs so robust code is easy to test because of its clear and well-defined APIs. It also uses abstractions to make tests set up easier. Easy to test code is more likely to have well written and maintained tests as the code changes and evolves throughout the life of the project. Finally, robust code is easy to read. Developers spend orders of magnitude more time reading code than writing it, so readability is a very important characteristic. Robust code handles edge cases and errors consistently, making it easy to read. SOLID principles were introduced by Robert C. Martin, also known as Uncle Bob, in the early 2000s. He coined the term and introduced the five principles you see on this diagram in order to encourage better design in object oriented code. There are a lot of techniques that don't translate well from OOP to Go, which is not an object-oriented language and does not support inheritance. But the core principles of SOLID can be applied to achieve robust code. The single responsibility principle encourages writing code with specialized functionality. This also echoes with the Go principle of creating small specialized custom types and packages. The interface segregation principle encourages creating small interfaces as opposed to one large and complex one. This is the same as the Go principle of creating small interfaces which can be used to expose a subset of functionality as well as provide abstractions where we need them. Finally, the dependency inversion principle encourages depending on abstractions, not concrete implementations. This fits well with the Go proverb of "Accept interfaces. Return Structs," which encourages representing dependencies as interfaces which can then be easily substituted and mocked. The last thing to remember is that engineers more often read and modify code than write new greenfield code. So it's important to consider the structure of your code as you write it and consider how easy it'll be for others and even yourself to understand and to change it in the future.

内容