• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Constant that represents moving from one based index to zero based index

 
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Human readable lists start at 1.

Java indices start at zero.

Hence the following:

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. -- Leon Bambrick



I want to avoid introducing a magic number into my code, namely (- 1) which takes into account moving between the human based and the java based indices.

Currently I am using:


Is there a commonly defined succinct term that I can use instead of the above?
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in the old Sn style guide (this might be the right section, and it might be the wrong section), you will find that 1 is a permisible number literal and doen't count a a magic number. You might be better with methods:-Since those numbers are encapsulated in the methods, they will not be visible anywhere else and won't cause confusion; you might wish to add comments explaining the workings of the methods, and the circumstances in which they might fail. By the way; there is no uch thing as a number contant −1.
 
Sheriff
Posts: 16961
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I understand your good intentions, zero-based indices are a convention in Java so your code will be more readable to experienced Java programmers if you stick with that convention. Trying to convert to one-based indices will just make your code more complicated and, in my opinion, convoluted. If for some reason you need to display a one-based sequence based on a zero-based index, just make the adjustment on the fly as you are printing out.
 
Junilu Lacar
Sheriff
Posts: 16961
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rex wrote:
Currently I am using:


This is a good example of how not following conventions leads to the opposite of what you're going for. That name is very difficult to read because you did not follow the convention of using underscore (_) to separate words in a constant name. Also, I don't really see a constant like this making the code more readable. If anything, it makes the code unwieldy and annoying to read.
 
Saloon Keeper
Posts: 13889
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wholeheartedly agree with Junilu. Stick with conventions.

Besides, 1-based indexing only seems more convenient because we use it in daily life. With a little practice, 0-based indexing becomes much easier to use for almost every job.
 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This morning, I wrote:. . . there is no uch thing as a number contant −1.

Apart from the many spellling errrors, that should have read number literal. Sorry.
Most things in Java┬« and indexed starting 0, which means when you get to index 1, you have moved 1 place from the first index.
People count 1‑based which means when you count 1, you have counted 1 item. I can think of two 1‑based things in Java┬«: the number of the index for the % tags (see the Formatter documentation) and order of fields in a reult set and other database results. As Stephan says, get used to both; you will find that integer arithmetic and 0‑based indexing match each other well.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was taught that -1 isn't a magic number.
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic