|
|
|
|
|
by middayc
821 days ago
|
|
I'm not trying to change your opinion, I just want to follow my opinion when coding. :) I prefer more verbs and not too many nouns, and I think our brain is used to reading "sentences" like that. get http://example.com |parse-html |find-links |for-each { .if-external { .print-it } }
Is not that dissimilar from instruction we can understand quite well: Take a basket, look into it, pick out apples, if the apple is red take it.
If there are complex procedures, I also want to name a temporary result. It's like a divide and conquer strategy. You divide complex expression or instructions to separate definitions and combine those. But I also don't want to divide too much. This on the other hand again obscures IMO.If I name every internal state, we get this. hmtlStr = get("http://example.com)
html = parse(htmlStr)
links = findLinks(html)
for l in links {
take = isExternal(l)
if take {
print(l)
}
}
Which I can parse, but it takes more energy to validate that I used the right variable at the right place, not some variable from higher up in the code for example. You probably also don't code like this, but there is some middle ground ...Just as a thought experiment, don't take me too seriously I will try to "humanize" those instructions: Take a basket in your hands.
Look into the basket in your hands.
Find apples in looked basket in your hands.
For every apple.
Check if the apple is red.
If it's red, take the apple.
Maybe you can make more balanced version which will be the best of both worlds. Thanks for the discussion :). |
|
It's funny, because I'm watching my experience as I'm reading the code and I feel like variables are like a little break while doing work. When you don't use them, it feels like a constant run.
Also, they give some extra structure, because they name the output of the function. It's true that you have to make extra effort to think of the name, though.
I'll be honest, your example with naming every internal state feels comfortable. Sometimes I do chain a few expressions together, but I avoid chains that are too long. I want to say that more than 3 is too much, but maybe it depends on the specific code.
I don't have examples of my own code handy, so I can see how I did it in the past. Now that I think about it a bit more, I think I'm okay with a longer chain, if it involves transformations of the same structure, but not if it involves destructuring. I perceive the `for` loop as a destructuring.