• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convention for displaying class data

 
Charles Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many classes contain methods that display the data in the object. Is there a convention for the names of methods that provide various levels of detail? For example, consider a class called Point containing floats for x and y coordinates of the point. One method might display "2.0, 3.0" and another might display "(x=2.0, y=3.0)". A more complicated class might have several formats.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None that I'm aware of.

The toString() method is usually used to create a human-readable string representation of the object, but the level of detail is up to the person overriding it. Anything beyond that is at the programmer's discretion. For example, for a detailed representation I usually use something like dump(). You could also create a method that takes an enum that defines granularity, similar to logging levels.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're really feeling bold you can write your own Format subclass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic