Results 1 to 18 of 18

Thread: [RESOLVED] Interesting code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Resolved [RESOLVED] Interesting code

    We often write code like this:
    Code:
    IO.puts(tabularize(to_map(Store.get_host(host))))
    
    '--- or ---
    
    list_data = Store.get_host(host)
    map = to_map(list)
    formatted_output = tabularize(map)
    IO.puts(formatted_output)
    Such code can be written in Elixir as:
    Code:
    host
    |> Store.get_host
    |> to_map
    |> tabularize
    |> IO.puts

  2. #2
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    Builder pattern. Easy to implement in VB6. Just return self in all subs (transformed to functions).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: Interesting code

    Quote Originally Posted by peterst View Post
    Builder pattern. Easy to implement in VB6. Just return self in all subs (transformed to functions).
    Please write a VB6 example. Thanks.

  4. #4
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    Create class BuilderExample. Put some code like this (manually written code shown):

    Code:
    private m_sFirstName as string
    private m_sLastName as string
    private m_nAge as Integer
    
    public function SetFirstName(byval s as string) as BuilderExample
        m_sFirstName = s
        set SetFirstName= Me
    end function
    public function SetLastName(byval s as string) as BuilderExample
        m_sLastName = s
        set SetLastName= Me
    end function
    public function SetAge(byval n as integer) as BuilderExample
        m_nAge = n
        set SetAge = Me
    end function
    public function ToLower() as BuilderExample
        m_sFirstName = LCase(m_sFirstName)
        m_sLastName = LCase(m_sLastName)
        set ToLower = Me
    end function
    
    public function GetPersonInfo() as string
        GetPersonInfo = m_sFirstName & " " & m_sLastName & ", Age: " & m_nAge
    end function
    Code usage:
    Code:
    dim person as New BuilderExample
    person. _
        SetFIrstName("JOHN"). _
        SetLastName("Smith"). _
        ToLower. _
        SetAge(55)
    debug.print person.GetPersonInfo
    Result will be:
    Code:
    john smith, Age: 55
    Last edited by peterst; Jun 22nd, 2021 at 03:12 PM.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    Builder pattern can be used wrongly quite easy and things can go wrong if blindly used.

    Code:
    person.SetFirstName("John").ToLower().SetLastName("Smith").ToLower().SetFirstName("Jane")
    Above examples are just replacing normal setters to have code look cool.

    If there is some logic like sorting and filtering data and trying to achieve "immutability" it can become "funny" to debug if it is badly written.

    With some languages it is much easier to write "builder" style code. Also using extensions to objects allows adding functionality that doesn't exist in the original library and still achieve the cool looking code :-)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: Interesting code

    Hi peterst, thank you for your example. Your example shows "Object Chaining" (Object Method Chaining), which is a builder design pattern.

    But have you noticed that your example is very different from the Elixir code I've shown:
    (1) Elixir allows multiple different objects in the chain (Elixir calls it pipe)
    (2) Elixir's piple can be applied to any function
    (3) Elixir's piple does not require you to change the software structure (builder design pattern requires us to make a thorough change to the software structure)
    (4) Elixir's piple is more concise and intuitive, and is more in line with programmers' thinking mode.
    (5) Elixir's piple is a system syntax/grammatical level support, which not only reduces the amount of code, but also provides a more code reading experience.

    Maybe the following syntax is better:
    Code:
    host
    -> Store.get_host
    -> to_map
    -> tabularize
    -> IO.puts
    
    '--- OR ---
    
    host -> Store.get_host -> to_map -> tabularize -> IO.puts
    Last edited by SearchingDataOnly; Jun 22nd, 2021 at 10:03 PM.

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: Interesting code

    What is your point? Semantics?

  8. #8
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    SearchingDataOnly, I gave you only example how to write such code. You can return in function not self but different class so you can create the "pipe" with different classes as input and output. Think of class Products that can return collection of products that you can sort in the "pipe" and then create tabularized string as final output. Or ProductrsBOM which will return collection of products (to calculate bill of materials) and perform SUM on field in the collection (returns floating point number) and finally format that number for the desired output as string.

    At the time of VB6 there were not designed lots of language features that are available now and allow extensions to existing classes, lambda functions and others, where you can much easier create missing "piping" functions. You have to create proxy classes with proper design to allow such functionality, which IMO requires more attention and time compared to classic objects design.

    Another intent of chaining/piping is immutability of data. Functions will not return self but new object that will contain the result so the input will be not modified.

    Something useful with such chains is to avoid creation of temp variables where to keep the result if it is done using separate operations. The intermediate objects will be discarded after the operation is completed.

    But I still think that forcing the use of patterns that are not suitable for the case but just look "cool" is waste of resources and leads to mistakes.

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,560

    Re: Interesting code

    I've started to despise threads like this.

    It's like demanding using chinese grammar with english language
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  10. #10
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: Interesting code

    Code:
    host -> Store.get_host -> to_map -> tabularize -> IO.puts
    
    OR
    
    IO.puts(tabularize(to_map(Store.get_host(host))))
    Except for the brackets, I don't see much difference..
    It's just what you are used to.
    In a first glance I don't see missing functionality

  11. #11
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    Quote Originally Posted by Arnoutdv View Post
    Except for the brackets, I don't see much difference..
    It's just what you are used to.
    In a first glance I don't see missing functionality
    It doesn't look cool :-)

    C# Code:
    1. var endpoints = configuration.GetSection("HttpServer:Endpoints")
    2.     .GetChildren()
    3.     .ToDictionary(section => section.Key, section =>
    4.     {
    5.         var endpoint = new EndpointConfiguration();
    6.         section.Bind(endpoint);
    7.         return endpoint;
    8.     });

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: Interesting code

    Quote Originally Posted by Arnoutdv View Post
    What is your point? Semantics?
    My point is: Maybe we can add a similar syntactic sugar to VBScript.Next.

    Quote Originally Posted by peterst View Post
    SearchingDataOnly, I gave you only example how to write such code. You can return in function not self but different class so you can create the "pipe" with different classes as input and output. Think of class Products that can return collection of products that you can sort in the "pipe" and then create tabularized string as final output. Or ProductrsBOM which will return collection of products (to calculate bill of materials) and perform SUM on field in the collection (returns floating point number) and finally format that number for the desired output as string.

    At the time of VB6 there were not designed lots of language features that are available now and allow extensions to existing classes, lambda functions and others, where you can much easier create missing "piping" functions. You have to create proxy classes with proper design to allow such functionality, which IMO requires more attention and time compared to classic objects design.

    Another intent of chaining/piping is immutability of data. Functions will not return self but new object that will contain the result so the input will be not modified.

    Something useful with such chains is to avoid creation of temp variables where to keep the result if it is done using separate operations. The intermediate objects will be discarded after the operation is completed.

    But I still think that forcing the use of patterns that are not suitable for the case but just look "cool" is waste of resources and leads to mistakes.
    I haven't thought about implementing a similar grammar in VB6, but some new languages related to VB6 may consider using such a grammar. It is just a preliminary discussion.
    Last edited by SearchingDataOnly; Jun 23rd, 2021 at 03:02 AM.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: Interesting code

    Quote Originally Posted by Arnoutdv View Post
    Except for the brackets, I don't see much difference..
    It's just what you are used to.
    In a first glance I don't see missing functionality
    No, there is a huge difference.

    Quote Originally Posted by peterst View Post
    It doesn't look cool :-)

    C# Code:
    1. var endpoints = configuration.GetSection("HttpServer:Endpoints")
    2.     .GetChildren()
    3.     .ToDictionary(section => section.Key, section =>
    4.     {
    5.         var endpoint = new EndpointConfiguration();
    6.         section.Bind(endpoint);
    7.         return endpoint;
    8.     });
    Obviously, the code above is not as cool as Elixir's
    Last edited by SearchingDataOnly; Jun 23rd, 2021 at 03:06 AM.

  14. #14
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    326

    Re: Interesting code

    Quote Originally Posted by SearchingDataOnly View Post
    Obviously, the code above is not as cool as Elixir's
    Obviously, if I wrote JavaScript (as it will look the same or quite similar) instead of something from .NET world like C#, it would be very very cool.

    And more obviously, talking about JavaScript, TypeScript (OMG! another Microsoft child), Elixir or anything else but not VB6 in the VB6 subforum, is just totally offtopic spam.

  15. #15
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: Interesting code

    Who do you mean with "We" and what is VBScript.Next?

    If you want a different syntax why not switch to a different programming language?
    If you like Pascal syntax use Pascal, if you like C++ use C++.
    I really don't understand the need to try to make VB (Next??) implement all kind of different syntaxes

  16. #16
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,560

    Re: Interesting code

    Quote Originally Posted by Arnoutdv View Post
    I really don't understand the need to try to make VB (Next??) implement all kind of different syntaxes
    Amen, brother!
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  17. #17
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,274

    Re: [RESOLVED] Interesting code

    I think it is very simple to do that in VB6.

    Just have a module level (or global) Variant variable from where is taken the input parameter for every function (they will be subs) and store the result of the function in the same variable.

    But that is like going back 30 years to QBasic, COBOL or whatever.
    Last edited by Eduardo-; Jun 23rd, 2021 at 06:29 AM.

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,317

    Re: [RESOLVED] Interesting code

    Interesting discussion, but it doesn't quite belong in the VB6 forum. I've moved it to the General Developer Forum.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width