rinukkusu / spotify-dart

A dart library for interfacing with the Spotify API.
BSD 3-Clause "New" or "Revised" License
204 stars 92 forks source link

Code conventions #151

Open hayribakici opened 1 year ago

hayribakici commented 1 year ago

While browsing through the endpoint files, I stumbled upon methods with no parameters (e.g. playlist.me) that are written with get properties and actual methods with no parameters (e.g. me.savedShows()). It would be nice to iron out the inconsistancy with a set code convention rules (contributing.md) or at least a guide (e.g. an issue or wiki-page) for new developers who want to add more features.

rinukkusu commented 1 year ago

Sounds like a great idea!

hayribakici commented 1 year ago

Here are my suggestions so far. Please feel free to add/change/remove/... them:

Suggested conventions:

  1. Methods with no parameters should be written as a property, e.g.
Bar get foo => ...;

or if it is an async operation:

Future<Bar> get foo async => ...;
  1. All members of the models should be written in camelCase. If the API returns a value with_underscore, use @Jsonkey(name: 'with_underscore') to map the json entry e.g.
@Jsonkey(name: 'with_underscore')
Bar? withUnderscore;
  1. Append query parameters in a variable:
/// Gets [Foo] given [bar]
Future<Foo> getFoo(Bar bar) async {
  final query = _buildQuery({'bar': bar});
  return await _api._get('$_path?$query');
}
  1. GET request methods should have unit tests. The path for the API (e.g. v1/me/tracks) reflects the json response files in the test/data/ folder.
  2. Methods with only one line should be written with the shorthand => arrow syntax.
  3. Each endpoint method must have a documentation on what it does. It can be copied from the spotify documentation as well.
  4. Use enum instead of String for types returned by the API. See the Album class for examples.
  5. Create an enhanced enum with a String key for the API. See dart's documentation for that and the Me.TimeRange enum as an example.