-
Notifications
You must be signed in to change notification settings - Fork 56
Clean up hard coded literals. #374
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request does a great job of cleaning up hard-coded literals by introducing an enum for property names and dynamically generating enum values for the schema. The refactoring of enum parsing logic into a reusable generic function is also a significant improvement.
I have a couple of suggestions to further improve the code:
- In
simple_items.dart, the newparseEnumfunction can be made more efficient and idiomatic by using.nameon enums instead oftoString().split(). - In
column.dart, the hard-coded default spacing value8.0can be extracted into a constant to eliminate the magic number, which aligns with the goal of this PR.
|
|
||
| typedef JsonMap = Map<String, Object?>; | ||
|
|
||
| T parseEnum<T>(String? value, List<T> values, T defaultValue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
Nit: Can we have named params just so it's a bit more obvious what the parameters mean at the call sites?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Jacob about the named parameters.
Also, it's too bad we can't eliminate the List<T> values and just use T.values in the implementation. (I know we can't, it's just too bad).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for iterating. I'd still like to see the named parameters here before submitting this!
| import '../../model/catalog_item.dart'; | ||
| import '../../primitives/simple_items.dart'; | ||
|
|
||
| enum _P { mainAxisAlignment, crossAxisAlignment, children, spacing } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe constant strings are a better fit here than an enum, seeing as the constants here aren't really interchangeable instances of something, i.e. there are no specific polymorphism requirements.
E.g. maybe mainAxisAlignmentPropertyName and crossAxisAlignmentPropertyName.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, how would that look? Something like this?:
abstract base class FauxEnum {
String get name;
List<String> get values;
}
final class _P extends FauxEnum {
const _P(this.name);
String name;
static const mainAxisAlignment = _P('mainAxisAlignment');
static const crossAxisAlignment = _P('crossAxisAlignment');
static const children = _P('children');
static const spacing = _P('spacing');
List<String> get values => [
mainAxisAlignment,
crossAxisAlignment,
children,
spacing,
].map<String>((v) => v.name).toList();
}And I guess you'd have to make a base class for the class FauxEnum with the static strings that has the value accessor in order for the parseEnum function to work similarly. But maybe you could eliminate the values arg to parseEnum then, since values wouldn't have to be static?
This is a nice cleanup, but one issue I have with all of this (including the enums) is that if there is a property that ends up being a Dart keyword, then we could have issues. (e.g. static const String class = _P('class'); wouldn't work). The current implementation doesn't have that problem.
At least with the constant strings, you could still implement it, it's just that the name would be different from the value: static const String clazz = _P('class');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I propose that we keep using the enums for MainAxisAlignment, CrossAxisAlignment here. These are from Flutter and they make sense as enums, and the parseEnum function is neat and works well in my opinion. These enums aren't used to define the string "MainAxisAlignment", but rather the different alignments that are available.
For the property names e.g. "mainAxisAlignment", "crossAxisAlignment", I vote we instead just have something like:
const mainAxisAlignmentPropertyName = `mainAxisAlignment`;
final _schema = S.object(
properties: {
mainAxisAlignmentPropertyName: S.string(
description:
'How children are aligned on the main axis. '
'See Flutter\'s MainAxisAlignment for values.',
enumValues: MainAxisAlignment.values.map((e) => e.name).toList(),There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to see what @gspencergoog things too before we go and change the entire codebase. I think I'm strongly in favor of the parseEnum utility, and mildly in favor of using the constants.
|
Thanks for the comments. I will respond and then I will make sure we all have agreement here before merging (it is good that it is not urgent). So, it will wait for Jacob to be back from OOO. |
Contributes to #358
If looks good, will ask AI to update other catalog items the same way.