3
$\begingroup$

They allow you to add methods to an existing class without extending the class directly. Normally, you can only create mutation methods by creating your own class that extends String as I have below.

public class StringPlusMutation extends String {
public string mutate() {
// add mutation code
}
}

But, all you have to do in languages with extension methods, such as Kotlin, is create a method that directly extends the String class.

Fun String.mutate():

// add mutation code

You can call mutate on it as if mutate was part of the String's original set of public methods. Since this feature is relatively rare though I figure there must be some other things in the picture here. So, what are the advantages and disadvantages of extension methods?

$\endgroup$
1

2 Answers 2

3
$\begingroup$

One advantage is that this permits extending the default classes to add additional functionality, to allow users to create their own useful extensions as they see fit. Another is the conciseness of not having to create a custom class just to extend an existing class with methods.

The only disadvantage I see is security. If there is a class with a method that calls another method, and the implementation of this feature is such that the client can override existing methods, they can change the behavior of the class in more ways than expected.

$\endgroup$
3
$\begingroup$

I think the pros have been pretty well covered in the question but I would like to add some more cons here:

  1. Name collisions/Semantic overloading. They're resolved based on namespace and accessibility, causing name collisions when multiple extensions with the same method name are in scope and introducing ambiguity over which extension method will be invoked. They can add semantic overloading if multiple extension methods with the same name are available for a type, leading to confusion if the appropriate extension method is not selected or if the developer doesn't know about it. Even worse, as Phillip said in the comments, the original class might get changed, gaining a new method with the same name and signature as the extension, but different behavior.
  2. Discoverability. They aren't immediatly visible to developers who don't know about extensions, affecting discoverability complicating understanding available functionality of a type.
  3. Cluttering. They can make the codebase more complex and less maintainable if used in excess.
  4. Can generally only access public members. They can generally only access public members of the extended type which limits the use cases added through extension methods.
$\endgroup$
4
  • $\begingroup$ In Swift, extensions can access private members if they're declared in the same file as the type itself, which is sometimes necessary for conditional conformances. $\endgroup$
    – Bbrk24
    Commented Jul 4, 2023 at 11:35
  • $\begingroup$ Most of the time they can only access public memebers @Bbrk24 $\endgroup$ Commented Jul 4, 2023 at 11:40
  • $\begingroup$ Even worse scenario for point 1: the original class might get changed, gaining a new method with the same name and signature as the extension, but different behavior. What then?!? $\endgroup$
    – Philipp
    Commented Jul 4, 2023 at 14:37
  • $\begingroup$ Yeah that would be bad @Philipp $\endgroup$ Commented Jul 4, 2023 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .