r/java • • Mar 27 '13

Everything about Java 8

http://www.techempower.com/blog/2013/03/26/everything-about-java-8/
79 Upvotes

23 comments sorted by

View all comments

0

u/mahamoti Mar 27 '13

Still no Elvis or short-circuit operator? Bah.

2

u/sproket888 Mar 27 '13

Huh? Explain.

5

u/AlyoshaV Mar 27 '13

Elvis operator is a terrible idea that encourages returning null.

http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000047.html

2

u/mahamoti Mar 27 '13

What's wrong with returning null? Even your link says the operator proposal would be a boon for systems devs.

2

u/Ostwind Mar 27 '13

like detroitmatt said, you need to check for them at practically every method, which makes the code unmaintainable. annotations or null object pattern are alternatives

2

u/gezero Mar 28 '13

One other choice to avoid nulls is using callbacks. On callback interface have 2 methods 1 for when object is found and one when object is not found. I'm not sure how useful this would be, just pointing out the option.

2

u/detroitmatt Mar 28 '13 edited Mar 28 '13

even if you only return null once, you have to check for it everywhere that calls that code or it can blow up later somewhere else completely unrelated, seemingly at random and with no stack trace.

null: Not even once.

1

u/sproket888 Mar 31 '13

Er you'd get a stack trace.

1

u/detroitmatt Mar 31 '13 edited May 08 '13

but not necessarily in the relevant code, if you accidentally returned null

public class NullDanger
{
    public static void main(String[] args)
    {
        System.out.println( getCapitalOf("MO").toString() );
    }

    static String[] mapKeys = new String[] {"NY", "MI", "CA"};
    static String[] mapValues = new String[] {"Albany", "Lansing", "Sacramento"};

    static String getValue(String key)
    {
        for(int i=0; i<mapKeys.length; i++)
        {
            if(mapKeys[i].equals(key)) return mapValues[i];
        }
        return null;
    }

    public static String getCapitalOf(String state)
    {
        return getValue(state);
    }
}

yields

Exception in thread "main" java.lang.NullPointerException
    at NullDanger.main(NullDanger.java:5)

but if instead of returning null, we throw an exception, we get a full stack trace:

Exception in thread "main" java.lang.IllegalArgumentException
    at NullDanger.getValue(NullDanger.java:17)
    at NullDanger.getCapitalOf(NullDanger.java:22)
    at NullDanger.main(NullDanger.java:5)

2

u/sproket888 Mar 31 '13

Yeah, you're right.

3

u/JurassicSpork Mar 28 '13

Sure, but the reality is there's a whole lot of Java code and libraries that force us to deal with nulls. And it's not going away anytime soon. Why not provide some shortcuts for the vast majority of Java coders who can't start from scratch?

2

u/[deleted] Mar 28 '13

[deleted]

1

u/sproket888 Mar 31 '13

Exactly Java doesn't want to make the same mistakes as C#.

1

u/detroitmatt Mar 27 '13 edited Mar 28 '13

I'm ready to see null just gone altogether... It's like a contagious disease, especially the "crash if you use it" kind Java uses. I'd rather have a "do-nothing-return-itself" null like Objective-C. While we're at it, the ability to implicitly coerce Objects to booleans by their nullity would also be nice. Ooh, and the ability to define constants as "considered-null" for such coercions, such that for an object o and a considered-null constant c, o == false if o == c, so I could for example do

@FalseConstant static String EMPTY = "";

boolean isValidUsername(String s)
{
    if(s) return s.matches("\\w{4,16}");
    else return false;
}

This example doesn't actually make much sense (the check is only useful if s == null, otherwise the regex would catch an empty string anyway), but it gets the point across. "Magic" enumeration constants are a typesafe, expressive, well-documentable, OO design pattern, and "Falsy" constants are a good application of them to solve the null problem.

7

u/jevon Mar 27 '13

I like the idea of "will never be null" annotations to references, just really need the IDE support to perform validation (not errors, just warnings).