1

I am currently having an issue concerning best practices where I essentially am trying to figure out the best method for exchanging instantiation settings to an arbitrary function.

At the moment, I use a setup function responsible for constructing multiple Camera objects (the specifics details of the function are not of huge concern).

def setup(self, cameras: dict):
    ...

This particular dictionary takes the form of...

{
     "hwid_1": {
       "type": integer,
       "fov": integer,
       ...
     },
     "hwid_2": {
       "type": integer
       ...
     },
     "hwid_3": {
       ...
     }
 }

Where each hwid corresponds to a separate physical camera and setup is responsible for instantiating a new Camera for each respective id.


Currently, setup() is also responsible for validating the schema of the input dictionary using JSON Schema and works exactly as expected.

The problem I am having with this approach is that the layer of abstraction introduced makes it difficult for other developers to know the format of the cameras argument without first consulting the relevant schema.

Are there any other approaches I can take to help make the schema of the input dictionary more explicit or is this an inherent limitation of attempting to instantiate an array of objects with different attributes?

  • The most common way to make your schema clear is to document it properly. It is also a commonly seen use case for a validator to make an API idiot proof. But in the end you will notice two things: idiots have much more stupid ideas than you can ever check for and it takes less time and gives better results to educate developers to code responsibly instead of putting barriers in place to force them into doing the "right" thing. – Klaus D. Dec 04 '18 at 09:11
  • Thanks @KlausD. I was wondering whether you had any thoughts on wrapping each set of attributes settings in a sort of `CameraParameter` class which would then be much more explicit as to what the `setup()` function is expecting. Completely understand your logical about over engineering the API, I keep having this feeling that the inherent design around passing nested dictionaries is somehow sub-optimal. – smooth operator Dec 04 '18 at 09:19

0 Answers0