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

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 3 days ago #1 by wriedmann
Hi All,

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

The first would be called
aTarget := aSource:SubArray( nStart, nLength )
and the second
aTarget := SubArray( aSource, nStart, nLength )

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 week 3 days ago #2 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

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 2 days ago #3 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
nPos := aBuffer:IndexOfBytes( aPattern, nOffset )
IMHO is easier to understand than
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

Please Log in or Create an account to join the conversation.

More
1 week 2 days ago #4 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.

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 2 days ago - 1 week 2 days ago #5 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
Last edit: 1 week 2 days ago by wriedmann.

Please Log in or Create an account to join the conversation.

More
1 week 2 days ago - 1 week 2 days ago #6 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.
Last edit: 1 week 2 days ago by Otto. Reason: typo

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 2 days ago #7 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

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #8 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

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #9 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.

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 1 day ago #10 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

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #11 by Karl-Heinz
Guys,

I demand on being counted, so the current score is not 3:1 but 3:1,5 ;-)

BTW. thanks Chris :-)

regards
Karl-Heinz

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 1 day ago #12 by wriedmann
Hi Karl-Heinz,

sorry, then the current score is 3,5 : 1,5.

You were half for extension methods on classes and against on basic datatypes.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #13 by Karl-Heinz
yes, of course 3,5:1,5 :-)

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago - 1 week 1 day ago #14 by FFF
Guy,
that i see similiar, as long as you "extend" "your" class - but then, why not write the functionality as part of the class directly.
Wolfgang adds to existing, i.e. , "system classes/types" - here i'm with Chris, usually one knows more or less what to expect from e.g. a string object - and has to wonder, why on earth now there's new functionality; and not to forget: "where" is the definition?
Nothing really wrong with this, but make it a 3.5:2.25 ;)
Last edit: 1 week 1 day ago by FFF. Reason: typo

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 1 day ago #15 by wriedmann
Hi Karl,

and has to wonder, why on earth now there's new functionality; and not to forget: "where" is the definition?


this is also the question with all these functions.

An if I have to put one static class "Functions" or "Funcs" or whatever in the library: this one will be contain a lot of functions/static methods that are not related at all.

With extension methods, the source code can be much better organized - and it is less to write. And maybe the names could be shorter.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #16 by FFF
Wolfgang,
i don't quite see the difference, wethere you have a file/lib "MyFuncs", vs. "MyExtensions" ;)

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 1 day ago #17 by wriedmann
Hi Karl,

i don't quite see the difference, wethere you have a file/lib "MyFuncs", vs. "MyExtensions"


There is a class "StringExtensions", a class "ArrayExtensions", a class "ObjectExtensions" and so forth.

With extension methods code like this is possible:
if oObject:IsProperty( cPropertyName )
and this works on every object.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #18 by Guy Deprez
Extensions methods VS Functions for existing classes. Ex:String

LOCAL cString:="xTE12y45ST " as STRING

//Using Functions. The "Russian dolls" model

cString:= ToUpper(ExtractLetters(left(strTran(AllTrim(cString),"x","a"),6)))

//Using Extensions Methods. The "Chaining model"

String:=cString:Trim().Replace("x","a").Left(6).ExtractLetters().ToUpper()

Result: "ATEY"

Witch style/model do you prefer?
Guy

Please Log in or Create an account to join the conversation.

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 week 1 day ago #19 by wriedmann
Hi Guy,

IMHO such a "sausage" of code is very bad coding style - impossible to understand and impossible to debug, and error prone.

But with extension methods is is a lot easier to read than with the "russian dolls" model.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #20 by FFF
Guys,
this results in left to right vs. right to left reading - to "like" is neither.
If i' d need that more than once, i'd write an access or a func, properly named ;)
See my request to Robert for a switch to access Dataserverfields "cleaned"...

Karl

Please Log in or Create an account to join the conversation.

Moderators: wriedmann