156

How language creators choose the function to get the size of an array..

I mean, the life could be more simple, this is just an example.

Comments
  • 5
    Varying syntax aside, fixed data structures like strings & arrays are generally implemented as a continuous block of memory with certain LENGTH. Other data structures, like sets and dictionaries, are commonly implemented as linked cells (either flat, or in a tree-like structure). For those types, you usually have multiple sparse blocks of memory, so you have either COUNT nodes, or a tree with certain SIZE.
  • 1
    Array has . Length and List has .Count
    But they both have a .Count(predicate<object>) method
    It's weird but you get used to it
  • 3
    Don't forgot lua's # operator.
  • 1
    My favourite in this way is trimming white spaces from string (trim vs strip) - I never get it right on first try...
  • 0
    true story.
  • 1
    where's is the super-upvote button. anyone?
  • 2
    In C, sizeof and len mean different things. Sizeof gives you the size of an array in bytes, possible padding included. The idiomatic usage of length however refers to the maximum element count, and that's only the same as sizeof for arrays of byte type.

    If you want to get the length of an array regardless of its underlying type, the expression is even weirder:
    len = sizeof(arr)/sizeof(arr[0]);

    But here comes the next fun part, when you hand over an array to a function, the function only sees a pointer without array length information, so you have to supply it as separate function parameter.

    The final fun thing is that you can still use sizeof(arr_ptr)/sizeof(arr_ptr[0]) in the function, but it will divide the pointer width of the architecture by the size of the underlying type, which is usually nonsense.
  • 9
    I never remember random functions.

    rand
    rand()
    Rand
    random
    random()
    RND
    RNG
    randInt
    Random.new()
    new Random()
    Math.random()
    ...
  • 1
    Wrong image, we all agreed this was the new gender selection component!!!
  • 1
    Stuff.howBigIsIt()
  • 2
    Can we all agree that .__len__() is the worst one
  • 0
    Each time i switch language XD
Add Comment