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

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 »

FFF wrote: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.
Hi Karl,

not sure i understand you correctly.

It doesn´t make sense to use "extension methods" to enhance "self" written classes. Consider the following sample:
I´m using enhanced VO Checkbox and Radiobutton classes. Both classes use e.g. the same algorithm to calc rectangles. So the x# "extension method" - the "anchor" class is BUTTON - looks like:

STATIC METHOD CalcTextAndFocusRectangle ( SELF o AS BUTTON , hDC AS PTR , pszCaptionText AS PSZ , struRectText AS _winRECT , struRectFocus AS _winRECT , lMultiLine AS LOGIC ,lCalcFocusRectangle AS LOGIC ) AS VOID PASCAL
...
RETURN

within my Radiobutton and Checkbox classes this method is called like any other method.

self:CalcTextAndFocusRectangle( hDC , ...)

All calculations are done with the given params. So there´s no need to get direct access to any Checkbox or RadioButton specific iVars, which wouldn ´t be possible at all from within such a "extension method"


regards
Karl-Heinz
Guy Deprez

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

Post by Guy Deprez »

Don't worry, guys...It's only an "dummy" example and It's willingly done to accentuate the difference between the styles..:cheer:
It's also an example that shows how easily the extensions methods are integrated with the existing methods

Wolfgang,
such a chain is not a problem for the debugger. It points correctly the errors if an error occurs. I don't know about the functions model.. If you forget a coma or a braket somewhere, good luck :)


Karl,
The chaining model is better to read and write. The parameters are allways defined closed to the method name (ex: left method vs left function). No extra parameter such a string for instance;)

If the Russian dolls model seems or is more complex, it is only because the string must be passed to the functions as parameter. It's the heart of the problem.
Guy
ic2
Posts: 1802
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

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

Post by ic2 »

Definitely functions. They are much more readable & compatible with what we did in VO.

Except in the Russian doll sample, concerning readability :unsure:

Dick
User avatar
wriedmann
Posts: 3655
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 Dick,

thanks, so now we are at 3,5 : 3,25 - the extension methods are slightly preferred.

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 »

Karl-Heinz wrote:It doesn´t make sense to use "extension methods" to enhance "self" written classes.
If you have DTO you can extend it with your extension methods.
You could add this functionality to the class itself, but then it wouldn't be a DTO anymore.

Some additional things to consider:

Finding the right (existing) functions was always a difficult task. I know for sure I have made my own functions, discovering equivalent functionality in the runtime of VO, just because I didn't knew it existed or how to find it.

With an extension method, you type the . or : after the object and intellisense gives you the list of known methods.

Naming functions is also (with no overloading) a Hungarian task. Aadd, the first A just to denote it is a function associated to an array.

With an extension method it is just an Add(...)

If you don't think the functionality can be an extension method, you probably must re-evaluatie it and see, how to cut it into pieces, create another type of class (like a provider, formatter, e.g.).
How many times have you written a function, just because you need something, and just put all you needed in one function, not thinking about the content etc? (I have done that a lot...)
Thinking in classes and methods makes you think better about the SOLID principles.

That's why I prefer methods.
User avatar
SHirsch
Posts: 282
Joined: Tue Jan 30, 2018 8:23 am

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

Post by SHirsch »

My point goes to methods.

Stefan
User avatar
wriedmann
Posts: 3655
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 Stefan,

thank you - new score is 4,5 : 3,25 for extension methods.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4583
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

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

Post by Chris »

Otto wrote: Finding the right (existing) functions was always a difficult task. I know for sure I have made my own functions, discovering equivalent functionality in the runtime of VO, just because I didn't knew it existed or how to find it.

With an extension method, you type the . or : after the object and intellisense gives you the list of known methods.

Naming functions is also (with no overloading) a Hungarian task. Aadd, the first A just to denote it is a function associated to an array.

With an extension method it is just an Add(...)
Right, that's why I suggested static methods and not functions. AAdd() is IMO indeed not very good, but ArrayFuncs.Add() is intuitive, easy to read, easy to understand what it really means under the hood and intellisense works with it fine. Again, not to say I am necessarily against extension methods, just mentioning there's also a good alternative.

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Guy Deprez

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

Post by Guy Deprez »

You have not voted? Don't forget to do it. This topic is all about the fundations of the future tools libs. Maybe you don't have a "strongly typed" preference? In this case, giving 1 point on both sides will also make a difference: it will be appreciated...;)

Guy
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

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

Post by Jamal »

Since I have been developing in .NET (C# and VB.NET) for the over 15 years, I prefer extension methods over functions, since they're more object oriented and easy to figure out with intelliSense.

I know in VO and X# you need to to use the colon : instead the a dot. Sometimes, i forget myself I start typing a dot in VO and the same goes for X#. Arrrgh!!
I've heard so many arguments about the pros and cons of each, but you should consider another question:

Do you prefer to use the dot or colon?

oSomeClass:DoSomething()

or

oSomeClass.DoSomething()

May be a switch in the project can implemented to use a . or :
Is it hard to do? Implementing the dot may attract C# or VB.NET developers or is that out of target?

Jamal
Post Reply