Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC:

what is the preferred way - functions/static methods or extension methods 10 Sep 2018 18:37 #6166

  • Karl-Heinz
  • Karl-Heinz's Avatar


  • Posts: 774
  • 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.

    what is the preferred way - functions/static methods or extension methods 10 Sep 2018 19:30 #6168

    • Guy Deprez
    • Guy Deprez's Avatar



    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.

    what is the preferred way - functions/static methods or extension methods 10 Sep 2018 23:52 #6176

    • ic2
    • ic2's Avatar


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

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

    Dick

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

    Last edit: by ic2.

    what is the preferred way - functions/static methods or extension methods 11 Sep 2018 05:19 #6179

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3366
  • 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

    www.riedmann.it - docs.xsharp.it

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

    what is the preferred way - functions/static methods or extension methods 11 Sep 2018 09:48 #6182

    • Otto
    • Otto's Avatar


  • Posts: 174
  • 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.

    what is the preferred way - functions/static methods or extension methods 11 Sep 2018 10:52 #6183

    • SHirsch
    • SHirsch's Avatar


  • Posts: 279
  • My point goes to methods.

    Stefan

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

    what is the preferred way - functions/static methods or extension methods 11 Sep 2018 10:56 #6184

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3366
  • Hi Stefan,

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

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    what is the preferred way - functions/static methods or extension methods 11 Sep 2018 14:47 #6185

    • Chris
    • Chris's Avatar


  • Posts: 3981
  • 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.

    what is the preferred way - functions/static methods or extension methods 13 Sep 2018 15:38 #6208

    • Guy Deprez
    • Guy Deprez's Avatar



    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.

    what is the preferred way - functions/static methods or extension methods 14 Sep 2018 08:26 #6218

    • Jamal
    • Jamal's Avatar


  • Posts: 309
  • 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

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

    Last edit: by Jamal.

    what is the preferred way - functions/static methods or extension methods 14 Sep 2018 08:34 #6219

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3366
  • 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
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    what is the preferred way - functions/static methods or extension methods 13 Oct 2018 14:06 #6544

    • Horst
    • Horst's Avatar


  • Posts: 293
  • 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.

    what is the preferred way - functions/static methods or extension methods 13 Oct 2018 18:26 #6545

    • wriedmann
    • wriedmann's Avatar
    • Topic Author


  • Posts: 3366
  • 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
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

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

    what is the preferred way - functions/static methods or extension methods 14 Oct 2018 18:34 #6548

    • Fab64
    • Fab64's Avatar


  • Posts: 63
  • also for me extension method

    Fabrizio

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

    • Page:
    • 1
    • 2
    Moderators: wriedmann