Wish optional argument and default values

Discussion in 'B4R Bugs & Wishlist' started by Mostez, Jun 4, 2018.

  1. Mostez

    Mostez Active Member

    it will be great to see default-value and optional-arguments options in next release like

    Private Sub DoSomething (X as byte, optional Y as boolean = false)
  2. MathiasM

    MathiasM Member Licensed User

    I highly support this feature request!
  3. AnandGupta

    AnandGupta Active Member Licensed User

    Agree. It will be in tune with other programming language I use.


    Chris Tyrie likes this.
  4. AHilton

    AHilton Active Member Licensed User

    Use a single map type for your subs' parameter, instead. Far more versatile.
  5. MathiasM

    MathiasM Member Licensed User

    Versatility: A lot, readability: none.

    Especially in (small) teams that would be very hard to maintain.
  6. AHilton

    AHilton Active Member Licensed User

    That's why you use comments just above the "Sub" code line to document the parameters that shows up with Erel's 'intellisense' if it's really needed for new programmers.

    The revolving team of developers we use both in-house and contract don't seem to have a problem with it. Those that know the code don't need the "document by parameter" approach and those that are new to the code (or have been away from it for awhile), know that there's far more (and better) documentation about those parameters just above the sub (and, of course, in the 'intellisense" popup).
  7. MathiasM

    MathiasM Member Licensed User

    You're right, those documentation lines above a Sub are very nice.

    Well, it's maybe a habit or something. One way or another, it feels 'wrong' for me to do that, because I wouldn't be able to understand those parameters without reading docs, while that is possible with parameters with good naming.

    But hey, there is nothing wrong with different styles. And your way of working is indeed a way to get optional parameters.
  8. LucaMs

    LucaMs Expert Licensed User

    I fully agree that optional parameters and default values would be very useful; but I know that this thing has been asked several times and so I suppose that implementing it is a problem.
  9. Sandman

    Sandman Well-Known Member Licensed User

    My best guess is that it's not a feature aligned with Erel's vision for B4X. I don't think I've ever read the vision anywhere, but from what I gather when I read between the lines, he's very focused on keeping B4X approachable for all users (which is a great focus).

    And this obviously also include those that are quite new to programming, which means that more advanced concepts just aren't in scope for B4X. I don't have a link handy, but I vaguely remember a discussion about implementing support for i++ and i--, which was pretty much shot down. And I also doubt the wish of this thread is aligned with the vision.

    And just to be super clear: This is all based on my amateurish interpretation of posts in the forum, don't take it any more serious than a mere guess. :)
  10. LucaMs

    LucaMs Expert Licensed User

    Yes, not complicating things is a goal, but in this case there would be nothing complicated for any developer.
    It is probably a technical complication (the implementation) or maybe a backward compatibility problem.

    I have a feeling that we will know very soon :)
  11. AnandGupta

    AnandGupta Active Member Licensed User

    Let put a scenario for the very concept of having default parameter value of a function.

    I created a function/sub 'ABox' with parameters 'row, col, width, height and fill color'.
    Now I used this sub in many places in my project with these parameters. So far so good.

    Then I decided to have 'bordercolor' parameters. Changing the function/sub is no problem. But now I must change all the used places for this new parameter.
    If I can make a default value for boardercolor, it will not break my code.

    I do not know if I can do this in B4A using some code trick, but will be happy to get a code to set default value for missing parameter.


  12. Erel

    Erel Administrator Staff Member Licensed User

    Not really a trick but the way to do it is by adding another sub with the additional parameter:
    Public Sub DrawText(Text As String)
    End Sub

    'or make it DrawText2
    Public Sub DrawColoredText(Text As String, Color As Int)
    End Sub
    There are advantages for writing it like this and in the long run it is probably easier to maintain than maintaining code with multiple optional arguments.

    BTW, I don't have anything against this feature and it could be added at some point.
    Generally speaking a programming language is not a bag of random features and the more features you add the less accessible the programming language becomes.
  13. AnandGupta

    AnandGupta Active Member Licensed User

    Agree. Also agree that all programming languages has some difference from one another. Seeking similar features in all is wrong, but as a developer we are lazy and tend to like the feature which helps us in doing more work in less code, like 'i++'.

    Nevertheless, B4A has more or less robust features and I can adapt myself as I develop in many different programming languages as per my need.


  14. LucaMs

    LucaMs Expert Licensed User

    I can agree only if the implementation of some features is very difficult.

    I don't think having more features in your bag is a problem, some of you (us) won't use them.
    Having a toolbox with 300 tools is better than having only 100.

    Furthermore, some things, such as inheritance, for example, would give b4a a more professional "appearance".

    My opinion remains that implementing certain things is very complicated, especially a posteriori (retrofitting?), otherwise I would strongly suggest doing so.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice