xsharp.eu • what is the preferred way - functions/static methods or extension methods
Page 1 of 4

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

Posted: Sat Sep 08, 2018 4:37 pm
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

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

Posted: Sun Sep 09, 2018 7:42 am
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

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

Posted: Mon Sep 10, 2018 4:12 am
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

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

Posted: Mon Sep 10, 2018 9:09 am
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.

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

Posted: Mon Sep 10, 2018 9:19 am
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

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

Posted: Mon Sep 10, 2018 9:30 am
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.

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

Posted: Mon Sep 10, 2018 9:34 am
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

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

Posted: Mon Sep 10, 2018 10:43 am
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

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

Posted: Mon Sep 10, 2018 11:31 am
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.

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

Posted: Mon Sep 10, 2018 11:39 am
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