-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Lint non-expressive variable names #644
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
Comments
Also bad is when in a single scope there are names that are longer than 3-5 chars but only have a levensthein distance of 1 to another name |
I dunno, I find myself doing this often when there is an conceptual thing in layers of objects of different types. E.g.
|
Perhaps only lint about |
Good point, @oli-obk, I hate it when this results in a typo that just happens to be valid name… (Also the first scientific suggestion, so we can check this with much more confidence.) @Manishearth, that's a good example, since your name is actually 'checked', which is not very expressive IMHO but nevertheless fine if you know the context in which it's used. I was thinking of suffixes for 'general container types', to be honest. |
So how do we distinguish between useful names (like expr, block), and nonuseful ones (btreemap)? It's also a common pattern to name things by exactly their type since often that variable is transient -- used only for one thing. You'll find this in clippy code often with variables named |
Great ideas you got here 👍 |
I recently thought about this again and wanted to add: A lint that explicitly warns when using very long binding names for very short lived bindings that are used multiple times. (Why the "used multiple times"? Because I want to allow code like |
@rustbot claim |
So, I've recently had to deal with code written in ▊▊▊▊▊ and for some reason the original authors of the software tended to use either one-letter variables or weirdly long names (that were not even very expressive). Now, don't get me wrong: I don't want to tell anyone how to name their bindings in Rust, I just want to suggest a few pointers.
I'm proposing a lint that suggests (very nicely) to the author to rethink the naming of bindings whose names fall in at least one of the following categories. This lint can, of course, only be a very subjective heuristic (which is basically the worst kind of lint there is), so I'll try to stay conservative.
e.g.
let people_vec = vec![]
This is helpful in dynamically typed languages, but less so in a Rust which is statically typed. One difficulty might be the usage of abbreviations for types (or the opposite, like
people_vector
which is aVec
.I think some prefixes (like
is_
for bools) are okay, though.There are more than 3 one-letter names in the same scopex
,y
,z
I can understand, but what doa
,s
,f
, andl
stand for?Done in #727.
This is 1/4 of the usual line width. I've seen many such names that seem to be written that way only to allow the developer to not add documentation/comments in good conscience, since "the code is so obvious". I don't think that is very helpful (variable names describe the implementation, but probably don't help you grasp the bigger picture).
This should probably be Allow by default. I can also imagine this being split up into multiple sub-lints (or as a separate lint group). I don't want to call this a lint for "bad naming". We should probably call it "checking for non-expressive names".
What do you think of this? Is this something you want to do? Is this too opinionated? Do you have other suggestions on how to detect non-expressive names?
Please note that I'm focussing on the naming of variables/bindings, not methods or functions. Conventions for method names (e.g. in builders with/without
set
andget
prefixes) should be discussed in another issue 😄The text was updated successfully, but these errors were encountered: