7

We are having a game of 'semantic football' in the office over this matter: I am writing a method for an object which will represent the object as a string. That string should be such that when typed (more likely, cut and pasted) into the interpreter window (I will keep the language name out of this for now), will produce an object which is, for our purposes, identical to the one upon which the method was called. There is a spirited discussion over the 'best' name for this method.

The terms pickle, serialize, deflate, etc have been proposed. However, it seems that those terms assume some process for the de-pickling (unserialization, etc) that is not necessarily the language interpreter itself. That is, they do not specifically refer to the case where strings of valid code are produced. This is closer to a quine, but we are re-producing the object not the code, so this is not quite right.

any suggestions?

10
  • 5
    How about "stringify"? Since I don't think anyone I know uses this term, you can come up with your own semantics for it. Commented Feb 11, 2011 at 18:44
  • 4
    Isn't "ToString" the common function name for this in a few languages or is this slightly different than a few cases where I've seen this in my experience?
    – JB King
    Commented Feb 11, 2011 at 18:45
  • 4
    Your title screams ToString Commented Feb 11, 2011 at 18:46
  • 1
    @shabbychef: If your object is CodeGenerator, having a toString() method for it fits semantically.
    – Jeremy
    Commented Feb 11, 2011 at 18:48
  • 2
    toString() sounds more like a user(programmer?)-readable representation, for the program itself (reflection) or debugging.
    – Michael K
    Commented Feb 11, 2011 at 18:56

11 Answers 11

6

In Python that is the "repr()" function. A string representation of the object.

11
  • 2
    Seems like a bad name to me. It tells me nothing about what it does.
    – Alb
    Commented Feb 11, 2011 at 20:31
  • @Alb: Take it up with the rest of the python community. We all know it as repr() and we implement a special __repr__ method in our class definitions to work with it.
    – S.Lott
    Commented Feb 11, 2011 at 20:35
  • 4
    @S.Lott Obviously you're not responsible for what it's called in python, but suggesting using a bad name just because it's used in python doesn't seem like a good idea to me.
    – Alb
    Commented Feb 11, 2011 at 20:37
  • 1
    @Alb: Agreed. You have no idea what any method really does. You can guess from the Java docs because you have a pretty deep knowledge. Read some of the ridiculous StackOverflow questions from folks who don't seem to be able to interpret the method names. Your standard for "good" or "bad" doesn't have much impact on other people's ability to remember what it does or guess wisely. Indeed, a surprising number of people look at the same methods you look at and guess very, very poorly.
    – S.Lott
    Commented Feb 11, 2011 at 20:52
  • 1
    @Alb: Liking them or not liking them doesn't matter. A judgement of "good" or "bad" doesn't matter. It turns out they work, even though we might attempt to judge them as "bad". I'll take "works" as more important than our judgements of "good" and "bad". Indeed, I think our judgements of "good" and "bad" are incorrect, since Unix works and Python works.
    – S.Lott
    Commented Feb 12, 2011 at 1:51
12

If you were writing Perl, you'd essentially be duplicating Data::Dumper's functionality. It takes a Perl object and turns it into a string representation that can then be used to recreate that object (essentially -- this isn't really true for blessed objects). The description of this module says it creates "stringified perl data structures".

Thus the process is stringification. That's what I've always heard it called.

1
  • I prefer to call it "dumping" an object, but your example is the best I've seen. In Java I would override toString() for a single object or use an XLM binding library to dump a more complex tree. The result is more often something I can paste into a spreadsheet or other tool than raw code.
    – Chris Nava
    Commented Feb 12, 2011 at 5:54
4

I'd pick something along these lines:

  • toStringRep()
  • createStringRepresention()
  • strRep()
  • toSource()
  • toSourceString()
4

Even if I see your point about serialize hinting at deserializing (or is it "unserializing" ?) being the opposite, that would still be my preferred wording because it naturally implies that the process returns a string equivalent.

4

I nominate sourcification. I agree that "quine" isn't quite right, but the part where you're producing valid source code for the object seems key.

2
  • 1
    or maybe toSourceString()?
    – Michael K
    Commented Feb 11, 2011 at 19:37
  • @Michael: That seems good for a method name, yeah. OP seems to be asking for a general word for the concept more than method names, though.
    – chaos
    Commented Feb 11, 2011 at 19:59
2

I like the pickle idea. It quite nicely says nothing at all, leaving people seeing it asking themselves WTF and having to search for documentation and/or the implementation to figure out what client code is doing...all because of a function name that is completely random.

The guy that used to run the development team where I work liked the Master and Commander book series. He liked to name things after the boats mentioned in those books. I have to say that I never ceased being titillated by the cute wit it must have taken to use those names in code. It always brought a smile to my face as I right clicked the name in VS and said, "Go to Definition," and got to wait the 20 minutes for stupisense to lock up my computer and finally take me to some random location that had nothing to do with the symbol I was looking for.

1

The method name should say what it does, not why it does it, therefore:

toString() because it's how you phrased it in the title. What's done with the string afterwards (i.e whether the object can be recreated from it) is nothing to do with the method that creates the string so shouldn't influence the method name.

If you want to also have a toString() method that creates a human readable representation of the string you could make the other one more specific like stringify() to differentiate but I would only do this if necessary.

1
  • as you intuited, we will have both such methods, and there will be plenty of room for misinterpretation.
    – shabbychef
    Commented Feb 14, 2011 at 17:31
1

It seems to me that you are looking to output not just a string, per se, but rather a blueprint of the object. Something that encapsulates how it behaves and all of its attributes. You stated that this string will need to be parsed by your language interpreter, which leads me to understand that this data is being stored in a language of its own. The language that only your language interpreter can read and write. Given that, what's this language called?

Whatever you decide that it is called could then be used in the name to clarify what is being done.

myLangRepresentingObject = LanguageInterpreter->convertObjectToMyLanguage( SomeObject );

For example, if the string stored the objects using the language, XML, you could use something like this:

xmlRepresentingObject = XmlInterpreter->convertObjectToXml( SomeObject );
1
  • I was looking for common usage across languages. For the record, we have this problem in Python, Perl, Matlab and R.
    – shabbychef
    Commented Feb 11, 2011 at 21:58
1

In Common Lisp this is called the printer and the reader.

Quoting the Hyperspec :

"write, prin1, princ, print, and pprint write the printed representation of object to output-stream. "

"read parses the printed representation of an object from input-stream and builds such an object."

0

Aren't you basically looking for a Hash() function? Where you would create a string representation of an object state that is reversible (unlike a normal hash).

Edit:I repeal the Hash() idea.How about ToSnapshot() and FromSnapshot()?

2
  • I think of 'Hash' as being irreversible.
    – shabbychef
    Commented Feb 11, 2011 at 21:58
  • i have to agree with your comment. The more I thought about it Hash() seemed to be more misleading than clarifying.
    – TheZenker
    Commented Feb 12, 2011 at 1:37
0

Encode or decode would be a a more general terminology for a bidirectional process. You may also consider transform for a one way process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.