Should I throw on null parameters in private/internal methods?

前端 未结 7 1879
逝去的感伤
逝去的感伤 2021-02-05 02:56

I\'m writing a library that has several public classes and methods, as well as several private or internal classes and methods that the library itself uses.

In the publi

7条回答
  •  不思量自难忘°
    2021-02-05 03:27

    1. The public interface of your library deserves tight checking of preconditions, because you should expect the users of your library to make mistakes and violate the preconditions by accident. Help them understand what is going on in your library.

    2. The private methods in your library do not require such runtime checking because you call them yourself. You are in full control of what you are passing. If you want to add checks because you are afraid to mess up, then use asserts. They will catch your own mistakes, but do not impede performance during runtime.

提交回复
热议问题