So many ways to do the same thing…

Among other things, one of the most challenging aspects of learning programming is that there are several ways to achieve the same result.

For example, I was writing a simple code in React to pick out even numbers out of an array, and then square the numbers. Simple enough…

In the above code, I created two variables (const): evenNumbers and squared

Constant evenNumbers will contain an array of even numbers: [2, 4, 6, 8]. And squared will contain squared evenNumbers: [4, 8, 12, 16]. And then following will be rendered:

In another code, I assigned a function to variable squared, and then I executed the function during render. With this code, variable evenNumbers will contain an array of even numbers [2, 4, 6, 8], same as above, but the squared is a function. And this function is invoked during rendering the component, achieving same results:

Which is better? I honestly don’t know the answer. But the point is that there are several ways to write a code to achieve the same result, and it seems there is always a better way. During my coding bootcamp, one of my instructors told us that getting it working is 90% of your work done, and I take comfort in the knowledge that at least I can complete 90% of my work done.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store