4 ms·
'Many' is the weasel word here.
by hardlianotion 9y ago
'Many' is the weasel word here.
- stephengillie 9y agoThere's an anti-pattern where a function will be declared entirely in a callback, instead of declaring the function in another location and referencing it locally. It's difficult to read and logically follow. This seems especially common in Javascript. Is it just a sign of novice?
- yen223 9y agoDeclaring the function in another location is the antipattern. Code locality is important for being able to maintain contextual information when reading code. Declaring a callback in a different location from where it's used means one extra level of indirection for no good reason. Also, not sure how this is relevant to anything.
- stephengillie 9y agoDeclaring functions in another location is the purpose of functions, yes? Otherwise we would code in a purely linear fashion. Some code locality should be sacrificed for readability, reuse, and clarity. Not to make a "forest of functions", but to define separate ideas more clearly than stuffing one thought into a parameter of another thought. It's like if I was telling you about something funny that happened when I got to work, but first I have to describe every detail of my drive to work... It's a thread on ineffective coding habits, so I thought I would piggyback with a general question. I regrettably did not pursue an undergrad CS degree, and so sometimes wonder about the gaps in my education such a degree would have filled. Thank you for your time.
- Roboprog 9y agoIf the function is large, sure, make it into it's own name thing (with as restricted a scope as you can get away with). However, many languages support callbacks/lambdas/closures/blocks as parameters to generic library code, and these dangly bits are often one or two lines. E.g. (pseudo-code) - * list.for-each : print * list.filter : is-wanted * db-connection.bracket-transaction : compound-update This allows you to use, or create your own, library code as extensions of the language to reduce boiler-plate code for visiting, filtering, resource management, etc. Have a look at something like the Ruby language to see how this is exploited to reduce A LOT of code noise.
- Roboprog 9y agoSome might also argue that "private methods", which are visible to every other method in a class, are a bad thing, rather than making a nested function within the only function / method that used said code. One of the things I used to be able to do in Pascal, that C ... Java and their ilk took away. Fortunately, most FP languages gave that back, with interest (closures).
- int_19h 9y agoC# just got true nested functions back in 7.0. Although, as you have said, it is somewhat redundant with closures, and even more so with local type inference. C++, C# and Java all let you do what effectively amounts to a local function these days.
- jhomedall 9y agoIn many cases the function passed in will not make any sense outside of the context in which it is used. For example, consider: const kittenNames = cats.flatMap(cat => cat.kittens) .map(kitten => kitten.name); In this case, what would be gained by extracting the two inline functions?
- arwhatever 9y agoI've been able to use F# at work for a few months now and I'm absolutely enamored with it. But I have not yet had any of what I call "ints, strings,and for loops-only developers," the folks who never really grasped O.O. very well at all, contribute to the code. I'll be eager see how that goes.
- Roboprog 9y agoGood point. Most of these "look how much better we did with X tool" comparisons usually have a strong sample bias in that the X team are highly motivated and talented. X probably does help them quite a bit, but perhaps not as much as indicated, especially once the thinks-in-Blub (and loves it!) lumpen-programmers get added to the project :-( I'd really like to move out of 1960s tech into 1970s tech, someday (e.g. - from Simula-67-inspired to Scheme/Smalltalk-inspired environments)
- hardlianotion 9y agoI am very enthused about functional languages like F# - don't know much about F# particularly and respect rather than love C#. I just find comparisons of this kind slightly off-putting. It's always an interesting time when you share your lovely clean concept with potential philistines - hope your project's exposure to your loop-community goes well.