10

When I need to do numerical comparisons of user ids, should I do something like:

int numeric_user_id = Integer.valueOf("1234343");

or is it better to put that into a long, or keep it an Integer?

Thanks!

11
  • 1
    depends on your need if int can do your job then go with it Commented Jan 6, 2013 at 15:48
  • 5
    It depends. Are you going to have more than 4 billion users? Do you ever need to represent null? Commented Jan 6, 2013 at 15:49
  • 2
    If max # of int is sufficient for your anticipated number of users, go with int. Max long is greater than the population of the planet - should be enough. Commented Jan 6, 2013 at 15:50
  • 1
    as SuKu mentioned, this really depends on your needs. be aware that java provides the UUID class for unique identifiers. might be overkill, might be just what you need, depends on the application. Commented Jan 6, 2013 at 15:51
  • 1
    and if a beta user has some problem with his acount and must create a new user, recieveing a new id above 30000, will he not have the features ? sounds like the design needs more work.. Commented Jan 6, 2013 at 16:04

6 Answers 6

10

Unique Identifiers should not carry any special semantic meaning; ever. UUID or GUID types were designed just for entity identifiers, so that you don't infer any semantic meaning to them, that is what makes them opaque.

One of many practical benefits is that they will never clash when importing and exporting data to different databases, ie; development, qa, production, or when used in a cluster. If you use plain old numbers, especially auto-incrementing numbers, each database will have a different user for the same number and it is next to impossible to keep out dupes and other bad data from creeping into the different systems. UUID/GUID solutions completely avoid this kind of problem.

There are lots of reasons not to use the id for anything other than ids. What happens when you have more than 30000 beta users, what happens when you want to kick some people out of the beta user program, what happens when ... ?

As for using ids for business logic like identifying beta users, don't use the id for that, use another attribute flag to identify them. You could have a boolean flag, or an enum or some other flag, or even combine the fact that they are a beta user with when they joined the beta program by having a nullable timestamp, that would flag them as such, but that could end up being less flexible by leaking concerns; but it may be appropriate in some cases.

public interface Identifiable {

    public UUID getUUID();
}

public interface BetaUser extends Identifiable {

    public boolean isBetaUser();
    public Date    joinedBetaProgramOn();
}

int, long, and other number types should only be used for math; not for identity management or business logic un-related to mathematical concerns.

if you want to get the nth user of the system, then sort them by join date and take their index from the list

Sign up to request clarification or add additional context in comments.

2 Comments

"One of many practical benefits is that they will never clash when importing and exporting data to different databases". Could you kindly explain this? Although the chance is extremely low, still it's possible in theory that different database can randomly generate the same UUID.
there are different versions of UUID not all are generated randomly. They have data in them that is specific to where they were generated, that is why they are called Universal. There is also a chance although extremely low that the universe will wink out of existence at anytime as well.
4

I'd suggest using a Long (an object, not a primitive), precisely because it allows a null value. There will likely be cases where you will want this when operating on transient User objects (copying, cloning, etc.).

If you're going to persist them in a database, however, make the userId column not-null (better yet, make it the primary key).

Use Long rather than Integer so that you don't have to worry about overflow in the future.

Don't compare userIds or try to use them for any logic purpose (such as figuring out who is a beta user) - keep them as opaque identifiers. If you need to know who is a beta user, introduce data (like, say, accountType) to track that.

As others have mentioned, a UUID is another perfectly acceptable way to go, and has the bonus of being fairly opaque in it's nature (though not completely), pretty-much enforcing the "don't interpret userIDs" rule.

2 Comments

0 is meaningful for "no user", without needing null.
Except in the case of root
2

I would say put it into a long primitive. The reason is that, assuming you never want it to be null, you will never have the accidental situation where a "getUserId()" method returns a null user id and the error is not caught.

If you store it into a long vs. Long or Integer, the system will detect it as a runtime error immediately.

if you store it in an actual object, Like Long, it may be null. If this is an error, your code may fail at a later point in an unpredictable, possibly damaging way. It's best to detect errors as early as possible.

If it can be null and you use a real object, be sure to write code to handle the null case.

As for int vs long, you will have to make the decision based on whether you think it will ever exceed the size of an int.

This is also assuming your only choice is the normal numeric types. Perhaps there is another data structure that is more appropriate for such IDs,

3 Comments

depending on the implementation, 'storing' a null for primitive long may result in a zero value, instead of an error, so be careful there too.
That is interesting and I'm not familiar with how that would work. Could you provide a quick example?
I'm just observing the fact that primitive numerics are automatically initialized to zero. If passing a null to whatever your implementation is is being 'smart' and bypassing setting the id in this situation (rather than throwing an error), you'll wind up with your id == 0
2

In this case, The best way is that using of a lone primitive type instead of integer.

1 Comment

I believe your answer currently has a negative score because you do not explain WHY you think this is true.
1

The max value for an int is 2,147,483, 647 which is over 2 billions. In most situations, int should be more than enough.

Comments

0

Strange that still no answer advocated int. As automatic incremented primary key that will suffice in 99.99% of all applications. Furthermore user references are many (creator, last-editor, ...) so a bit saving as opposed to a long is nice.

Long will come into use as primary key if you have things like user notes and other historic items.

Rule: before long start with int, as a modification to java sources and database is easy.

1 Comment

The modification to java sources and database will be far from easy when he'll change from int to long because of so many users, so, the rule is not so ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.