Java language and API improvements
Alberto Otero Rodríguez
alber84ou at gmail.com
Fri Dec 4 15:22:37 UTC 2015
Hi,
I'm Alberto, a Java EE developer from Spain. I would like to suggest
several improvements to the Java language and API. These are my suggestions:
*1) Variable declaration block*
I don't know why, but you can declare variables in a for declaration,
but not in an if declaration. I would add a variable declaration block that
could be use in if declarations like this:
varDeclarationBlock(XSpecificClass x = complicatedXCalculation(),
YSpecificClass y = complicatedYCalculation())
{
if(x != null && x.getValue() < 100)
{
...
}
else if(y != null && y.getValue() == 1)
{
...
}
else
{
...
}
}
*2) Null-safe operator*
Many times I have to write code like this:
String customerName = (order != null && order.getCustomer() !=
null) ? order.getCustomer().getName() : null;
I think there should be an # operator in the object class that could
check if the object is null and return null in this case without doing the
next methods. So, the previous sentence could be written like:
String customerName = order#getCustomer()#getName();
*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
{
...
}
}
*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;
}
*5) printObject*
Sometimes I want to know the values of all the properties of an Object
recursively. I have written a method to do that, but I think Java should
have something similar.
I attach my method which is called Util.printObject(Object
initialObject, int initialDepth, String filePath) where initialObject is
the object to print recursively, initialDepth is the maximum depth to print
and filePath the path of the file where the String will be printed.
More information about the core-libs-dev
mailing list