0

For example (in JavaScript):

//Not that I would ever add a method to a base JavaScript prototype... 
//(*wink nudge*)...
Array.prototype.lastIndex = function() {          
  return this.length - 1;
}

console.log(array[array.lastIndex()]);

vs

console.log(array[array.length - 1]);

Technically speaking, the latter method uses one less character, but also utilizes a magic number. Granted, the readability may not really be affected in this case, but magic numbers suck. Which is better practice to use?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Bloodyaugust
  • 2,463
  • 8
  • 30
  • 46
  • Many people advocate against it for various reasons, such as some older browsers not supporting the prototype object, `for in` loops, etc. – Bloodyaugust Aug 02 '12 at 15:23

4 Answers4

6

I'm of the opinion that 1 and 0 don't really count as "magic numbers" in many cases. When you're referring to the index of the last item (i.e. length - 1), that would definitely be one time where I would not consider 1 a magic number.

KRyan
  • 7,308
  • 2
  • 40
  • 68
1

Different languages have their own idiomatic ways of accessing the last element of an array, and that should be used. For example, in Groovy that would be:

myArray.last()

While in C, one would very likely do:

my_array[len - 1]

and in Common Lisp, something like:

(first (last my_list))
River
  • 1,504
  • 13
  • 21
  • Some don't though, IE JavaScript. – Bloodyaugust Aug 01 '12 at 21:42
  • I always thought that the len-1 approach was quite idiomatic for javascript, but there is this: http://stackoverflow.com/questions/3235043/last-element-of-array-in-javascript – River Aug 01 '12 at 21:52
1

i agree with @DragoonWraith that 1 is not a magic number. however it's not about magic numbers but about readability. if you need last index use myArray.lastIndex(), if you need last element use myArray.last() or myArray.lastElement(). it's way easier to read and understand than myArray[myArray.length - 1]

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • Readability was another major concern, but it would seem that most people here agree that [array.length - 1] is readable. – Bloodyaugust Aug 06 '12 at 21:18
  • no, it's not :) people just use it because they used to do it and changing habits is hard. the code is easy to understand when you write it. but it dramatically slows you when you read such code. – piotrek Aug 06 '12 at 21:24
  • @piotrek: Not every array object in every language *has* a `last____()` function, though. In languages without it, `length-1` is always going to be the most common way of indexing the last element, so within that language, I would argue that it is more readable (don't have to double-check that the custom function does what you think it does). If such a function exists, though, then I definitely agree with you. – KRyan Aug 07 '12 at 15:30
0

My take is that we should be looking for the style which the most programmers will be familiar with. Given that anyone who's been programming for more than a couple weeks in a language with this sort of array syntax (i.e., C-influenced imperative languages) will be comfortable with the idea that arrays use 0-based indexing, I suspect that anyone reading your code will understand what array[array.length-1] means.

The method calls are a bit less standard, and are language-specific, so you'll spend a bit more time understanding that if you're not totally familiar with the language. This alone makes me prefer the length-1 style.

Peter
  • 1,349
  • 11
  • 21