Exercism: Bob

Yeah, your solution is quite common throughout various tracks on exercism, but it has its drawbacks.

We do only have 4 cases here, of which one is a fallback, so we have 3 “real” cases. Now our customer chimes in and has a CR: “Change bobs reply to yelling to ‘NOT YELL AT ME’”.

Looking at your code it will take me a while to actually figure out which of the clauses does actually belong to yelling, but other solutions like @dogweather’s do give you proper hints were you need to change. They do not require you to understand the conditions, all you need to now is some plain english.

Very similar reasoning applies when there is a request to change the condition for a question. Which one belongs to questions here?

Extrtacting that conditions into predicates does increase readability and maintainability of the code alot. This applies even more on a larger codebase. And thats what exercism is about, giving small problems and requiring you to find solutions for that small problem that scale when the problem grows.

Especially for this exercise, there was a talk given by Katrina Own (founder of exercism), https://www.youtube.com/watch?v=qLpvc5r6Bb0 (I think this one was it, just waded through my browsers history and skimmed the video)

2 Likes

I understand your viewpoint and I get the benefits of abstraction. I think it comes down to a different assessment of at what point to make abstractions. i.e. when do the benefits outweigh the costs? In this case I think that a slight increase in complexity of 3 lines of code is worth it to reduce the size of the codebase by ~70%.

Well, we could have a cond with about a hundred clauses. We could have even started with 3, and then it grew faster then one could change the stuff.

I do pretty much often optimize for readability, maintainability and scalability (of the codebase) rather than speed, unless it turns out that we really have a bottleneck here. And still, I’d prefer to create macros in this case instead of writing plain conditions.

FWIW, my version was sort of a compromise between “keep it simple” (more visible in the initial version) and “keep it clear” (goal of the 2nd iteration): http://exercism.io/submissions/e757624bd8fe4ec8912c0760b29b3e8b

Here is what I ended up writing:

dimitarvp: Exercism Bob solution

Nothing revolutionary and as shared with @NobbZ in the chat there, I opted for a bigger singular function because I did not want to repeat is_binary/1 guard in several smaller functions. I agree with him that private functions are less of a priority – in principle – but my mindset is firmly set in strictness above all else and it’s just my style and way of work.

I have to admit, @Qqwy’s solution looked the best to me though.

1 Like

The chat we made about that solution was in private. In the new exercism all discussions between mentors and students happen privately and are ony accessible by the studen, the mentors of the track and exercism admins.

1 Like

yours is similar to me.

I don’t get your remark? Do you disapprove of cross-posting Exercism discussions here?

If so, okay.

No, I don’t, I do only explain why no-one will ever see the discussion you reference.

2 Likes

Ha, yup, so it is. Looks like we got pretty much the same thing, semantically, except a couple swapped lines, where the order doesn’t matter.

The only nitpick I’d say about yours is that in is_shouting?, since you’ve inserted a linebreak, you may as well drop the comma and do:. Since I’m “old-sk00l” and prefer shorter lines, I’d also reformat the line to fit into 80 chars, but that’s mainly a matter of personal style.

Were it ten years ago, when I was working mainly in plain old C, I would probably also try to memoize the upcasing, but I’ve gotten over obsessing about performance – and for all I know, maybe the Elixir compiler is smart enough to memoize it for me. (Anybody know?)

It wont, as elixir has to assume that all functions are impure and might cause side effects it can not swap out similar function calls with a common value. For the same reason it also can’t swap out variables with the function call creating their value.

All it is allowed to do is to inline functions by replacing the call with the functions body, but due to technical limitations resulting from hot code reloading this is only allowed from within the same module.

1 Like