| Issue |
185318
|
| Summary |
[clang-tidy][readability-identifier-length] Add the option to allow short names for short-scoped variables
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
dutkalex
|
# Motivation
When a variable is very short-lived, short names can be acceptable because the definition of the variable is located right next to all of its uses. In such cases, conciseness can be more valuable than having long descriptive names which simply paraphrase the code. In the following example, using one-letter intermediate variables (`n`, `b` and `e`) and the `x` placeholder helps the reader focus on what matters (i.e. the algorithmic structure):
```cpp
template<typename T>
auto compute_standard_deviation(std::vector<T> const& data){
auto n = data.size();
auto b = data.begin();
auto e = data.end();
auto avg = std::reduce(b, e, 0, std::plus{}) / n;
auto sum_dev_sq = std::transform_reduce(b, e, 0, std::plus{}, [=](auto x){ return pow(x - avg, 2); });
return std::sqrt(sum_dev_sq / n);
}
```
On such pieces of code, the `readability-identifier-length` check issues many warnings, leaving the programmer with three options, neither of which is satisfying:
- live with the noise generated by this check
- use longer names to silence the warning, at the cost of decreased readability and verbose code
- disable this check (this is frequently what happens in scientific software projects for example)
# Proposed solution
Whether a name is long enough is not an exact science and depends on the context, but generally speaking the longer a variable is used, the more descriptive its name should be. Introducing a user-defined threshold, in terms of distance between declaration and last use, below which a variable is allowed to have a short name would significantly decrease the "false positive" rate and overall improve the user experience.
Additionally, such a parameter would ease adoption of this check in legacy codebases: instead of enabling this check and getting thousands of warnings (each of which requires manual fixing), the check could be introduced first with a large threshold to focus on the most concerning matches first, and the threshold could then gradually be decreased over time.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs