-
-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Role-based names vs. Information Repetition #47
Comments
Hey, @xtofl. This is a great dive into meaningful callback/handler names. We don't currently recommend how you should name your handlers, and the A/HC/LC pattern is still applicable to functions like There's one nuance about naming handlers after their intention as opposed to after their application:
When preferring the intention, there's always a danger of that intention migrating away or growing out of proportion in time. Let's take your country's example: country.on_click(mark) Initially,
That's my argument for considering application-based names like I also think that function names alone are insufficient sometimes to describe the intention. Mainly, due to the complexity of those intentions that I've described above. That's why it's generally dangerous to derive intentions from function names alone. I'd rather trust tests, as those can be as verbose as the intention requires. |
The words 'handler', 'callback', ... are often used to add a notion of where a variable/function is going to be used. However, this is very much as useless as naming a variable after its type.
As an example, consider some library to represent countries on a geographical map or so...
And we want to use this library to e.g. draw dots on the respective countries.
In this code, the
my_click_handler
name is meaningless. When reading the main file, you have to read up to the body of the click handler function to know what should happen when a country is clicked.Imagine a more extreme example:
In both cases, the information in the variable names (my_click_handler, nominator, denominator) are mere repetitions of the parameter names of the called function. This information is already present in the function signature itself, and must not be repeated.
Rather, when the names are chosen to reflect the role the variable/function plays in the program, it becomes much more meaningful:start_time
This naming pattern happens very often for the simple reason that it seems that the role of the variable is determined by the called function (a dependency, external to our own context), and needs no thought.
However, this does not add information and does not help the reader of the code to understand it. Rather, choosing the name according to the role within the context of the own problem domain gives the reader insight in its effective meaning.
The text was updated successfully, but these errors were encountered: