fbpx

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

More
3 months 1 day ago #21 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

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

More
3 months 1 day ago #22 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

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

More
3 months 1 day ago - 3 months 1 day ago #23 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
Last edit: 3 months 1 day ago by ic2.

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

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
3 months 1 day ago #24 by wriedmann
Hi Dick,

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

Wolfgang

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

More
3 months 1 day ago #25 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.

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

More
3 months 1 day ago #26 by SHirsch
My point goes to methods.

Stefan

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

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
3 months 1 day ago #27 by wriedmann
Hi Stefan,

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

Wolfgang

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

More
3 months 1 day ago #28 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

XSharp Development Team
chris(at)xsharp.eu

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

More
2 months 4 weeks ago #29 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

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

More
2 months 4 weeks ago - 2 months 4 weeks ago #30 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
Last edit: 2 months 4 weeks ago by Jamal.

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

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
2 months 4 weeks ago #31 by wriedmann
Hi Jamal,

thank you - so we are at 5.5 : 3.25 now.

Regarding your comment about dot or colon: in the Core dialect you can use the dot instead of the colon. But at least XIDE does not support that in thew editor, I don't know about Visual Studio.

Personally I prefer the colon because it makes it clearer where a method comes from - it is easier to read.
It seems to be a similar question as "implements" and "inherit" - X# makes a difference and the code easier to read, whereas C# is shorter, but IMHO harder to read.

Wolfgang

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

More
1 month 4 weeks ago #32 by Horst
Hi
i like functions, i dont like when a variable has addional features.
a varibale is a variable and finish. keep it simple.

so this -> cString := cMotherString.Substr (1,5) i dont like.

and btw i like the russian doll modell ;-)because its like mathematik first clean up the inner clips.

also in VO, all the methods of a class will be compiled and blows up the executable, even you need only one method of the class in the whole program.

Horst

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

  • wriedmann
  • wriedmann's Avatar Topic Author
  • Away
More
1 month 4 weeks ago #33 by wriedmann
Hi Horst,

a varibale is a variable and finish. keep it simple.


a variable can be an object - in fact in .NET everything is an object, even a string or an integer. And an object can have methods.

Therefore I like extension methods, as they allow to use the same method name on different object or variable types.

Wolfgang

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

More
1 month 3 weeks ago #34 by Fab64
also for me extension method

Fabrizio

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

Moderators: wriedmann