<p>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.</p>
<pre><code>public class StringPlusMutation extends String {
public string mutate() {
// add mutation code
}
}
</code></pre>
<p>But, all you have to do in languages with extension methods, such as Kotlin, is create a method that directly extends the String class.</p>
<pre><code>Fun String.mutate():

// add mutation code
</code></pre>
<p>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?</p>

<extension-methods>
What are the advantages and disadvantages of extension methods?
CC BY-SA 4.0
2023-07-03T22:51:00.390Z
Related: [Advantages of extension methods vs uniform function call syntax (UFCS)?](https://langdev.stackexchange.com/q/265/89)
kaya3
<p>I think the pros have been pretty well covered in the question but I would like to add some more cons here:</p>
<ol>
<li><strong>Name collisions/Semantic overloading</strong>. 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.</li>
<li><strong>Discoverability.</strong> They aren't immediatly visible to developers who don't know about extensions, affecting discoverability complicating understanding available functionality of a type.</li>
<li><strong>Cluttering</strong>. They can make the codebase more complex and less maintainable if used in excess.</li>
<li><strong>Can generally only access public members.</strong> They can generally only access public members of the extended type which limits the use cases added through extension methods.</li>
</ol>

CC BY-SA 4.0
2023-07-04T11:22:15.417Z
2023-07-04T14:39:42.310Z
2023-07-04T14:39:42.310Z
Starship
CC BY-SA 4.0
2023-07-04T11:35:13.620Z
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.
Bbrk24
CC BY-SA 4.0
2023-07-04T11:40:01.740Z
Most of the time they can only access public memebers @Bbrk24
Starship
CC BY-SA 4.0
2023-07-04T14:37:55.800Z
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?!?
Philipp
CC BY-SA 4.0
2023-07-04T14:39:06.723Z
Yeah that would be bad @Philipp
Starship
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2023-07-04T02:26:28.197Z
2023-07-04T02:26:28.197Z
CPlus
# What are the advantages and disadvantages of extension methods?
Posted by: username_0 on 2023-07-03
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?
**Tags:** extension-methods
> **Comments:**
> **username_1**: Related: [Advantages of extension methods vs uniform function call syntax (UFCS)?](https://langdev.stackexchange.com/q/265/89)  
---
## 2 answers
### Answer by username_4 on 2023-07-04. Score: 3 | Accepted answer
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.
---
### Answer by username_0 on 2023-07-04. Score: 3
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.
> **Comments:**
> **username_2** (2023-07-04): 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.  
> **username_0** (2023-07-04): Most of the time they can only access public memebers @Bbrk24  
> **username_3** (2023-07-04): 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?!?  
> **username_0** (2023-07-04): Yeah that would be bad @Philipp  
---
