I prefer to use long identifiers so that my code is semantically understandable, but in the case of repeated references to the same identifier, I would like it to "get out of the way" in the current area. Take this example in Python:
def define_many_mappings_1(self): self.define_bidirectional_parameter_mapping("status", "current_status") self.define_bidirectional_parameter_mapping("id", "unique_id") self.define_bidirectional_parameter_mapping("location", "coordinates")
Suppose I really want to stick with this long method name and that these arguments will always be hardcoded. Implementation 1 seems to be wrong, because most of each line is busy repeating characters. Lines are also quite long in the general case and can exceed 80 characters when nested inside a class definition and / or try / except block, which leads to ugly line portability. Try using the for loop:
def define_many_mappings_2(self): mappings = [("status", "current_status"), ("id", "unique_id"), ("location", "coordinates")] for mapping in mappings: self.define_parameter_mapping(*mapping)
I am going to combine all such iterative methods under the auspices of implementation 2, which improves the separation of "unique" arguments on behalf of the "reuse" method. However, I do not like that this leads to the placement of arguments before the method to which they are passed, which confuses. I would prefer to keep the syntax "verb followed by a direct object."
I found myself as a compromise as follows:
def define_many_mappings_3(self): d = self.define_bidirectional_parameter_mapping d("status", "current_status") d("id", "unique_id") d("location", "coordinates")
In implementation 3, the long method is smoothed out by the extremely short “abbreviation” variable. I like this approach because it is immediately recognizable as a collection of repeated method calls at a glance, with fewer redundant characters and much shorter strings. The disadvantage is the use of an extremely short and semantically fuzzy identifier "d".
What is the most readable solution? Is the use of an “abbreviation variable” acceptable if it is explicitly assigned from an abbreviated version in a local area?