arguments-ordering
Ensures that the order of named arguments in function, method, and constructor invocations matches the corresponding order of named parameters.
Consistent order of named arguments helps with two main aspects: predictability and reducing merge conflicts.
For example, adding child
to the configuration of last
arguments helps ensure that in every invocation (e.g. widget creation) this argument will be the last one. Which makes every appearance of this argument more predictable. As for merge conflicts, consistent order helps when multiple people edit the same argument list. If the order is not enforced, the chances of adding/editing the same line are higher.
⚙️ Config
Set first
(default is empty) to configure the list of arguments that should be the first (example).
Set last
(default is empty) to configure the list of arguments that should be the last (example).
Set alphabetize
(default is false
) to enforce alphabetical order instead (example).
dart_code_metrics:
rules:
- arguments-ordering:
alphabetize: false
first:
- some
- name
last:
- child
- children
Example
❌ Bad:
Person buildPerson({ String name, String surname, String age }) { ... }
// LINT: Named arguments do not match the parameters declaration order. Try sorting them.
final person = buildPerson(age: 42, surname: 'The Pooh', name: 'Winnie');
✅ Good:
Person buildPerson({ String name, String surname, String age }) { ... }
// Correct, all arguments match the order of parameters
final person = buildPerson(name: 'Winnie', surname: 'The Pooh', age: 42);
// Correct, 'surname' is not provided
final another = buildPerson(name: 'Winnie', age: 42);
Example with "first"
Config
dart_code_metrics:
rules:
- arguments-ordering:
first:
- child
❌ Bad:
class PersonWidget extends StatelessWidget {
PersonWidget({
required this.name,
required this.child,
});
final Widget child;
final String name;
...
}
// LINT: Named arguments do not match the parameters declaration order. Try sorting them.
final widget = PersonWidget(name: 'some', child: Container());
✅ Good:
// Correct, 'child' is first
final widget = PersonWidget(child: Container(), name: 'some');
Example with "last"
Config
dart_code_metrics:
rules:
- arguments-ordering:
last:
- child
- children
❌ Bad:
class PersonWidget extends StatelessWidget {
PersonWidget({
required this.child,
required this.name,
});
final Widget child;
final String name;
...
}
// LINT: Named arguments do not match the parameters declaration order. Try sorting them.
final widget = PersonWidget(child: Container(), name: 'some');
✅ Good:
// Correct, 'child' is last
final widget = PersonWidget(name: 'some', child: Container());
Example with "alphabetize"
Config
dart_code_metrics:
rules:
- arguments-ordering:
alphabetize: true
❌ Bad:
Person buildPerson({ String name, String surname, String age }) { ... }
// LINT: Named arguments do not match the alphabetical order. Try sorting them.
final person = buildPerson(age: 42, surname: 'The Pooh', name: 'Winnie');
✅ Good:
Person buildPerson({ String name, String surname, String age }) { ... }
// Correct, alphabetical order
final person = buildPerson(age: 42, name: '', surname: '');