Tuesday 18 November 2014

What should CFML's deleteAt() method return?

G'day:
This will be quick, as I'm out of time before I'm due to start work.

As I mentioned in my earlier article ("Weekend quiz: my answer (CFML version)"), Railo's (and ColdFusion's for that matter) Array.deleteAt() method returns a pointless boolean, rather than something useful. What do you think it should return?



All the other similar array methods - insertAt(), append(), etc - return the updated array. This means calls can be chained:

sumOfUpdatedArray = someArray.append(newValue).sum()

However one cannot do this:

sumOfUpdatedArray = someArray.deleteAt(n).sum()

I needed to do this yesterday.

I raised a ticket with Railo accordingly: "Array.deleteAt() should return the array" (RAILO-3250).

Igal came back with a valid point:

I disagree. if anything it should return the deleted value. that is the standard in many programming languages.
Having been prompted to think about it some more, I thought this:

I could see a case for that. But there are some considerations either way.

For:


  • there's a precedent set in Java with ArrayList's remove() method: it returns the removed object.
  • Ruby does the same with Array's delete_at()
  • JavaScript has similar methods such as shift() and splice() which return objects removed by the operation.

Against:


  • the prodedural functions in CFML such as arrayAppend(), arrayDeleteAt(), arrayInsertAt() all return a pointless boolean. All the other member function equivalents have been changed to return the whole array - except for deleteAt() - which is clearly (?) an oversight. I think the intent here was to change the boolean-returning behaviour in the procedural functions to resultant-array-returning behaviour for member functions, specifically so the member function calls can be chained.
  • personally I think the behaviour of the Java and Ruby methods are incorrect. By their name, they are element removing functions, not element extracting functions. Similarly CFML's deleteAt() method is for deleting. Not extracting and returning.
  • JavaScript gets it right in the methods are more reflective of their operation.
  • Note Java's ArrayList .removeAll() method does not return the removed elements; it returns a boolean. So Java's precedent is blurry here.


Partial:


  • there is no CFML function which does the equivalent of "get elements out of the array", and it would be convenient to have one. However I don't think this is the job of a delete() function; perhaps a splice() or extract() function is missing from the mix here.


I know three things:


  • the current behaviour is not useful
  • and is at odds with equivalent behaviour in the other member functions
  • what would be convenient for me would be if it returned the whole array, as that's what my current use case is. I could understand an additional extract() operation, but I don't need that behaviour, and CFML's never had it, so probably not in-scope for this ticket.

I was wondering what your thoughts were? Even if you're not a Railo user, there's an equivalent ticket for ColdFusion: 3844976.

Righto.

--
Adam