I\'m finding that my code becomes more readable when I make a local reference to
ID: 647871 • Letter: I
Question
I'm finding that my code becomes more readable when I make a local reference to this/self, and/or call it something that makes sense in the context. "self" or "this" doesn't really indicate what kind of object we are dealing with, in the same way as "default" or "standard" or "master" is unclear because it's not explicit.
For example, in Python code, instead of naming the instance reference "self", you could name it something else:
class User:
def getFullName(user):
return user.firstName + " " + user.lastName
What are the arguments for naming it self, instead of something more explicit, like "user" in this case?
A similar example in JavaScript when doing a manual mixin:
function asUser(obj, name) {
var user = obj;
user.name = name;
return user;
}
I guess in JavaScript this is less controversial because there is not strong tradition of using a certain naming, as in the case of "self" in Python.
Explanation / Answer
In general I feel it might make your code less readable. Simply because inside different classes you're going to use different words to reference the instance. I refactor a lot of code for a living and a this, self or even _this is immediately scanned by me as the reference to the instance.
This may not exactly fit your requirements but in JavaScript we do have to frequently rename this! The this keyword loses context inside callbacks and refers to window / global!
Is how most JS libraries roll!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.