James P.
James P.

Reputation: 19617

What justifies choosing a method or class?

Say you need to reverse a String.

You could make a method called reverseString. But would you consider making a class called StringReverser ? If not, what criteria do you use to decide that a class needs to be made ?

P.S: This may touch the topic of systems analysis. If you are familiar with some questions or rules of thumb that answer the question clearly please share.

Upvotes: 0

Views: 33

Answers (2)

NPE
NPE

Reputation: 500475

This is a judgement call.

Here, we are looking at a single function that performs a single operation and requires no state. Furthermore there's no need for polymorphic behaviour. In these circumstances, I would create a static[1] class or a module called StringUtils, and would place the function there. I would place other similar functions into the same class/module.

[1] By "static", I mean a class that consists entirely of static methods.

Upvotes: 1

Dai
Dai

Reputation: 155280

Only make an instance class if you have instance-state: that is, if you're representing a persistent object in-memory.

I would only create a StringReverser class if the act of reversing a string required persistent state or configuration - but I can't think of any, so I wouldn't.

But consider the difference between the method String.Concat(params String[] strings) and the class StringBuilder, both do the same thing (concatenate strings together), but StringBuilder has state that you can pass between consumers, which is not something you can do with the .Concat method.

Upvotes: 1

Related Questions