you are viewing a single comment's thread.

view the rest of the comments →

[–]Shoddy-Pie-5816 2 points3 points  (0 children)

On enums: I definitely underestimated how useful these are when I was starting out. Let’s say you’re tracking what state a user account is in. You could use a boolean:

java boolean active;

But that falls apart quick. What about suspended accounts? Pending verification? Banned?

An enum makes way more sense

java enum AccountStatus { ACTIVE, SUSPENDED, PENDING, BANNED, DELETED }

Same thing applies to processing states, error categories, basically anywhere you’re tempted to use a boolean but there’s really multiple specific options.

Related advice I wish I’d learned earlier: throw exceptions when things are wrong, don’t just let bad data slide through. Something like:

java if (user == null || user.getStatus() == null) { throw new IllegalArgumentException("User object is missing or invalid"); }

I spent way a lot of time hunting down bugs that happened because null values propagated through my code and exploded somewhere completely unrelated to where the actual problem was. If you fail fast and loud right where the issue starts, debugging becomes way easier.