code like SD (senior | shit) Developer - the choice is yours!

This is one of those code reviews where I am not sure if I shall cry or call for more ice-creams to cool down.

Someone was tasked with a function that is adding a checkbox to the dataTable and then making an API call with selected Ids to the backend.

A trivial task we can think, but not as always.

Let see a bit of simplified frontend code, where rows selected are taken form checkboxes plugin for dataTables and that comes as object so it need to be iterated to get values and store them in new place. In this particular case an engineer decided to push it to comm separated values, the assign to model and push to an api endpoint.

Is this bad - hmm it depends - if we will use this string as a query parameter - that is fantastic (no JSON conversion needed on client and server side), but pushing it to string as an model is a no, no.

Why?

1. performance overhead on client and server

2. native form change (array of integers to string)   - that need to be redone on an api side

3. now see the object where that was passed:



As this object has noting to do on client side, then it will have a small side effect on server side. We will see this in a minute.

So what is performance overhead in the client code? https://jsbench.me/ provides a very simple and easy benchmark harness that allows for a quick performance checks.

Unfortunately their dark theme sucks as a hell

With 10 ids we are having  15% less compute (in this case payload on wire will be similar).

Now the let's the fun begin on the api side.

I created object called CutModel where only an array of ints or string line is provided as a property.
I need to admit that I was in shock when I found the results.

So the obscured model processing code looks like this:

As we expected the payload size difference was something that we can easily ignore, but memory footprint and timing is something that we CAN'T.
Pls see screenshot below and get your answer about being as "S" developer - but please chose wisely.




Someone could ask: "why that noise"? - and yes it is working, so what do you want.

  1. We are wasting user time in a processing overhead
  2. The transform array to string and back adds extra lines of code to maintain that means a longer time is needed to understand it (yeah know 3 seconds :-P) but we are reading code 20 times before we add another line
  3. Api as a cloud service consumes precious CPU ticks so in fact our $$
  4. If we can reduce compute in an easy way - then we also emit less heat and consume less energy (and all the eco shit here). 

Comments

Popular posts from this blog

when a new guy joins a team...

FizzBuzz - my first interview task on whiteboard

are YOU a garbage collector?