3 ms·
I teach a JS coding class and I had the luxury of creating the curriculum. To the horror of my peers, I decided that coding with loops was not necessary for beg
by yjhoney 7y ago
I teach a JS coding class and I had the luxury of creating the curriculum. To the horror of my peers, I decided that coding with loops was not necessary for beginners and got rid of loops altogether from our curriculum. I thought OP was proposing the same thing, but I was wrong. the refactoring was good, but my students will probably end up with the following result (more in line with your no loops title):
```
function highlightProducts(aListOfProducts, i=0){
if (i === aListOfProducts.length) return null
if(productShouldBeHighlighted(product)){
product.highlighted = true
}
return highlightProducts(aListOfProducts, i + 1)
}
```
The added benefit of the above approach is that debugging is easier! If you want to debug the last 5 items, you can simply call the function like this:
highlightProducts(aListOfProducts, aListOfProducts.length – 5)
Ultimately, though, everyone should be using higher order functions (map, reduce filter, find, etc.)
- helloiloveyou 7y agoHey! you are the one from the blog comment :) It must be pretty fun to create those curriculums!! I agree with you that the restriction of not using loops can also direct you to apply recursion. However do you believe recursion makes it easier to debug certain parts of the array when you can adjust the starting and stoping points in the guard of the for loop?
- yjhoney 7y agoUltimately, everyone should use higher order functions. In your case (as other commenters have said), map or forEach should have been used. I don't really want to make a statement that recursion makes debugging easier because I don't code like that at work. I use utility functions (map, reduce, filter, etc.)
- BalinKing 7y agoThis seems a bit worrying to me as a general principle, since Javascript's lack of tail call optimization (TCO) means that you can blow the stack pretty easily by manually recursing. Just my two cents :-)
- BalinKing 7y ago(Out of curiosity, have you considered starting at raw recursion – like in your example – but then moving on to using utility functions like `map` from e.g. Ramda or lodash?)
- yjhoney 7y agoYes. curriculum starts off with recursion, then moves on to higher order functions and nobody touches recursion again.
- yjhoney 7y agoeventually everyone defaults to using forEach, every, map, reduce, filter, etc. Nobody writes recursion in codebases.
- jtgi 7y agothought you were going to say you teach functional basics like map, filter, reduce, etc. converting between recursive/iterative is a good exercise but teaching one and not the other seems unnecessary, especially considering you see more loops in the wild. --- also fwiw can just ``` highlightProducts(aListOfProducts.splice(-5)) ```
- taneq 7y agoOur first year coding lecturer did similar, but instead of teaching us Java (which is what most other classes used at the time) he insisted we use Gofer. From the point of view of a student who was excited to finally learn "real" programming, having this toy language crammed down our throats instead, along with a side-serving of smugly academic FP-superiority, was enough to sour me on FP for a long time. Hopefully you gave your students this more as "here's a neat trick to add to your toolbox" and not "listen up plebs, iteration is for losers."