As a minimal example, consider the following (in Python):
class Foo:
def __init__(self, bar):
# Assume bar is a valid at this point
self.bar = bar
def divide_by_bar(self, num):
return num / self.bar
If the attribute bar is public, my understanding is that it is valid for the client to do something like:
>>> foo = Foo(1)
>>> foo.bar = 0
>>> foo.divide_by_bar(2)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
We encountered a more complex version of this in our team and I suggested to make the attribute private, or read-only (or to consider the handling the exception). However, I got the reply that, since it is only used in one place in our code base, it is fine to leave it like that. I agreed to that, but I have noticed numerous examples of this practice in our code base and I am uncertain when should one be more strict with these cases. Should I ignore it until it is used in multiple places?