Argument order should be consistent in your API

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the functions category.

Last Updated: 2024-11-23

In my midi to mp3 conversion project, very many functions acted on the MidiFile itself, therefore required this as a parameter, along with something else specific to that particular function (e.g. bpm, track_number). Unfortuantely, my method signatures sometimes had the midi file argument first and sometimes they had it last:

def set_bpm(bpm, midi_data):
  ...

def extract_channel(midi_data, channel_number):
  ...

This lead to an inconsistent and difficult to use API.

I should have established a convention upfront and stuck to it. In this case, putting the midi_file - or the part that was needed in many contexts - first, makes sense, since it makes it easier to curry the function and reduce the arrity.