Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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. –  FrustratedWithFormsDesigner Feb 11 '11 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 Feb 11 '11 at 18:45
4  
Your title screams ToString –  Aaron McIver Feb 11 '11 at 18:46
1  
@shabbychef: If your object is CodeGenerator, having a toString() method for it fits semantically. –  Jeremy Heiler Feb 11 '11 at 18:48
2  
toString() sounds more like a user(programmer?)-readable representation, for the program itself (reflection) or debugging. –  Michael K Feb 11 '11 at 18:56
show 5 more comments

11 Answers

up vote 5 down vote accepted

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

share|improve this answer
1  
Seems like a bad name to me. It tells me nothing about what it does. –  Alb Feb 11 '11 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 Feb 11 '11 at 20:35
3  
@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 Feb 11 '11 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 Feb 11 '11 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 Feb 12 '11 at 1:51
show 6 more comments

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.

share|improve this answer
    
+1 for having the right answer for the question title. –  Jon Purdy Feb 11 '11 at 20:26
    
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 Feb 12 '11 at 5:54
add comment

I'd pick something along these lines:

  • toStringRep()
  • createStringRepresention()
  • strRep()
  • toSource()
  • toSourceString()
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
1  
or maybe toSourceString()? –  Michael K Feb 11 '11 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 Feb 11 '11 at 19:59
add comment

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.

share|improve this answer
    
as you intuited, we will have both such methods, and there will be plenty of room for misinterpretation. –  shabbychef Feb 14 '11 at 17:31
add comment

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 );
share|improve this answer
    
I was looking for common usage across languages. For the record, we have this problem in Python, Perl, Matlab and R. –  shabbychef Feb 11 '11 at 21:58
add comment

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.

share|improve this answer
add comment

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()?

share|improve this answer
    
I think of 'Hash' as being irreversible. –  shabbychef Feb 11 '11 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 Feb 12 '11 at 1:37
add comment

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

share|improve this answer
add comment

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."

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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