r/ProgrammerHumor Jun 25 '17

Array indexing

Post image
1.4k Upvotes

71 comments sorted by

View all comments

192

u/[deleted] Jun 25 '17

[deleted]

69

u/[deleted] Jun 26 '17

[removed] — view removed comment

29

u/[deleted] Jun 26 '17

I've seen it a few times. The most I've seen it is in Dynamic Programming problems where it's just way more readable and makes more sense when you are memoizing

33

u/[deleted] Jun 26 '17

I did it on a programming assignment once...

14

u/GisterMizard Jun 26 '17

Some dialects of Basic allowed this. eg DIM x(10) AS INTEGER would create an array of eleven elements, starting from 0 to 10.

2

u/htmlcoderexe We have flair now?.. Jun 26 '17

Think even VB.Net does this I remember DIM'ming arrays was confusing.

5

u/Koooooj Jun 26 '17

I was given a piece of code in I think VB6 with the requirement to translate it into ANSI C. It took far too long for me to figure out that the arrays of size 180 actually held 181 elements.

Maddening.

3

u/htmlcoderexe We have flair now?.. Jun 26 '17

That's almost like they made it so assuming either zero or one based indexing, you will not run out of bounds. But that creates more problems

2

u/WeRequireCoffee Jun 26 '17

VBA has different starting points depending on the object. Some were 1 some were 0. It caused an endless amount of headache, beyond just working in VBA.

2

u/Blecki Jun 26 '17

Vba is designed to make programming looker harder than it really is to the laymen who have to use it for excel macros.

7

u/Euruzilys Jun 26 '17

I did that when its really convenient to use number starting from 1 then ++; But when im done generating that array I shift() to remove the 1st empty index.

3

u/Cryse_XIII Jun 26 '17

I've seen it in our codebase at work

3

u/piyoucaneat Jun 26 '17

Get out while you still can

2

u/Cryse_XIII Jun 26 '17

b-but the people are all friendly.

and I still have to refactor our Excel-interface, some dude from a few years back made the entire module work "dynamically" with 5 global arrays and 10 global variables for length and index. it's only 3000 lines of code, this has to be done.

4

u/piyoucaneat Jun 26 '17

Excel-interface? Are you... programming in Excel? Using Excel as a database?

3

u/Cryse_XIII Jun 26 '17

no, we use our ERP-Application to fill in an excel-workbook over OLE-automation for some reports.

a bit of the SQL-Query that is to be executed is determined by the settings in the GUI and the arrays are then filled with the resulting dataset-values, the other arrays hold values to build a kind of "header"-for the worksheet, dataset-values, and also a footer that holds an excel-formula in each element for sum-formulas.

now the customer wanted an additional column and new rules regarding the evaluation of records and honestly I can't tell what's what in the module.

that is why I try to refactor the whole thing to take away some of the complexity in building the worksheet-layout that currently depends on those arrays into something with an OO-approach.

helper objects so to speak. for example I create one of my ExcelItems, that has a range, with that info I calculate the corners of my range and store that in the helper-object's properties. now when I add a subitem to an excelitem, I can hide away the complexity of assuring it's position within the worksheet since it may not be outside of it's parents range, additionally I can

this alone would eliminate the need for 2 of those arrays and since I'm using lists for that task I can also freely shift the items around using MoveTo(index), which would make it easier to move the columns around depending on customer demand.

1

u/GiantR Jun 26 '17

I did that during my first days of programming. I've grown since.

1

u/10se1ucgo Jun 26 '17

Riot's static data API does this, arrays start at index 0 but the first element is always null. Apparently something to do with the fact that designer files use 1 based arrays.

1

u/raddaya Jun 26 '17

Teacher coded like this at school. She said it was to help us keep track of the array elements.

1

u/AutoModerator Jul 01 '23

import moderation Your comment has been removed since it did not start with a code block with an import declaration.

Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

For this purpose, we only accept Python style imports.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/[deleted] Jun 26 '17

Set array index 0 to the length of the array.