Back to all reviewers

Design convenient APIs

commaai/openpilot
Based on 2 comments
Python

APIs should provide convenient methods that handle common operations directly, rather than requiring users to write helper functions or use verbose patterns. This improves developer experience and reduces boilerplate code.

API Python

Reviewer Prompt

APIs should provide convenient methods that handle common operations directly, rather than requiring users to write helper functions or use verbose patterns. This improves developer experience and reduces boilerplate code.

Key principles:

  • Build common functionality like type conversion into API methods themselves
  • Provide simple, direct constructor patterns for object creation
  • Avoid forcing users to chain multiple method calls for basic operations

Example of good API design:

# Good: Simple constructor with direct parameter passing
CC = car.CarControl(cruiseControl={'cancel': True})

# Avoid: Verbose chaining that could be simplified
CC = car.CarControl.new_message(cruiseControl={'cancel': True})
test_car_controller(CC.as_reader())

When designing APIs, consider what the most common use cases will be and optimize the interface for those scenarios. If you find yourself writing helper functions or seeing users write repetitive boilerplate, that’s a signal the API could be more convenient.

2
Comments Analyzed
Python
Primary Language
API
Category

Source Discussions