3 ms·
> Avoiding shared state mutation isn't especially hard in imperative languages You're right, it isn't, and I wasn't trying to claim it was a particularly big p
by sodapopcan 4y ago
> Avoiding shared state mutation isn't especially hard in imperative languages
You're right, it isn't, and I wasn't trying to claim it was a particularly big problem. What it is is another little you don't even have to think about which I found to be a pretty delightful feeling. Cloning isn't a consideration in mutable languages and that's a nice a little win. Though as a sibling comment mentions, if you're working with a bunch of inexperienced folk, these things are a lot more likely to slip through.
As for the debugging bit, I made the sin of comment before I read the article and realized it was using JS. No matter what anyone wants to tell you, JS is very good as a functional language. It can be ok, but it's not great. "Most functional styles" is otherwise a very unfair statement as most functional languages come with pipes or infix application operators that make debugging really easy as you can throw an `inspect` into a pipeline.
Getting a bit off the rails just as I was excited about this, but Elixir recently introduced a `dbg` function which, if used at the end of a pipeline, outputs every line of it which is crazy nice (unsure if this was borrowed from another language or not):
"hello there"
|> String.split()
|> IO.inspect()
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
|> dbg()
produces:
[file_name.ex:8: ModuleName.function_name/0]
"hello there" #=> "hello there"
|> String.split() #=> ["hello", "there"]
|> IO.inspect() #=> ["hello", "there"]
|> Enum.map(&String.capitalize/1) #=> ["Hello", "There"]
|> Enum.join(" ") #=> "Hello There"
So that's really nice.
- agumonkey 4y agonow all we need is an accumulative dbg so you can inspect data flow between different runs :)
- sodapopcan 4y agoedit: of course I meant that cloning isn't a consideration in IMmutable languages and that JS is NOT very good as a functional language... oh boy. Annnd another nice thing is consider code like this: perform_some_action_with_object(user) user.i_hope_user_hasnt_changed() In mutable languages we can't be sure that `perform_some_action_with_object` doesn't mutate `user` so we have to check. This isn't even a consideration in immutable languages. Again, not the biggest problems in the world, but it really helps when you want to quickly scan through a codebase you are unfamiliar with. You could also say "don't write code like that" and I tend to agree, but I have seen a lot of code like that in OO codebases in my time. I made sure to double check my spelling this time :D