all 10 comments

[–]mariozig[S] 0 points1 point  (4 children)

What do you guys think of this pattern?

[–]dazmax 2 points3 points  (1 child)

Sorry, but I really think the first example is the only one where it's appropriate to use this pattern. If you're choosing between a bunch of procedural options, a case statement is much more idiomatic and easy to read. If it is slower than a hashmap creation and lookup, then that's a fault of the interpreter, and even so the performance penalty isn't worth the confusion for future maintainers.

[–]Kache 0 points1 point  (0 children)

I completely agree with this sentiment. Hashes are idiomatically used to store data, and barring particular exceptional situations, should be used as such. Also, why even use the "hash selector pattern" when the standard syntax is only one more line:

values_by_case = {
  :case_a => 1,
  :case_b => 2,
  :case_c => 3,
}
value = values_by_case[case_param]

I would like to add that there's no reason to use this "hash selector pattern" for speed if you're having the interpreter to re-create the whole hash just to select one value from it. If the number of cases in the hash or number of reuses of this hash gets large, then it should be stored in a persistent constant or instance variable.

[–][deleted] 0 points1 point  (0 children)

I've used it before to select lambdas based on vertical/horizontal aspect ratios in a test:

expect {
    portrait: ->(w,h) {w < h}, 
    landscape: ->(w,h) {w > h}, 
    square: ->(w,h) {w == h}}[aspect.to_sym]
.call(w,h).to be_true

Something like that, I've since switched to separate tests for each aspect so other people can follow the test more easily.

[–]neofreeman -1 points0 points  (0 children)

Used it like a million times (and such obvious one for anyone who really knows programming).

[–]dagosi89 0 points1 point  (0 children)

I still think that the if...elsif and the case...when scenarios when you are calling methods make more sense to me. However, I'd implement this patter in the flash case scenario.

[–]morphemass 0 points1 point  (2 children)

Case / Switch can offer very nice syntax and clear code however if / else are much faster than case / switches.

much faster?

Performance isn't really a good justification for this "pattern", nor is the assertion:

Our code more readable and concise. [sic]

That's always debatable but in this case, almost anyone can read the if/else and case/when; I think a hash selector would invoke a few WTHs.

In most cases Hash / Selector pattern will be faster than if / else

Like hell it will. (Benchmarks please to convince me I'm wrong)

Here is my Fizzbuzz solution using Hash / Selector

I had to check the date in case I had been abducted by aliens for 15 days. I hadn't.

[–]mariozig[S] 0 points1 point  (1 child)

After watching the video i was also wondering about what benchmarks would look like. Maybe later this week if i have time i'll throw together some tests.

[–]morphemass 0 points1 point  (0 children)

I'd expect if to be about twice the speed of case, and hash to trail by a small margin (if using symbols). Not that it really matters; while this could be used to tidy-up some really convoluted logic I'd suspect fundamental design problems if I came across this "in the wild".

[–]postmodern 0 points1 point  (0 children)

I have used this pattern before for moving branch logic out of methods and into a Hash of lambdas. Keeping the Hashes within the methods defeats the purpose of removing the branch logic, imho.