Skip to main content
added 33 characters in body
Source Link
Ben Aaronson
  • 5.7k
  • 21
  • 39

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

Sure, your method is better. It's a pretty straightforward example of some fundamental programming and design concepts:

  • You spotted repetition. The same thing is being repeated multiple times. In this case, it's 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

Sure, your method is better. It's a pretty straightforward example of some fundamental programming and design concepts:

  • You spotted repetition. The same information is being stated multiple times. In this case, the 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
Source Link
Ben Aaronson
  • 5.7k
  • 21
  • 39

Sure, your method is better. It's a pretty straightforward example of some fundamental programming and design concepts:

  • You spotted repetition. The same thing is being repeated multiple times. In this case, it's 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