Page 3 of 4
what is the preferred way - functions/static methods or extension methods
Posted: Mon Sep 10, 2018 4:37 pm
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
what is the preferred way - functions/static methods or extension methods
Posted: Mon Sep 10, 2018 5:30 pm
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
what is the preferred way - functions/static methods or extension methods
Posted: Mon Sep 10, 2018 9:52 pm
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
what is the preferred way - functions/static methods or extension methods
Posted: Tue Sep 11, 2018 3:19 am
by wriedmann
Hi Dick,
thanks, so now we are at 3,5 : 3,25 - the extension methods are slightly preferred.
Wolfgang
what is the preferred way - functions/static methods or extension methods
Posted: Tue Sep 11, 2018 7:48 am
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.
what is the preferred way - functions/static methods or extension methods
Posted: Tue Sep 11, 2018 8:52 am
by SHirsch
My point goes to methods.
Stefan
what is the preferred way - functions/static methods or extension methods
Posted: Tue Sep 11, 2018 8:56 am
by wriedmann
Hi Stefan,
thank you - new score is 4,5 : 3,25 for extension methods.
Wolfgang
what is the preferred way - functions/static methods or extension methods
Posted: Tue Sep 11, 2018 12:47 pm
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
what is the preferred way - functions/static methods or extension methods
Posted: Thu Sep 13, 2018 1:38 pm
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
what is the preferred way - functions/static methods or extension methods
Posted: Fri Sep 14, 2018 6:26 am
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