Question

When creating a library, must I ensure that the private methods must work as expected when called not by other methods of the same class, but by another library through reflection?

For example, if a private method private DoSomething(int number) expects that:

  • number is a positive non-zero integer, and:
  • a private variable string abc is not null and not an empty string,

and completely, ugly fails if those two conditions are not matched, must I handle those failures even if I know that all methods in the class will always¹ assign a non-empty value to abc before calling DoSomething, and pass a positive non-zero integer to this method?

In other words, is the code which is not protected against unsafe calls through reflection can be considered as low quality code, or it belongs to the caller who uses reflection to ensure that the call does not break anything?

Note: my question covers only a standard set of libraries. This does not cover code which must be highly secure (i.e. when somebody may be interested by using reflection in order to make it behave unexpectedly or crash).


¹ Because the class is correctly documented, because there are enough unit tests to be sure that any other developer will not break this method, etc.

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top