preconditions

Check in range precondition

六眼飞鱼酱① 提交于 2019-12-04 14:49:15
I like guava preconditions, but what I really need from it is one more method - check that the number is in range. Smt like this //probably there should be checkStateInRange also public static void checkArgumentInRange(double value, int min, int max) { if (value < min || value > max) { throw new IllegalArgumentException(String.format("%s must be in range [%s, %s]", value, min, max)); } } I believe I'm not alone and it's a pretty common case. But such method doesn't exist. Is there any reasons to not put such methods in com.google.common.base.Preconditions ? There are quite a few reasons I'd

how do i explain that if (xyz == null) checks are not “protective”

半城伤御伤魂 提交于 2019-12-04 11:30:38
i have a few developers who constantly put If null checks For example: Run(Order order) { if (order == null) return; } in their code as they think they are protecting their class if someone passes in a parameter that is null. I am trying to tell them the flaw in their logic because if someone is passing in null in this case, its most likely an issue with the consumer code and instead of this class throwing an exception and failing fast, it gracefully handles the bad behavior of the consumer and keep chugging away. another suggestion is to have precondition or guard class that fail fast and

Null check error message as “is null” or “was null”

走远了吗. 提交于 2019-11-30 12:33:27
When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use? We tend to use something like this public User getUser(String username){ if (username == null){ throw new IllegalArgumentException("username is null"); } // ... } What is better : "is null" or "was null", and why? For me "is null" feels more natural. polygenelubricants Since the Exception is thrown due to a failed precondition check, I think rather than simply stating a fact, you should state the requirement that was violated. That is, instead of saying

Null check error message as “is null” or “was null”

二次信任 提交于 2019-11-29 17:53:14
问题 When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use? We tend to use something like this public User getUser(String username){ if (username == null){ throw new IllegalArgumentException("username is null"); } // ... } What is better : "is null" or "was null", and why? For me "is null" feels more natural. 回答1: Since the Exception is thrown due to a failed precondition check, I think rather than simply stating a

What's the point of Guava checkNotNull

好久不见. 提交于 2019-11-28 18:06:12
I'm pretty new to Guava (let's be honest, I'm not "pretty new", I'm a complete rookie on that subject) and so I decided to go through some documentation and got quite amazed while reading this: com.google.common.base.Preconditions.checkNotNull(...) I don't get the point of this method. This means that instead of doing : myObject.getAnything(); (which might cause a NullPointerException if myObject is null) I should use checkNotNull(myObject).getAnything(); which will throw a NullPointerException if myObject is null and return myObject if it is not null. I'm puzzled and this might be the

What's the point of Guava checkNotNull

馋奶兔 提交于 2019-11-27 10:40:25
问题 I'm pretty new to Guava (let's be honest, I'm not "pretty new", I'm a complete rookie on that subject) and so I decided to go through some documentation and got quite amazed while reading this: com.google.common.base.Preconditions.checkNotNull(...) I don't get the point of this method. This means that instead of doing : myObject.getAnything(); (which might cause a NullPointerException if myObject is null) I should use checkNotNull(myObject).getAnything(); which will throw a