AdvancedExpressionFolding icon indicating copy to clipboard operation
AdvancedExpressionFolding copied to clipboard

Fold all 0-based, cardinal array accesses into 1-based, ordinal function calls

Open ciscorucinski opened this issue 7 years ago • 4 comments

After some thought with issue #72 , I wonder if there is a better solution that would fit every code instance of a direct array access.

Instead of folding into .first(), with the auxiliary fold of .last(), you can provide a folding for each and every possible direct array access; something that no other language has done, and should help all types of developers prevent simple off-by-one issues. The conversion from zero-based array access to one-based mental thought is a big issue for newer programmers, and I am certain it has occasionally hit the experienced programmer a few times.

Instead of folding array[0] into array.first(), it should be folded into array.1st(). This should work even though functions cannot start with numbers as this is just a fold. Note: .last() should still work

array[0] --> array.1st() array[1] --> array.2nd() array[2] --> array.3rd() array[3] --> array.4th() array[4] --> array.5th() array[10] --> array.11th() array[999] --> array.1000th() array[1000000] --> array.1_000_001st()

Most of the folded direct array access will be a # + "th". The only time it will not be that is when the one's position of the array is a 0, 1, or 2 (zero based). If this is true, then the folded text will be _1 + st, _2 + nd, or _3 + rd ...

1st, 101st, 201st, 521st

...Except when the ten's place is a 1, then the folded text will be _11th, _12th, or _13th

11th, 111th, 211th, 511th, 9411th

ciscorucinski avatar Jun 28 '17 09:06 ciscorucinski

Or, instead of folding it into a faux function call, it could be folding into a faux variable access

array[0] --> array.1st

ciscorucinski avatar Jun 28 '17 09:06 ciscorucinski

https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)#Array_system_cross-reference_list Of all the 1-based arrays, the ones IDEA supports are CFML, Sass, and XPath/XQuery, unless there are some plugins not listed on Jetbrains registry or that are not obviously named. Every other language is zero-based. 1-based arrays are just not a thing. If this is implemented, there needs to be an option specifically to turn this off.

petteyg avatar Jun 28 '17 16:06 petteyg

@petteyg this plugin already has a plethora of options to enable and disable. In fact, there already is an option to disable the .first() folding. My idea would simply extend that type of folding to more candidate code snippets.

Also, this isn't converting it to 1—based array access notation. It still is 0-based array access. All this is doing is saying...

Hey I want the 1st element in my array. Hey I want the 10th element in my array. Rather than array[0] and array[9].

When you push your code to Github when array[0] is folded to array.1st(), all that Github and your coworkers see is array[0]

Also, when you know you want to get the first element in the array, but it was folded into array.2nd(), then you know your code is automatically wrong without running it. You know you accidentally wrote a 1 instead of a 0.

ciscorucinski avatar Jun 28 '17 16:06 ciscorucinski

There is an option, yes, but it's combined with other folds that I do want to keep on. I should emphasize "specifically" in my previous comment. I like the put/set(0, val) to [0] = val folds, but I don't like the idea of the number 0 visually changing to 1. So I say if this is implemented, I'd really like an option to turn off this single specific piece (and the same for the other issue #72 of [0] changing to first()).

petteyg avatar Jun 28 '17 19:06 petteyg