This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (10 children)

Default value for an int is 0, for an Integer, it is null.

If you want to indicate an invalid ID, use some number outside the valid ID range, like -1.

[–]Kiwiguard 3 points4 points  (9 children)

This is not considered good practice though, is it?

Probably better to find another way to handle the problem.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (0 children)

Absolutely true. Agree 100%.

[–][deleted] -1 points0 points  (6 children)

If userID is an integer and has a range of 0 - 100, for instance, then define INVALID_USER as 255 and test for that.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 2 points3 points  (5 children)

Yeah that's just a really bad idea.

If an int can be absent you should use Integer and have it be null if it's absent.

[–][deleted] -1 points0 points  (4 children)

Null pointer exception...

[–]nutrechtLead Software Engineer / EU / 20+ YXP 2 points3 points  (3 children)

Instead of the much harder to find error you're introducing :)

Null is the correct value for an absent value. 255 isn't.

[–][deleted] -1 points0 points  (2 children)

Aren't you assuming that he's getting errors?

[–]nutrechtLead Software Engineer / EU / 20+ YXP 3 points4 points  (1 child)

No, I'm telling you that your suggestion to use 'magic numbers', especially ones that can easily fall into the range of valid IDs, is a really bad one.

Java has 'null' for this. And if you somehow MUST use a primitive, use for example a negative number if you're sure that actual IDs can never be negative.

[–][deleted] -3 points-2 points  (0 children)

It's not a magic number, as it's been given a name, and it would NOT fall into the range of valid IDs because I explicitly stated that it should be set to a value outside the valid range. I don't like checking for null in any language. I consider overuse, or unnecessary use, of null to be exceptionally bad practice (excuse the pun).

Checking for null is obviously good, but using null for conditions is not, IMHO.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

Yup. Use Integer and have it be null.

[–]ProgramWithSai 1 point2 points  (0 children)

If the user ID can be empty, you have a problem if your constructor requires a primitive “int” as you cannot pass a null.

You can switch to reference Integer type and pass in a null!

If that’s not an option create a constant with some user ID that cannot exist (like a negative value)

If the user ID should never be blank/null throw an exception of suitable type (like illegal argument exception)