Algorithms on the Frontend? [anecdote]
I recently started working full time as a developer. Started working on a project where the data was flowing left right and center, a lot of conditional rendering. I didn't have much experience with Frontend frameworks like React, I had built a few applications with React before joining this org, but nothing at a really large scale.
Now that there was a lot of data flowing, in the application, with my inexperience, I didn't use any state management library like redux, which later resulted into a component with 15-20 useStates. I decided to implement that once we release the BETA version of the application. I wanted to roll out the BETA as soon as possible, but in each review I had a few revisions. Anyway, I'm not here to tell about the challenges I faced and the mistakes I made in my first project. Let me tell you about one of the proudest moment till now.
I'll be honest, I struggle while I write code, the most optimized solutions are not very intuitive to me. I try, not to write anything beyond O(n). While working on the project I had to display some data after searching a pincode in a JSON response that I had. Luckily the data was sorted and I went on to implement binary search on that data. Supercool for a fresher right, implementing a searching algo on a JSON response, and rendering the result on the Frontend. With react, this is what I thought how the code is written, JavaScript logic on top, and the HTML on the bottom, simple. But we had a problem, I thought this is how react works, you'll never need a backend, all the logic will go above the html. Little did i know, that code is visible in the browser, I sat the whole night through Sunday to make the application serverless, so the API's fetches data and sends the data to my react Frontend logic. Which also made me wonder if implementing the binary search was a really something to be proud of, is it okay to write that logic there, or should it be somewhere in the backend, tbvh I don't know the answer to this question, this is something that I was really proud of, there are times when you look the the code that you wrote, the names you selected, the abstraction that you did, all this makes you fall in love with it. But was it the right place to write that logic, I don't know, but if it should not be written there, then the debate should be settled about weather Frontend devs should learn algorithms, but if not, I don't know if this is something only the people writing the backend logic should know, I personally think that the knowledge of algorithms helps you a lot while writing code for the Frontend too, because now with SPA's the front end is more than just HTML and CSS, and I believe that knowing some algo's helped me a lot while making the application better, for example using hashmaps instead of arrays, the time complexity reduces from O(n) to O(1) when u use hasOwnProperty
instead of includes
.
see you, back to releasing the BETA ;(
Edit : Read the part 2 here