DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Is Java's String Class a God Object?

Is Java's String Class a God Object?

Roger Hughes user avatar by
Roger Hughes
·
Feb. 27, 12 · Interview
Like (0)
Save
Tweet
Share
7.77K Views

Join the DZone community and get the full member experience.

Join For Free
In October I wrote a blog entitled Top Trumps in God Objects where I talked about the discovery of an object I’d found with 167 disparate methods that linked this object with all other parts of the application and, as you’d expect, followed the general criteria for a God or Monster Object. It was recently pointed out to me that method count alone isn’t an indication that an object is a God Object, which is quite true. The comment also went on to site the String class as an object that isn’t a God Object, but has a large number of methods.

This got me thinking: is the String class a well designed class in terms of the general rules of Object Oriented Design? Does it, in fact, break the single responsibility principle or the laws of demeter and is it tightly coupled to all other parts of an application? Is it a God Object?

The easy way to answer this is to look at a String in terms of the Single Responsibility Principle, and the best way to do that is to count the number of method calls it has and figure out what they do.

If you do that you’ll find that Java’s String class has 66 method calls in it’s repertoire and you may conclude that a number this high could be the first sign of a God Object.

Next, if you look at these 66 method calls you can group them into several different areas of responsibility. For example there are:

Conversion Methods

  • getBytes(...)
  • getChars(...)
  • valueOf(...)
  • toUpperCase(...)
  • toLowerCase(...)
  • toCharArray(...)

Methods that split String

  • substring(...)
  • split(...)
  • subSequence(...)

Equivalency Methods

  • equals(...)
  • matches(...)
  • compareTo(...)

Search Methods

  • indexOf(..)
  • lastIndexOf(...)
  • charAt(...)
  • contains(...)

Combining Methods

  • append(...)
  • concat(..)
  • +
  • +=
...and more besides.

Using the lists above, you could now write a CRC card for a String that goes something like this: “A String is responsible for combining itself with other Strings to create new Strings AND converting itself into other objects or arrays of objects AND splitting itself into different pieces AND comparing itself to other String objects AND performing searches on itself”

Sticking to the letter of the law then the lists above show that String’s method calls break down into at least five areas of responsibility, which means that, by the book, the Single Responsibility Principle has been violated.

Taking this to its logical conclusion, then you could refactor a String to look something like this....


However, and this is the question, why isn’t Java’s String class composed of some data and a bunch of classes that operate on it? The answer is both a matter of opinion and multifaceted. Firstly, history... Java as grown over time as so has the String class; methods have been added piece-meal as the Java version number ticks ever upwards. Secondly, pragmatism. A String object in it’s current design may break the Single Responsibility Rule, but it doesn’t matter, it’s easier to leave it as it is and get on with writing the programs that count. Lastly, and I think that this is important, we all generally know what a String is. Its name and meaning is in common usage. If we had to define what a String’s responsible for using a CRC card, then we’d write something like: “a String is responsible for being s String”.

A String may have 66 methods, but I believe that by general consensus it’s not a God Object. However, software is just a matter of opinions, so I’ll leave you answer the other questions I posed at the beginning of this blog and to make your own mind up...

 

From http://www.captaindebug.com/2012/02/is-javas-string-class-god-object.html

Strings Object (computer science) Data Types Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • 10 Best Ways to Level Up as a Developer
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: