-3

The absolute value function is an instance of the norm function in one dimension. I've seen instances of libraries where the absolute value function is overloaded to also take the norm of higher dimensional vectors. Would it be better practice to define a new function, say "norm," or is there some reason why overloading a standardized absolute value function might have benefits.

1 Answers1

1

You shouldn't overload just because you can, excessive overloading sometimes breaks the principle of least astonishment. More often than not you just shouldn't do that as it makes harder to detect when a scalar is used instead of a vector (or the opposite) by mistake (edit: this applies mainly to regular development, if you are developing a math library then users are supposed to read the documentation so they will be well aware of the overloading).

The absolute value can be considered a particular case of norm, so it would be ok to overload the norm function to return the absolute value for scalar inputs if that makes the code significantly cleaner and the change is understood by the rest of your team. Not all norms are absolute values so other developers wouldn't know what to expect when a abs function is called on a vector, e.g. they may think it applies the abs function to each component.

ggf31416
  • 151