This is valid for many other functions.
get(array,key) or get(key,array) ?
find(array,value) or find(value,array) ?
Etc...
This is valid for many other functions.
Etc... |
|||
I always try and put the "most important" elements at the start, which would be the array in this case. I do this primarily so that my methods have more similar prototypes and I know that the first item in the method is going to be the array/list/etc. For example:
Is much nicer to me than:
An example recently where I've encountered this is when using EntityFramework I had the DataContext in inconsistent positions in the parameter list, this just involves a little more thought than having it as the first parameter all the time. A side note, if working in a language where you can, it may be nicer to put the method on the array.
|
|||
|
If your language uses currying, there is another view:
(I'll use Haskell for examples.) As an example, let's consider
This way, we can compose consecutive maps like
of type
would have been much less composable - try expressing the above example using This ideas can be seen in many many places. For example, inserting an element into a
which allows us to do things like So (in languages with currying) it's more convenient to have For |
|||
|
Youre question reminds a question I had on the difference between Both are made for adding elements to a container, but they have a lot of differences. I won't enter in the details of A difference is in the parameters order. While My conclusion would be: If you design your function to takes multiple arguments, put the new elements on the right. It's common idiom. When lazy, consider the legacy Update: As a general rule, when you don't know what to do for interface design, try to see how your language/standard library is designed. I don't for Javascript, but in C++ most of time it is carefully designed and choices are made for good reason. I think that is a good piece of advice to follow. |
||||
|
Long story short, it's a lot easier to remember -and more logical IMO- if one can read
instead of
and that still go with
as mentioned by Tyriar in first answer like in
so my answer is :
|
|||
|
push
returns a new array, instead of updating the existing one. Is this the case? – Joey Adams Feb 25 at 6:27