Java language and API improvements
Jonathan Yu
jawnsy at cpan.org
Fri Dec 4 18:28:01 UTC 2015
Hi Alberto,
It might be easier to discuss these proposals by separating them into
individual emails, to help keep things focussed. I'm not even sure if this
is the right list for Java language discussions.
I'm not an expert but just sharing some quick thoughts...
On Fri, Dec 4, 2015 at 10:22 AM, Alberto Otero Rodríguez <
alber84ou at gmail.com> wrote:
>
> *3) Switch with blocks*
>
> I think there should be a switch made with blocks in order to permit
> using variables with the same name and avoid the problems of not putting a
> break. It would be something like:
>
> switch(x)
> {
> case(1)
> {
> int i = 1;
> ...
> }
> case(2)
> {
> int i = 2;
> ...
> }
> default
> {
> ...
> }
> }
>
This should already be doable today, e.g.
switch (x) {
case 1: {
int i = 1;
break;
}
case 2: {
int i = 2;
break;
}
The "break" part is unfortunate but cannot be changed without breaking
existing code, since code that currently falls through would suddenly start
behaving differently.
> *4) Multiple condition ternary operator*
>
> I think it would be useful to be able to do this:
>
> String str = condition1 \ condition2 ? stringCondition1 :
> stringCondition2 : stringElse;
>
>
> Instead of:
>
> String str = null;
> if(condition1)
> {
> str = stringCondition1;
> }
> else if(condition2)
> {
> str = stringCondition2;
> }.
> else
> {
> str = stringElse;
> }
>
Personally, I find even a single ternary operator to sometimes be a bit
hard to follow in code, and I think having multiple like this would be
worse. It can also make stepping through line-by-line in a debugger more
difficult (a reason that I like to use lots of intermediate variables),
even if it results in more lines of code.
Cheers,
Jonathan
More information about the core-libs-dev
mailing list