2

Firstly I am new to Software Engineering and my last question was closed. I am doing my best to ask relevant questions and improve. If you are going to down vote my question I'd really appreciate if you explain why you are downvoting me so I can learn.

Background:

I am developing a new Python API that deals with vehicles. A user can request a background check by POST a registration to a vehicles endpoint. What is returned is a defined vehicle object with things including engine size, colour, number of previous owners and whether it was in a crash.

My program makes calls to various different third party APIs depending on the manufacturer and combines the results to create the vehicle object.

Current Approach

At the moment the code is laid out so the request will go through the following stages.

If the registration belonged to a car it would be:

-> view.py
-> vehicle.py 
   -> car.py
       -> ford.py
           -> ford_api.py

If the registration belonged to a van it would be:

-> view.py
-> vehicle.py 
   -> van.py
       -> toyota.py
           -> toyota_api.py

Purpose of each file:

view.py -> layer to perform input validation and authentication

vehicle.py -> Calls an API to get the basic vehicle details and route it to the relevant vehicle type class e.g. car.py, van.py, bus.py

car.py -> This deals with the vehicle type specific API call. For example buses and commercial vehicles have a different endpoint to call compared to cars.

ford.py -> Will get the relevant manufacture specific details.

ford-api.py -> Actually does the calls connecting to fords api.

Obviously there is a lot more going on in each file but I'm looking to get feedback on the general approach.

My Questions

I have two concerns and I am not sure if they are founded or not.

(1) I'm worried the program might have too many layers. The view.py calls car.py which calls ford.py which calls ford_api.py

I am wondering if it is better to have one file call car.py get the response then call ford.py and then call ford_api.py but that results in a lot of if elif elif elif code.

Should I be concerned about having such a layered structured?

(2) I am trying to work out the most efficient way of getting the data retrieved from the API back to the caller. I am considering creating a python class that represents the vehicle check.

class VehicleCheck:
    def __init__():
        self.registration = None
        self.manufacturer = None
        etc

in the vehicle.py layer and pass it down. The methods that are called with update the VehicleCheck instance and not return anything, apart from the default None. So at the end of the check, when it gets back to vehicle.py that VehicleCheck object will have all the relevant information filled.

I know a method should only either update an object OR return something never both. I also know updating class instance attributes is fair game in Python the idea of getters and setters isn't really relevant.

Is this approach considered bad? Is it unusual? I have never seen this approach taken before but I am wondering why.

5

My suggestion here is that you should consider inverting your dependency (dependency inversion principle). Rather than vehicle knowing about car types that knows about car manufacturer, you should instead make it so that the dependency goes the other way around, so Toyota knows Cars who knows Vehicles. You can do this by building a service registry so that your view can directly lookup the Toyota class based on the input.

Why do this? Imagine that you have to add new manufacturer or a new API. Within the current system, you'll have to modify the view, the vehicles, the car/van/bus, and finally create a new manufacturer. This is logically just change, but it impacts multiple unrelated files, which is an undesirable coupling.

Instead if you reverse the dependency, you can just add new manufacturer say, Hyundai, which registers itself so it can be discovered by Car, and if you want to add a new vehicle type, you can just add Aircraft without modifying Vehicle.

| improve this answer | |
  • 1
    Thank you for that suggestion. Looks like I have some reading to do! Do you have an opinion on updating objects -v- returning an object? – user7692855 Apr 19 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.