Parameters of routines

LucaMs

Expert
Licensed User
Longtime User
I would like to recommend using Lists instead of Arrays as parameters in your routines "when possible" (it's always possible).

B4X:
Sub MyRoutineWithList(lstValues As List)
End Sub

instead of:
B4X:
Sub MyRoutineWithArray(Values() As Int)
End Sub


The reason is that this way you can call:
B4X:
MyRoutineWithList(Array As Int(1,2,3))
MyRoutineWithList(lstData)

but you cannot call:
B4X:
MyRoutineWithArray(lstData)
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
If it works for you then why not? It would be interesting to know if there's any difference in overhead between using an array & using a list though. Not just in the passing of it, but also accessing & other manipulating.

- Colin.
 

LucaMs

Expert
Licensed User
Longtime User
It would be interesting to know if there's any difference in overhead between using an array & using a list though. Not just in the passing of it, but also accessing & other manipulating.
This is very likely, as you will often have to cast.

Furthermore, there are also other negative aspects:
1) if you pass a List it might contain any type of variable, even a mix of types;
2) you should add a comment that indicates the type of data that the list should contain.

However, it also has positive aspects.
 

KMatle

Expert
Licensed User
Longtime User
I'm using lists (with maps) for all of my data transfer even for subs (except it's just a single value). Lists with or without maps are arrays, too which can easily be transformed into JSON and then back to arrays in php or any other language.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
That's true. Lists should be preferred in most cases over arrays.
If I remember correctly I'm talking about it in the collections video tutorial.

The overhead of lists over arrays is very small and unlikely to make any difference.

One exception is arrays of bytes. In that case you can use BytesBuilder as an alternative to a list.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
That's true. Lists should be preferred in most cases over arrays.
If I remember correctly I'm talking about it in the collections video tutorial.

The overhead of lists over arrays is very small and unlikely to make any difference.

One exception is arrays of bytes. In that case you can use BytesBuilder as an alternative to a list.
Good to know. I guess through other languages I've gotten into the habit of using arrays, so I'll have to try to break it & start using lists instead.

- Colin.
 
Top