While Java beans did not agree with the get / set naming convention, some of them often saw functions (especially methods in C ++ and other OO languages) that performed exactly the way you describe.
They were often called after the variable they set or received, for example:
int counter_; int counter () { return counter_; } int counter (int c) { counter_ = c; return counter_; }
In languages ββwith different namespaces for variables and functions, you can even have a variable, and get / set functions have exactly the same name (without the need to use trailing _, as I showed here).
In languages ββwith default parameters, you can write getter and setter as one function, for example. something like that:
int counter (int c = MAX_INT) { if (c != MAX_INT) { counter_ = c; } return counter_; }
... although I was not particularly keen on this approach because it led to subtle errors if someone called counter (MAX_INT) , for example.
I always thought this naming approach made sense, and I wrote several libraries that worked just that way.
However, this naming convention potentially confused the reader with the code, especially in languages ββwhere it would be possible to call a function without parameters without end brackets, so it was difficult to see if the function was called or if the public variable was accessed directly. Of course, some will call the latter as a function, not a problem ...
source share