The reason why the ternary operator doesn't work is because that is for assigning values. Meaning: the "else" part after ":" needs to return a value of the same type as the "then" case after "?".
And throw new
doesn't return a providers object ...
But in the end, that doesn't matter anyway; as the really simple version of that code looks more like:
if (providers.length == 0) {
throw new IllegalArgumentException();
}
this.providers = providers;
And in order to make things easier to read, you could even go for:
checkProvidersNotEmpty(providers);
this.providers = providers;
In other words: you move the exception throwing into a separate method. The implicit convention here would be that a method named checkSomething()
throws an exception when the check it does fails. And beyond that: give a reasonable message when creating that exception. It will help debugging later on.
You do not strive for the shortest program possible, but for the shortest version that comes with the best reading experience.
Using the ternary operator here would not result in an "easy to read" experience. Thus: forget about it.