Sure, your method is better. It's a pretty straightforward example of some fundamental programming and design concepts:
- You spotted repetition. The same thinginformation is being repeatedstated multiple times. In this case, it'sthe information is how to perform a particular operation for formatting a string. Generally, you should not repeat yourself.
- This clued you in to an abstraction: the implementation of this formatting operation can be abstracted out behind a meaningful name
- With this knowledge you abstracted out the operation by applying an extract method refactoring.
- As a result, you now have code which is:
- More readable, since the methods are simpler, and the name of the formatting method explains exactly what its purpose is.
- More maintainable, since now any change to this formatting operation just has one place to occur, rather than having to be made in all the places that it's repeated