3

I was reading through another SO question, Swift do-try-catch syntax. In his answer, rickster creates an extension for the OP's custom class. Konrad77 comments that it's a "Really nice way to keep your code clean." I respect their knowledge which leads me to believe I'm missing the point somewhere in my own code.

Are there any other benefits (aside from cleanliness) or reasons to create an extension for a class I created? I can just put the same functionality directly into the class. And does the answer change if I am the only one using the class or if someone else will be accessing it?

Community
  • 1
  • 1
Shades
  • 5,568
  • 7
  • 30
  • 48
  • "Are any other benefits (aside from cleanliness) or reasons to create an extension for a class I created? I can just put the same functionality directly into the class." Define benefits. I voted to close this question as primarily opinion-based because answers can only be based on preference, not fact. Extensions are just another way to write class functionality. How they're used and created is up to the original programmer. – JAL Mar 28 '16 at 14:15
  • @JAL I respect the close votes. They in essence do answer my question. I was thinking I was missing some other nuance to the process. – Shades Mar 28 '16 at 14:19
  • I don't feel that this question is strictly opinion-based. Yes, the answers will contain opinions but they also have the potential to clarify good practices that extensions can promote. –  Mar 28 '16 at 14:23
  • 2
    Same question here: http://stackoverflow.com/questions/24793696/why-extend-class-in-extension-not-directly-in-class-itself – Martin R Mar 28 '16 at 14:24
  • @MartinR Thank you. I didn't see that one before I posted – Shades Mar 28 '16 at 14:26
  • @MartinR The original question to which mine was duplicated has been removed. Voting to reopen this question. – Shades Aug 12 '16 at 23:25
  • Here's [another answer](http://stackoverflow.com/q/40502086/104790) regarding the same topic. – Nikolai Ruhe Nov 09 '16 at 15:53

1 Answers1

8

In the case of a class that you create from scratch extensions are a powerful type of documentation through structure. You put the core of your class in the initial definition and then add on extensions to provide additional features. For example, adding adherence to a protocol. It provides locality to the contained code:

struct Foo {
  let age: Int
}

extension Foo: CustomStringConvertible {
  var description:String { return "age: \(age)" }
}

Could I have put the protocol and computed property in the struct declaration? Absolutely but when you have more than one or two properties it starts to get messy and difficult to read. It's easier to create bugs if the code isn't clean and readable. Using extensions is a great way to stave off the difficulties that come with complexity.

  • 2
    This doesn't answer the question, as OP specifically mentioned "aside from cleanliness". – Madara's Ghost Mar 28 '16 at 14:23
  • 3
    Using an extension in this way goes beyond simple cleanliness. Part of coding is to make code understandable and avoid bugs due to confusion. We all know that there are many ways to code a solution many of the best solutions are the ones that do it in a clear, concise, readable manner. By grouping related code you can increase the usefulness of the code, not just make it cleaner. It becomes self-documenting. –  Mar 28 '16 at 14:29
  • 2
    This is all an extension (pun not intended) of "cleanliness", it's an all encompassing name that refers to code being readable, structured, easy to debug, and easy to understand. – Madara's Ghost Mar 28 '16 at 14:30
  • 3
    At some point there needs to be a distinction between simple cleanliness and deep, useful, desired structure. After all, I could code a solution in any Turing-complete language. We use higher level languages because they often produce cleaner, more readable, and easier to use code. Good structure goes beyond just looking nice, it's a feature of the code. –  Mar 28 '16 at 14:36