Skip to content
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

sort_constructors_first should let you put getters & setters before constructors #57664

Closed
kwalrath opened this issue Dec 18, 2017 · 4 comments
Closed
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-enhancement A request for a change that isn't a bug

Comments

@kwalrath
Copy link
Contributor

When I switched a property from an instance variable to a getter, this linter rule forced me to move the getter below the constructors. That doesn't feel right, and it's now harder to see what properties the class has. It seems like getters and setters shouldn't be treated like ordinary methods.

Before:

class Spacecraft {
  String name;
  DateTime launchDate;
  int launchYear;

  // Constructor, including syntactic sugar for assignment to members.
  Spacecraft(this.name, this.launchDate) {
    ...
    launchYear = launchDate?.year;
  }

  // Named constructor that forwards to the default one.
  Spacecraft.unlaunched(String name) : this(name, null);
  ...
}

Expected:

class Spacecraft {
  String name;
  DateTime launchDate;
  int get launchYear => launchDate?.year; // read-only non-final property

  // Constructor, with syntactic sugar for assignment to members.
  Spacecraft(this.name, this.launchDate) {
    // Initialization code goes here.
  }

  // Named constructor that forwards to the default one.
  Spacecraft.unlaunched(String name) : this(name, null);
  ...
}

Actual:

class Spacecraft {
  String name;
  DateTime launchDate;

  // Constructor, with syntactic sugar for assignment to members.
  Spacecraft(this.name, this.launchDate) {
    // Initialization code goes here.
  }

  // Named constructor that forwards to the default one.
  Spacecraft.unlaunched(String name) : this(name, null);

  int get launchYear => launchDate?.year; // read-only non-final property
  ...
}
@pq
Copy link
Member

pq commented Dec 19, 2017

@kwalrath
Copy link
Contributor Author

Maybe the issue is that the linter should make you put other variables below the constructors? (Not that I like that any better.) It just seems weird to split up the properties.

@pq
Copy link
Member

pq commented Dec 19, 2017

Agreed. Checkout the relevant conversation going on over here: dart-lang/linter#834.

@pq pq added the type-enhancement A request for a change that isn't a bug label Jun 29, 2018
@parlough
Copy link
Member

With dart-lang/linter#834 merged this rule has been made more consistent, suggesting placing all members after constructors.

@kwalrath kwalrath closed this as completed Jun 1, 2021
@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants