javascript-refactoring icon indicating copy to clipboard operation
javascript-refactoring copied to clipboard

Note on preserving context when refactoring for-loop

Open bbbrrriiiaaannn opened this issue 10 years ago • 3 comments

Moving from a for-loop to forEach can have side effects when the logic depends on context, so add an example

bbbrrriiiaaannn avatar Nov 01 '15 00:11 bbbrrriiiaaannn

I agree this is useful info but I think it should go into a new section to keep the main example concise.

KarlPurk avatar Nov 02 '15 19:11 KarlPurk

Martin Fowler has a section labeled "Additions" that seems appropriate for incidental notes like these. I didn't see a convention like that on yours, but maybe gotchas like this belong in one? That way the focus is still on the main example, kept concise, and supplemental notes are sequestered separately.

Or maybe this can be noted more briefly? "forEach takes 'this' context as a second parameter, necessary if refactored code depends on it.".

bbbrrriiiaaannn avatar Nov 03 '15 03:11 bbbrrriiiaaannn

Additions seems like a suitable title. Nice suggestion.

It would be good to keep the note brief, but I think this raises a question that I'd not considered yet - what level of experience is this repository aimed at? I'd not really thought about that before, but I want this refactoring guide to be valuable to JavaScript developers of all levels. With that in mind, it makes sense to include examples to help illustrate points like this.

KarlPurk avatar Nov 03 '15 08:11 KarlPurk