DEV Community

Discussion on: Javascript Sock Merchant Challenge - Solution 2

 
adyngom profile image
Ady Ngom

Hey John awesome to have you back and yet coming up with other ways of enriching the discussion. I really, really like what you are proposing here.

It really boils down to having an imperative or a declarative style in solving those problems. Your solution here is kind a mix of both. For those who are used to reduce this might look declarative even though the ternary operation is leaning more towards imperative.

If those concepts are new or not clear, I can't recommend enough Tyle McGinnis article on the subject: Imperative vs Declarative Programming

Regarding performance, I will quote what I think is a real gem from the above article:

Many (if not all) declarative approaches have some sort of underlying imperative abstraction.

-- Tyler McGinnis

That should settle the performance discussion since declarative (map, reduce, filter, etc...) will always include and abstract one or more imperative layers such as a for loop. I can live with 30ms for a dataset of 10,000 items though if I have the understanding that the for loop might cut in half, it is an informed decision.

The last part that is left from my perspective would be readability. I think it is highly readable for the trained eye but might still be hard to digest for the enthused and upcoming at first glance. With time though, one might learn to grow and appreciate the details in it.

Cheers

Thread Thread
 
johnboy5358 profile image
John

Thanks Ady, Tyler's article is interesting and he also says in that same article...

"Imperative: C, C++, Java

Declarative: SQL, HTML

(Can Be) Mix: JavaScript, C#, Python"

JavaScript, as he points out, is a mixed bag and I think we should take every opportunity to use that wherever it works best. Yes, I have been having fun trying out different solutions.

But, I think, that's all from me on the sock merchant. I had a thought that I might go and learn a functional programming language like Elm or Elixir.

Thanks again for the Sock Merchant problem... it has been fun!

Thread Thread
 
adyngom profile image
Ady Ngom

You will be missed Sir John, thank you and happy coding :)