149 points · by udfalkso · 6y ago · 70 comments
This is _so_ relative to the background of the people doing the glancing. These days [1, 2, 3, 4].map(x => x + 12).filter(x => x % 2 == 0) is obvious at first glance. Twenty years ago most people would have begged you to rewrite it with for loops.
Right now I am in a hell of trying to figure out if the Scala codebase I'm working on is terrible or if I am just not fluent enough yet with FP and cats and related libraries. There are some points of style I'm confident are poor choices, but when it comes to other aspects that seem horribly convoluted to me... I'm still not sure if the code is written for somebody with more experience in the style, or if it's a poorly executed example of the style.
I would prefer to have small functions named after what it is accomplishing and then have 2 function calls..
something like
list_of_grades = [1 , 2, 3 4]
adjusted_list_of_grades =
apply_end_of_semester_grade_adjustment(list_of_grades)
odd_grades = remove_even_grades(adjusted_list_of_grades)
Of course that this example is very silly but understanding why a transformation is happening when you're looking an old code base that you don't have the context is easier w/ a function and docstring that explains it than a map or filter(IMO)
I was laughing at myself the other week because I was having so much trouble writing a for loop because I had gotten used to the explicitness of the functional iteration operatiors
If you really want to document it more wrap the map/filter inside a single function, assign it to an aptly named variable or add a comment above.
You also introduce a refactoring problem when you split it out in functions as you now need to consider that it may be called in other places too.
Someone else reading this code in a few years time will need to look into the definitions of each of those method calls in order to understand the code, because names can't be trusted. All the more so with instance methods, as they may access arbitrary instance state to do their work, so that non-local (to the calling point) state may influence meaning.
Single use small methods need to justify their existence to avoid being inlined; they need a smidgen more purpose than a comment, or they hurt readability long term more than they help.
Here is a code example from the front page of the official Python website [python.org]:
>>> numbers = [2, 4, 6, 8]
>>> product = 1
>>> for number in numbers:
... product = product * number
Compared to the Ruby equivalent, product = [2, 4, 6, 8].inject(:*)
this feels like unnecessary bloat that makes it harder to understand what is going on in the larger picture, i.e. the unit of code this is a part of.I actually did last night what you prefer, pulled a small lambda out of a map and named it.
Oftentimes, if I have a one or two line function that is only used in one place, I prefer to have it inline and use a named intermediate value to document its meaning.
grades_with_end_of_semester_adjustment = grades.map(x =>
code code code
code code code code code
)
odd_adjusted_grades = grades_with_end_of_semester_adjustment.filter(x =>
x % 2 != 0
)This is very true. I am running a project at work using Elixir, in a software group that writes most of its code in C. I am curious to see how our style evolves as we become more intimately familiar with functional tools like chained iterators.
But definitely the point stands, the readability of code is often a function of experience and ability on both the reader and writer's sides.
I'm not sure about it, but I have certainly seen some overengineered code using reduce().
I understand the aversion to 'reduce', since it can get quite messy, but I still prefer it to e.g. WHILE loops (note that the 'for' keyword in most languages actually implements a WHILE loop). I think a more general rule is that custom abstractions can sometimes be useful, so we shouldn't try to write everything in terms of language builtins (these days 'map', 'filter' and 'reduce' are often built-in, but we can still make our own abstractions on top if appropriate).
As a comparison, the elimination form for booleans is 'if/then/else': we could write all of our branching in terms of if/then/else, but there are common patterns that can be expressed using abstractions like boolean algebra (AND/OR/NOT/etc.).
... in 6 months when you’ve forgotten 80% of the context.
Nobody writes code like the op though (I'm pretty sure it's satire)
That and the fact most of the community started out with similar ideas on good code, so there was less bikeshedding around formatting.
In other cases, side projects with an emphasis on learning, coding for fun, practicing, or just doing something neat, the clever code can meet other subjective standards for "goodness".
I'm sure most people don't care, but that doesn't make it less true.
That said, the older I get, the more I appreciate obvious code...
I agree with you in spirt, but I would change "you" to "a new teammate" in the above sentence.
Challenge accepted. Here's how far I've got so far:
$l=[nil,nil,nil,nil]
TracePoint.trace(:call) do |tp|
$l << tp.self
$l.shift
end
class Lol < BasicObject
def initialize(f)
@f = f
end
def method_missing(m, *a)
# puts $l[0].inspect
$l[0].send(m, @f, *a)
end
end
class BasicObject
def ►(&blk)
::Lol.new(self).instance_exec(&blk)
end
end
module FooBar
def self.foo(a)
if a < 0
a.► { bar -1 }
else
a.► { bar 1 }
end
end
def self.bar(a, b)
(a*b).► { puts }
end
end
FooBar.foo(4) FooBar = Module.new
def FooBar.foo(a) a < 0 ? bar(a, -1) : bar(a, 1) end
def FooBar.bar(a, b) self.puts [a, b].reduce &:* endJokes aside, there is something fascinating about programming languages being abused like this.
Like tongue twisters and word games, ain't these basically the same thing in a programming language instead of a spoken language?
I definitely didn't know about |> def
I suppose ruby has dynamic def as well though.
|> case do
is a fun one that I actually do use often.
I guess riddles of obscurity are kinda like using overly esoteric words in conversation. Does that seem like the right parallel?
When I see something like the elixir with too many pipes, I think of Dr Seuss rhymes, but when I see obfuscated code, it's kinda like gibberish.
I find this part curious. How does Elixir know that a and foo aren't references/calls, before reaching def? Does Elixir work |> chains backwards?