Chapter 6. Methods
This chapter discusses several aspects of method design: how to treat parameters and return values, how to design method signatures, and how to document methods. Much of the material in this chapter applies to constructors as well as to methods. Like Chapter 5, this chapter focuses on usability, robustness, and flexibility.
Item 23: Check parameters for validity
Most methods and constructors have some restrictions on what values may be passed into their parameters. For example, it is not uncommon that index values must be nonnegative and object references must be non-null. You should clearly document all such restrictions and enforce them with checks at the beginning of the method body.
This is a special case of the general principle, and you should attempt to detect errors as soon as possible after they occur. Failing to do so makes it less likely that an error will be detected and makes it harder to determine the source of an error once it has been detected.
本章探讨了方法设计的多个方面,包括参数处理、返回值设计和文档编写。强调了尽早检测错误的重要性,通过在方法体开始处验证参数的有效性来确保程序的健壮性和灵活性。
3006

被折叠的 条评论
为什么被折叠?



