views:

90

answers:

7

We have a lot of objects for which we like to implement a simple toString to output attributes of the object. Some of these attributes may be complex objects themselves.

Is there any standard, or simply just a best practice for a style? I'm thinking something like:

[SimpleClassName] { prop1:value, prop2:value }

In which case a nested value would look like:

[SimpleClassName] { prop1:value, prop2:[NestedObject] { prop3:value}}

We are using Java but I find myself asking the same question in most languages!

A: 

json syntax seems to fit pretty well since it was designed specifically to represent complex objects as strings

Person = {
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": 
    {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": 
    [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
bemace
I think `toString` should really aim to return a single line, so something like json might be overly verbose.
zigdon
@zigdon - there's no reason you couldn't condense it into a single line
bemace
Right, but it would still be fairly verbose on large nested objects.
zigdon
True. So far I'm liking Wouter's, but I'm out of votes for the day
bemace
A: 

If your objects have something that might be useful as an identifier, I'd implement something like your second example:

[SimpleClassName:id] { prop1:value, prop2:[NestedObject:id] { prop3:value }}

Where the id is whatever makes sense for that object to be an identifier - the name for the canonical Person object, a primary key for an object from a database, etc.

zigdon
+2  A: 

Personally, I find the the mix of [] and {} not so easy to get an immediate view of the hierarchy.

I like this format (and I've seen it being used in a number of places):

SimpleClassName[prop1=value, prop2=value]
SimpleClassName[prop1=value, prop2=NestedObject[prop3=value]]

There's also the possibility to add an identifier with @, for example the default style for the commons-lang ToStringBuilder does that (using it's own example):

Person@182f0db[name=John Doe,age=33,smoker=false]
Wouter Coekaerts
I like it, but any idea how this meshes with toString used by various frameworks and other open source projects?
Renesis
A: 

Since you asked about what other open source projects to, here's how jEdit does it, which is similar to Wouter's:

BufferChanging[what=BUFFER_CHANGING,source=org.gjt.sp.jedit.EditPane[active,global]]
bemace
A: 

check out phps print_r($obj, true) or also serialize() could work, dont know exactly for what its needed for. jsons is also a clean solution, especially if u want to import the data in javascript environbments

w13531
A: 

I think the format produced by Guava's Objects.toStringHelper() is pretty nice, but it's mainly just good to have some consistent format that you use:

public String toString() {
  return Objects.toStringHelper(this)
      .add("prop1", prop1)
      .add("prop2", prop2)
      .toString();
}

// Produces "SimpleClassName{prop1=foo, prop2=bar}"
ColinD
A: 

Is there any standard, or simply just a best practice for a style?

No. The "best" output for a toString() method is determined by what you want to use it for. Is it for serializing the object state in a form that allows it to be deserialized? Is it for creating debug messages? Is it for rendering the object for display to end-users?

If you want to develop an in-house style for your debug/logging toString() methods, that's fine. But unless there was a requirement for this, I wouldn't bother. IMO, it is effort that could better be spent elsewhere.

Stephen C