Search code examples
naming-conventionscomputer-science

Why do people put "my" in their variable names?


I have seen an incredibly large number of variables in our codebase at work that are along the lines of myCounter or myClassVariable. Why?

I don't just see this at work, either. I see this in tutorials online, github, blogs, etc. I would understand if it's just a placeholder for an example, but otherwise I can't imagine it being a standard of any kind.

Is this a holdover from some old standard or is it just a bad practice that snuck in before code reviews were common place? Was it an ancestor to people's usage of the underscore to indicate a _ClassName?


Solution

  • Starting a variable name with my indicates that it is defined by the programmer rather than a predefined variable. In a statically typed language (with explicit types) it also prevents a name clash with the type name, for instance

    Collection myCollection;
    

    A my variable name should only be used in generic examples where the variable has no other interpretation.

    It may also be worth mentioning that in the programming language Perl (since version 5 from 1994) the keyword my is used to declare a local variable, for instance

    my $message = "hello there";
    

    https://perldoc.perl.org/functions/my