what is the preferred way - functions/static methods or extension methods

This forum is meant for questions and discussions about the XSharp Tools libraries, which contain code written by XSharp users that they want to share with others,
The development team is not responsible for this code

Moderator: wriedmann

User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

what is the preferred way - functions/static methods or extension methods

Post by wriedmann »

Hi All,

what do you prefer: extension methods or functions/static methods?

The first would be called

Code: Select all

aTarget := aSource:SubArray( nStart, nLength )
and the second

Code: Select all

aTarget := SubArray( aSource, nStart, nLength )
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

what is the preferred way - functions/static methods or extension methods

Post by Karl-Heinz »

Hi Wolfgang,

no final decision made, but currently i would use extension methods to extend e.g. gui classes, but not to add functionality to types like string, int etc. Ok, in .net everything is an object, but maybe i´m too old and worked too long with the VO mixture of objects and pure funcs ;-)

regards
Karl-Heinz
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

what is the preferred way - functions/static methods or extension methods

Post by wriedmann »

Hi Karl-Heinz,

for myself I prefer the extension methods, even to basic types likes strings, because .NET already uses them und they are easier to read and write.

When you use an extension method you will understand immediatly on wich data the method is used.
The code

Code: Select all

nPos := aBuffer:IndexOfBytes( aPattern, nOffset )
IMHO is easier to understand than

Code: Select all

nPos := IndexOfBytes( aBuffer, aPattern, nOffset )
And Intellisense is another advantage of extension methods: they are shown on the data types on which they can be applied.

I've asked because the VO style is to use functions, and maybe all of you prefer this style.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

what is the preferred way - functions/static methods or extension methods

Post by MathiasHakansson »

Would you use the prefix name (discussed earlier) both for extension methods and functions?

VO used usuals if the same function could be used with different data types. In .net you have the option to use overloading instead.

In .net if feels more natural to use extension methods.
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

what is the preferred way - functions/static methods or extension methods

Post by wriedmann »

Hi Mathias,

Would you use the prefix name (discussed earlier) both for extension methods and functions?
No, I would not use them.

VO used usuals if the same function could be used with different data types. In .net you have the option to use overloading instead.
we are in the .NET world, so I would use overloading (which is a great thing IMHO).

In .net if feels more natural to use extension methods.
Thank you for your comment!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

what is the preferred way - functions/static methods or extension methods

Post by Otto »

I prefer (extension) methods over functions in an object oriented environment.

If you go to a functional programming style/language, it becomes somewhat different.
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

what is the preferred way - functions/static methods or extension methods

Post by wriedmann »

Hi Otto,

thank you! So we are currently at 2:0 for extension methods (if I don't count the answer from Karl-Heinz).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Guy Deprez

what is the preferred way - functions/static methods or extension methods

Post by Guy Deprez »

Extension methods.

I prefer encapsulate fonctionality around an object. It seems to me more "accurate" when the methods are attached to a clearly defined class, when they make part of a structured set.

New Score is 3:0

Guy
User avatar
Chris
Posts: 4981
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

what is the preferred way - functions/static methods or extension methods

Post by Chris »

I do not have a strong preference, but since the score goes too high and I usually support the losing team :), I'll mention that I usually prefer using a static method of a "Funcs" class (yes, similarly to what an extension method is under the hood) for that, because sometimes when I read some code that calls an (extension) method that I am not aware of, I am left wondering what that is.

I prefer things to be obvious how they are working, rather than being hidden behind implementation details, so I usually avoid extension methods. I understand they are also a nice thing, too, though, so I am not against using them either.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

what is the preferred way - functions/static methods or extension methods

Post by wriedmann »

Hi Chris,

one could not say that the members of the devteam may have another weight. If we attribute then 5 points to you, you would be on the winners side <g>.

But for now I would let it to 3:1 for extension methods.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply