Wish Sub argument variable repeated in body of the Sub

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Possibly this could/should be a warning in the IDE as it can cause a bug that can be hard to spot.

For example we have this code:

B4X:
Sub AddToListItems(lst As List, iAdd As Int) As List

    Dim i As Int
    Dim lst As List

    For i = 0 To lst.Size - 1
        lst.Set(i, lst.Get(i) + iAdd)
    Next
    
    Return lst

End Sub

This will give an error:
java.lang.RuntimeException: Object should first be initialized (List).

In this simple example the coding mistake will be easy to spot, but with more complex code it can be tricky.
Also, I think it makes no sense ever to code like this as the provided argument can never do anything useful.
So, maybe there should be warning about this in the IDE.

RBS
 

Sandman

Expert
Licensed User
Longtime User
It doesn't take anything away from your argument, but to minimize the risk for this I've taken the standard of naming the incoming values with the prefix "in". Seems to help me remember what's what

B4X:
Sub AddToListItems(inList As List, inAdd As Int) As List

    ...

End Sub
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Good idea, but I think (hope?) I have learned my lesson now.

RBS
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…