5 ms·
I can't make it past the first sentance in the opening without just wanting to scream in annoyance. Nothing about immutable objects means that you are going to
by taeric 12y ago
I can't make it past the first sentance in the opening without just wanting to scream in annoyance. Nothing about immutable objects means that you are going to write thread safe code. Pretty much period.
Worse, nothing about functional programming is necessarily about immutability. It is a convenience on small and easy problems, but is far from required. Consider how your computer adds two numbers. Lots of things change state under highly timed situations to give you that answer. It is not some magic table lookup that works with immutable facts.
I may just be bitter by a system right now that is using immutable objects everywhere. I realize this is a straw man, but with heavy reliance on deep (3+ levels) of immutable relationships, it is not uncommon to find crap that is the equivalent of this:
String foo = "hello";
Character bar = foo.charAt(0);
...
//We need to make sure bar is capitalized everywhere
baz = bar.toUpperCase();
//Now to find everywhere this matters and let the caller know.
I realize lenses and zippers and such can help, but so can just being a bit more disciplined about using modifiable items in the first bloody place.
- Dewie 12y ago> I can't make it past the first sentance in the opening without just wanting to scream in annoyance. Can I recommend meditation? Maybe going for a run.
- taeric 12y agoYeah, screaming is a touch heavy. I should have just said drop my head in sadness. And that I lost a little more hope from the industry. It is funny that this would lead to meditation, as there is a good parable that seems appropriate here.[1] Not all fads are processes, and I'm sure there is a "truly immutable patterns don't have this problem" hiding somewhere. [1] http://www.ckwop.me.uk/Meditation-driven-development.html http://www.ckwop.me.uk/Meditation-driven-development.html
- davexunit 12y agoYour example makes no sense to me, particularly the "find everywhere this matters" part. Working with immutable data is fairly straightforward. You perform an operation on a data structure, which produces a new version of that data structure, and then you to return it.
- taeric 12y agoThis only works if you still have a reference to the root of the structure everywhere. Which is why something like Huet's zipper works. If you did not build such a structure, then you are likely out of luck. A better example would be SomeLargeTreeThing foo bar = sanitizeTreeThing(foo); //Now, make extra sure nobody uses foo. //Not even accidentally in the future. Now, this can be solved with more types and more layers. So, yes, it is bad code. But, "write less crappy code" is far less sexy than "immutability prevents errors!"
- ticking 12y agoI think you got immutability and single side assignment mixed up. If foo is immutable there is no harm if something else tries to manipulate it because it will just derive a new data-structure.
- theseoafs 12y agoYour complaint doesn't make any sense. I think you're struggling with immutability because you don't understand that it actually works very differently from mutability. In this example, it's not about "making sure nobody uses foo". Nobody is making you use it again. If you don't want to use the variable again, just... don't use it again. Or, you can use it again, if you want to, but if you make an error your tests should fail or your compiler should complain about an unused variable. It's not rocket science.
- taeric 12y agoI literally just had to fix this error in my colleague's code. It looked a little more like: public foo(Bar bar) { Bar updatedBar = baz(bar); //Lots (10+ lines) of code return finishProcess(bar); //Note, this should have been updatedBar. } Now, again, I will not defend this as good code. Neither would my coworker. Mistakes happen, though. And while I will not claim that mutable code is free of defects and makes it easier to write troublefree code, I do take issue with the claim that immutability does accomplish this.
- tel 12y agoObviously this is just a short fragment and explicitly it's a straw man, but I can't help but get this incredibly strong feeling that this code is immutability bolted on instead of embraced. Lenses, yes, could be used to solve something like this, but it feels like you're facing a problem which is better avoided than fixed. I'm not really trying to rain on your tirade---surely there are ways to misuse immutability---but I can't look at this example and not feel that immutability is telling you something about a better architecture.
- taeric 12y agoOh, I certainly agree. And really... this is my point. Misused immutability is just as garbage heavy as misused mutability. It does not, ipso facto, prevent bugs or make threaded code easy to do.
- eweise 12y ago//Now to find everywhere this matters and let the caller know. Do you mean callers, who may be threads some of which have decided that the first character of foo should be lowercase while others have been sticking a prefix on foo? How do you coordinate all these separate changes to your foo string? Is there a String manager that coordinates all the threads and decides what the best string update policy is? Maybe I'm not following your argument for mutable strings.