-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Lint for literal values used in expressions #57199
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
Could you add some good/bad examples? How would you phrase the advice? |
class Person {
String name;
int age;
List<String> validate() {
var errors = [];
if(name == null || name.isEmpty) {
errors.add('"name" is mandatory.');
} else if(name.length > 32) {
errors.add('The maximum name length is 32');
}
if(age == null) {
errors.add('"age" is mandatory.');
} else if(age < 18) {
errors.add('The minimum age is 18 to register for this service.');
}
return errors;
}
} should be something like class Person {
static const maxNameLength = 32;
static const minAge = 18;
static const maxNameLengthExceededMessage = 'The maximum name length is $maxNameLength';
static const minAgeBelowMinAgeMessage = 'The minimum age is $minAge to register for this service.';
static const nameMandatoryMessage = '"name" is mandatory.';
static const ageMandatoryMessage = '"age" is mandatory.';
String name;
int age;
List<String> validate() {
var errors = [];
if (name == null || name.isEmpty) {
errors.add(nameMandatoryMessage);
} else if (name.length > maxNameLength) {
errors.add(maxNameLengthExceededMessage);
}
if (age == null) {
errors.add(ageMandatoryMessage);
} else if (age < minAge) {
errors.add(minAgeBelowMinAgeMessage);
}
return errors;
}
} |
I'm not really tempted by this lint. Especially when the literal is used only once. This makes readability harder IMHO because you end up jumping to variable declarations all the time to really understand what is going on. Remember that one of the most difficult task in software development is to find (variable|class|member)'s names ;) |
I don't think this should be forced on users.
This would for example allow to do a quality check with this specific lint enabled where each magic number/string can be checked if it is still ok to keep the literal or create a constant instead. |
Ok, thanks for the clarification. I thought it was a general lint that would have been activated by default. Sorry for the noise ;) |
@a14n : no apologies necessary. Input is a good thing! And configurability is hot topic #57153 (and #57189). @zoechi : thanks for the clarification. It looks like you're asking for a flavor of "magic number" checking. For reference, checkstyle's MagicNumber check is defined like this:
|
Further rationale for a check of this sort over on this stack overflow thread. |
Not sure it's a flavor. I would say it's exactly that ;-) |
updated link for magic number
|
Any updates regarding this lint? |
No updates. |
literal values (String, int, double, not sure about others) should only be assigned to named constants (except 0, 1)
The text was updated successfully, but these errors were encountered: