question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid types in variable name

See original GitHub issue

Rule request

Thesis

Forbid using built-in type name in the variable name, either as prefix or suffix. It applies to all variable definitions: function arguments, assignments, with.

# bad
username_str = 'root'
username_str = current_user()
USERNAME_STR = 'root'
str_username = 'root'

# good
username = current_user()
username: str = current_user()
stranger = 'root'
default_user = User()

The rule should be applied to built-in types only: str, int, float, set, dict, and so on. Having context = Context() is fine.

Reasoning

Type annotations are better, they can be statically checked. Names can get outdated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
orsiniumcommented, Feb 18, 2021

Good point. Let’s try to classify then

  • Abbreviated, can be safely banned: int (integer), bool (boolean), dict (dictionary).
  • Joined words, can be banned: frozenset (frozen set), bytearray (byte array).
  • Real words but rarely used, consider banning: float (hard to imagine context where it means something except floating point number), tuple (I know it is used for database entities but word “record” is a better synonym), bytes (can be something like “bytes_count”, probably), complex (same as float, what else can be complex if not numbers).
  • Real words, proceed carefully: set, list, slice, type, range.
1reaction
sobolevncommented, Feb 18, 2021

It can have other types as well, for example: module_list = ModuleList()

Read more comments on GitHub >

github_iconTop Results From Across the Web

You shouldn't name your variables after their types for the ...
A variable's name should be independent of its type. You shouldn't name your variables after their types for the same reason you wouldn't...
Read more >
Forbid assigning a variable of type any to a ... - Stack Overflow
S or T is the Any type. This indicates that any type is assignable to "any" and "any" is assignable to any other...
Read more >
RANT: Stop Indicating Type in Variable Name Please
compiler knows the type of a variable and will warn, or out and out prevent misuse of the type. Therefore the type info...
Read more >
variable-name - Rule
"ban-keywords" : disallows the use of certain TypeScript keywords as variable or parameter names. These are: any , Number , number , String...
Read more >
Keywords You Cannot Use as Variable or Constant Names
Some words are reserved by C++, and you cannot use them as variable names. These keywords have special meaning to the C++ compiler....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found