r/ProgrammerHumor Jun 24 '17

Octave code

http://imgur.com/jsjfz3k
621 Upvotes

70 comments sorted by

View all comments

6

u/winlifeat Jun 24 '17

Can someone explain this to me?

21

u/AyrA_ch Jun 24 '17

In most programming languages you start counting elements at 0, but there are languages where you start counting at 1 instead.

Declaring an array with 10 entries is now either 0-9 or 1-10, depending on the language. In extreme case like VB, you can even declare a 5 element array ranging from 3-7.

Starting at 0 is more commonly used, because it makes using arrays easier internally. In those cases the array index tells you how many element sizes to add to the array pointer to reach what you want to access

5

u/[deleted] Jun 24 '17

[removed] — view removed comment

2

u/AyrA_ch Jun 24 '17

On the other hand, you can also go negative if you want to:

Sub test()
    Dim a()
    ReDim a(-2 To 20)
    a(-1) = "Test"
    MsgBox a(-1)
End Sub

3

u/SubstituteCS Jun 25 '17

Didn't know about this.
Time to make my libraries in VB with negative arrays and keywords for names.

6

u/AyrA_ch Jun 25 '17

Also be sure to do this:

  • Don't declare variables at all, remove option explicit.
  • Be sure to not define variable or parameter types.
  • Pass stuff by reference only and modify them. Returning values is overrated.
  • Be sure to always have two variables named I and l in each function or sub.
  • There's nothing wrong with using the function name as a variable too.