Most active commenters
  • nixpulvis(4)

←back to thread

498 points azhenley | 24 comments | | HN request time: 0.555s | source | bottom
1. nixpulvis ◴[] No.45771961[source]
I try to keep deeper mutation to where it belongs, but I'll admit to shadowing variables pretty often.

If I have a `result` and I need to post-process it. I'm generally much happier doing `result = result.process()` rather than having something like `preresult`. Works nicely in cases where you end up moving it into a condition, or commenting it out to test an assumption while developing. If there's an obvious name for the intermediate result, I'll give it that, but I'm not over here naming things `result_without_processing`. You can read the code.

replies(5): >>45772035 #>>45772551 #>>45773468 #>>45775029 #>>45776914 #
2. furyofantares ◴[] No.45772035[source]
You're using really generic terms which I have to think is mostly because you're talking about it in the abstract. In most scenarios I find there are obvious non-generic names I can use for each step of a calculation.
replies(2): >>45772164 #>>45773979 #
3. nixpulvis ◴[] No.45772164[source]
I mean, I use `result` in a function named `generate` within a class `JSON < Generator`. Stuff like this is pretty common.
replies(2): >>45772296 #>>45775018 #
4. philipov ◴[] No.45772296{3}[source]
if you're already committing to generic names, what's wrong with a name like `processed_result`?
replies(3): >>45772326 #>>45772349 #>>45772510 #
5. snarfy ◴[] No.45772326{4}[source]
In the flow he describes you end up with processed_processed_processed_result.
replies(1): >>45772628 #
6. throwway120385 ◴[] No.45772349{4}[source]
I think what they're getting at is that they sometimes use composition of functions in places where other people might call the underlying functions as one procedure and have intermediate results.

At the end of the day, you're all saying different ways of keeping track of the intermediate results. Composition just has you drop the intermediate results when they're no longer relevant. And you can decompose if you want the intermediates.

7. catlifeonmars ◴[] No.45772510{4}[source]
That name is kind of redundant, since `result` implies `processed` in the first place.
8. nailer ◴[] No.45772551[source]
result.process()

That doesn’t make logical sense. You already have a result. It shouldn't need processing to be a result.

replies(1): >>45773269 #
9. WhyNotHugo ◴[] No.45772628{5}[source]
Java mentioned!
replies(1): >>45774508 #
10. riskable ◴[] No.45773269[source]
It also doesn't make sense for `process()` to be an attribute of `result`. Why would you instantiate a class and call it result‽
replies(2): >>45773534 #>>45775586 #
11. jimbokun ◴[] No.45773468[source]
Yes, but there are often FP tricks and conveniences that make this unnecessary.

Like chaining or composing function calls.

result = x |> foo |> bar |> baz (-> x foo bar baz)

Or map and reduce for iterating over collections.

Etc.

replies(1): >>45773924 #
12. Bayko ◴[] No.45773534{3}[source]
A more common example for me at work is getting a response from url. Then you gotta process it further like response.json() or response.header or response.text etc etc. and then again select the necessary array index or doc value from it. Giving a name like pre_result or result_json etc etc would just become cumbersome.
replies(1): >>45773943 #
13. nixpulvis ◴[] No.45773924[source]
Yea, very true. Not every language makes this nice though.
14. nixpulvis ◴[] No.45773943{4}[source]
I would never do `response = response.json()`. I use it when it's effectively the same type, but with further processing which may be optional.
replies(1): >>45775077 #
15. eugenekolo ◴[] No.45773979[source]
I disagree you'd find "obvious" non-generic names easily. After all, "naming" is one of the hardest things in computer science.
16. strbean ◴[] No.45774508{6}[source]
AbstractFactoryResultFactoryProcessedResultProcessedResultProcessorBeanFactory
replies(1): >>45778051 #
17. MetaWhirledPeas ◴[] No.45775018{3}[source]
> Stuff like this is pretty common.

Common != Good

18. MetaWhirledPeas ◴[] No.45775029[source]
> result.process()

What result? What process?

...says every person who has to read your code later.

replies(1): >>45776019 #
19. nomel ◴[] No.45775077{5}[source]
Depends on how clear it is.

I usually write code to help local debug-ability (which seems rare). For example, this allows one to trivially set a conditional breakpoint and look into the full response:

    response = get_response()
    response = response.json()
The fact that the first response is immediately overwritten proves to the reader it's not important/never used, so they can forget about it, where a temp variable would add cognitive load since it might be used later.

and I think is just as clear as this:

    response = get_response().json()

This motivated by years of watching people through code, and me working with deeply non-software engineers, and is always very much appreciated.
replies(1): >>45785592 #
20. feoren ◴[] No.45775586{3}[source]
> Why would you instantiate a class and call it result‽

Are you suggesting that the results of calculations should always be some sort of primitive value? It's not clear what you're getting hung up on here.

21. b_e_n_t_o_n ◴[] No.45776019[source]
I mean, it's probably pretty clear when you look at result's initial assignment...
22. swid ◴[] No.45776914[source]
I'm going to ignore the actual names used here - you can use any name you want. I think this pattern is vulnerable to introducing bugs that allow security bugs. I'm imagining process being some kind of sanitization or validation. Then, you have this thing called result, and some of the time it might be "safe" or processed, and sometimes not. Sometimes people will process it more or less than once with real consequences.

So yeah, definitely it is much better to name the first one in a way that makes it more clear it hasn't been processed yet.

23. codr7 ◴[] No.45778051{7}[source]
...BeanFactoryContextConfig

First you configure a context, then you can use that to get a bean factory and start processing your whatevers.

24. nailer ◴[] No.45785592{6}[source]

    get_response().json() 
is ideal, and I'm assuming yoiu're writing an HTTP wrapper since decoding JSON is a sensible default.

If you need to add an intermediary variable, name it as clearly as possible:

    raw_response = get_response()
    response = raw_response.json()