0

I were working on my app today and when my friend looked on my code he told me that before I'm making an HTTP request to update objects I should remove the properties that are not used in my server and I didn't understand why. I didn't find any best practice or any explanation on the web why it is better to clean my objects before sending them to my server...

Let's say I have a dictionary with 100 keys & values with the same properties (but different values) like this one:

    {
        '11':{'id':11, 'name':'test1', 'station':2, 'price': 2, 'people':6, 'show':true, 'light': true},
        '12':{'id':12, 'name':'test2', 'station':4, 'price': 2, 'people: 1, 'show': true, 'light': false},
        ....
    }

The only thing I need to change is the station of each pair. The new station number is set on my client and sent to my server to make an update in my DB for each pair...

Should I iterate over the dictionary and clean every object before making an HTTP request to my server as my friend said?

ronara
  • 336
  • 11
  • 26

2 Answers2

0

I can not add a comment because of my reputation, so I'll put as an answer

Not necessarily, it depends a lot on how your server's API works, if it expects an entire object, it's no use cleaning, now if you have the option to send only the modified element, you do not have to send the entire object.

The HTTP request will work in the same way with a single piece or with an integer object, but you can shorten the data traffic in kbps by sending less, only the Required, like as the changed values

Summary, it depends a lot on your approach, working single values and not whole objects you can do more generic functions and improve their entire scope.

Check: THIS It's similar to your question.

EDIT:

Maybe the cleanup he's referring to, is the question of clearing the code and sending only the necessary, so I understood the scope of the question

Remember that the less you pass, the more intact the original object will be (on the server).

It is a good practice to create generic (modulable) functions that only work with the necessary changes.

0

Couple reasons that come to mind:

  • plan for the unknown: today, your server doesn't care about the people attribute. But imagine you add something server side and a people attribute appears and is a string. Now all your clients fail, because they try to push numbers to a string

  • save the world: data is energy, and you're wasting it by sending more data that your server can handle, even if it's just a little

  • save your own energy: sending more attributes is likely to mean more work (to write the code and/or test it)

Arnaud
  • 17,268
  • 9
  • 65
  • 83