tags:

views:

206

answers:

7

When we talk about strings as being mutable, is this synonymous with using the word 'changeable' or 'modifiable' or is there some additional nuance to explain why this jargon is used instead of a simpler word like 'modifiable'?

+2  A: 

A mutable object is an object that can be modified after it has been created. So in a way, it is synonymous with "modifiable." The word "mutable" means "liable or subject to change or alteration." I think it sounds better than "modifiable."

Robert S.
"in a way"? It is synonymous.
It depends on the context.
Robert S.
+7  A: 

I think the word "mutable" is a good option for this.

If you used "modifiable", it would be less clear. For example, if your string is a heap allocated type, when you say you are modifying the "string", it's not clear whether you're changing the data on the heap (the string's contents) or the string's heap pointer.

However, "mutable" suggests that the string's actual data is changing, to me. I think it's due to it being the same root as to mutate. If something is mutating, it's not changing from looking at A to looking at B (ie: changing a pointer), but rather mutating itself, or becoming (iteratively) something it was not originally.

Reed Copsey
+1 for a reasoned answer. Just noticed a typo: "...changing the data on the [heap]"
Dave
+1  A: 

Yes, mutable is the term for an object that is capable of being changed after it is created, whereas immutable refers to an object that cannot change. Mutable literally means "ability to mutate" which I think fits perfectly with how mutable objects behave.

Heather
A: 

Specifically in relation to strings, in a mutable string, if you change the first character from 'A' to 'B', you're still dealing with the same bytes in memory; one of them has just taken a different value. With immutable strings, i.e. in Java, a new object would be created at a new location in memory, because the original one cannot be changed.

Mutable is a decent word to describe that, and it has the advantage of not being loaded with real-world meanings or interpretations that would make it difficult to use precisely.

DNS
A: 

And since you didn't point out the specific context you're speaking of (and so please forgive me if this is scope creep)...

Strings in .NET specifically are immutable, not mutable. Once they're created, that's it. They never change. If change attempts are made, new strings are created and the old ones get released for garbage collection.

Boydski
A: 
  • mutable
    • "liable or subject to change or alteration."
  • modifiable
    • "to change somewhat the form or qualities of; alter partially;"

conversely

  • immutable
    • "not mutable; unchangeable; changeless"
  • unmodifiable
    • "incapable of being modified in form or character or strength"

In programming you more often hear the terms mutable and immutable than you do modifiable and unmodifiable. However I think it is safe to say that either way has the same meaning.

But when in Rome... so you should use mutable and immutable as they are the more commonly used terms (at least in my experience).

As to why that choice? I asked my mother in law (she is up on words :-) and from a non-programming point of view "mutable" is shorter then "modifiable"... seems likely enough of a reason to me.

TofuBeer
We're agreed then that choosing one word over the other is just a preference that's based on convention, since they are synonymous. I thought maybe there was something more to it than that. Thanks.
A: 

Here is a pretty simple explanation of mutable/immutable using java as an example:

Mutable

  • can be changed without reallocating memory
  • a StringBuffer api documentation is a mutable version the standard string class

  • to change the standard value of the String class you need to reallocate memory

I have never really heard of using the term modifiable.

zPesk