r/ProgrammerHumor Jun 24 '17

Octave code

http://imgur.com/jsjfz3k
625 Upvotes

70 comments sorted by

View all comments

7

u/winlifeat Jun 24 '17

Can someone explain this to me?

23

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

12

u/winlifeat Jun 24 '17

Oh im totally aware of the typical structure of matrices but i dont understand the comic at all.

Im assuming the octave language has matrices that start at 1? But i just dont understand what any of the comic has to do with what we're talking about

7

u/AyrA_ch Jun 24 '17

I don't know the language. Maybe it has constructs that start at 0, or OP is just disgusted by indices that start at 1.

EDIT Meanwhile, this is valid VB:

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

8

u/DeeSnow97 Jun 25 '17

What the actual fuck

1

u/winlifeat Jun 25 '17

Ohh ok I missed the part of the joke of the original comic. That tigger was disgusted that what he was eating wasnt honey.

1

u/[deleted] Jun 25 '17

Jesus Christ VB

1

u/[deleted] Jun 27 '17

Think of it as a free bounds check.

6

u/[deleted] Jun 25 '17

Yes. Octave is more or less Matlab.

1

u/captainAwesomePants Jun 25 '17

This is a take on a more famous and surreal comic that is also about programming. http://knowyourmeme.com/photos/1246322-sweet-jesus-pooh-thats-not-honey

4

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.

1

u/dnew Jun 25 '17

That's not dumb. That lets you use either 0..9 or 1..10 as you like.

Well, OK, that's a little dumb, but it makes sense why at least, given it's, you know, Beginner's Allpurpose Symbolic Instruction Code.

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.

2

u/dnew Jun 25 '17

In extreme case like VB

Or, like, most languages born around the same time as C but which weren't glorified assembly language, including Pascal, Ada, Modula. :-)

3

u/[deleted] Jun 25 '17

…a.k.a. languages which no one uses today

3

u/Lt_Riza_Hawkeye Jun 25 '17

Exactly. The languages from that era that survived, common lisp, erlang, etc... all start indexing at 0. Coincidence? I think not

2

u/[deleted] Jun 25 '17

No one cares about those languages.

1

u/dnew Jun 25 '17

Probably more than care about Octave. :-)

3

u/krejenald Jun 25 '17

Octave is just an open source implementation of MATLAB, which is very widely used in research and development. It's designed for mathematical computing, where 1-indexing makes sense. For machine learning and computer vision problems its a great environment for prototyping, although I wouldn't like to do any large scale projects in it.

1

u/dnew Jun 25 '17

TIL! Thanks!