Replies: 4 comments 4 replies
-
Actually my example ended a bit unfortunate, as |
Beta Was this translation helpful? Give feedback.
-
I can at most only speculate that the reason this is not the case is to make these methods much clearer to beginner users. A long text of methods that do not imply what is actually being returned can be somewhat tricky. It may also be some sort of performance benefit(?). Chaining can be easily done with Strings, but those are immutable, and a whole different String is returned when operating on them. To be honest, I find the inability to chain these methods to be most apparent during initialization. I often want to resize Arrays after defining it to avoid appending later, or storing a slight variation of the same array, and that may require just a few extra more lines that it feels necessary. |
Beta Was this translation helpful? Give feedback.
-
Using in-place methods that modify the array can be preferable for performance reasons, especially with larger arrays. |
Beta Was this translation helpful? Give feedback.
-
Intuition is not build on nothing. Yes, some of it is knowledge transfer from other programming languagess. However, you should also consider natural languages. If I see |
Beta Was this translation helpful? Give feedback.
-
Change Array's
append
,append_array
,duplicate
,erase
,insert
,invert
,push_back
,push_front
,remove
,resize
,shuffle
,sort
, andsort_custom
methods to return the Array itself. That would allow chaining methods when e.g. a sorted copy of the original array is needed:instead of having to do this:
Code would be shorter and in my opinion, also much clearer. In the current way, the first line is a bit misleading,
sorted_arr
is not sorted, it is just a duplicate of the original array. It will be sorted only later.The downside might be that the programmer then expects that some of those methods return a copy of the original array, but I guess that most programmers would find this proposed way more intuitive. This is also how e.g. Javascript works, so I guess this is how most programmers with experience with other languages would expect these methods to work.
Beta Was this translation helpful? Give feedback.
All reactions