

Lol yeah she’s in insurance! I bet you could probably also infer from the fortran that I work for a science-y outfit.
Lol yeah she’s in insurance! I bet you could probably also infer from the fortran that I work for a science-y outfit.
My wife was telling me at her work they’re desperate for cobol programmers, as they’re all retiring boomers leaving behind a giant code base. At my work, it’s legacy fortran that’s all over the place, but we’re a much smaller company.
That’s the point, when programming with immutable structures you always pass the mutability onto the enclosing structure.
I guess the point I was trying to make here was if the data type is already mutable, there is no point in sticking it in a list just so you can replace a reference with an identifier. You’re just adding an extra level of indirection. But sure yeah, if the type is inherently immutable, you have to do something.
A list is an antipattern here IMO. Just wrap it in some dedicated object (see e.g. Java’s StringBuilder).
Interesting. I’m not aware of anything like StringBuilder
in the standard library for either Python or JavaScript. Looks like it wraps a list of characters and tries to behave as string-like as possible? You could presumably write your own class like that or download an implementation from someplace.
I guess in most cases in my own code, where I need a mutable string is usually as part of a larger data structure which is the thing that gets passed around by reference, so it’s easy enough to replace a field within that.
For building up a string, I would tend to use an io.StringIO
in Python with file-writing calls, but those aren’t meant for sharing. What you don’t want to do is use the +=
operator a lot on strings. That gets expensive unless strings are mutable (like they are in say C++'s std::string
).
Well, but then you’re basically just pushing the mutability onto the container, since you need to be able to replace elements within it.
It’s a good strategy at times though. Like say you’re working in a language where strings are immutable and you want a string you can change. You can wrap it in a list along the lines s=['foo']
and pass references to the list around instead. Then if you go s[0]='bar'
at some point, all the references will now see ['bar']
instead.
As others have pointed out, there is the issue of breaking references to objects.
There can also be a lot of memory thrashing if you have to keep reallocating and copying objects all the time. To some extent, that may be mitigated using an internment scheme for common values. In Python, for example, integers are immutable but they intern something like the first 100 or so iirc? But that doesn’t work well for everything.
Any container you want to populate dynamically should probably be mutable to avoid O(N²) nastiness.
How’s this for an obscure reference? This reminded me of an episode of Max Headroom in which the wunderkind Bryce invented a robotic fly with a spycam that could be used to literally bug a room. They send it on a mission to uncover an evil plot and everyone is excitedly crowded around the screen and heaping praise on it. Then it manages to sneak into the evil lair where it promptly gets swatted, leaving Bryce shocked and devastated.
Ouch. Well if you wanna take your chances in Canada, definitely advertise your senior COBOL dev skills! ;)