0

For example

public static void mapValues()
{

    if(getName("Wilson") != null)
    {
        String name = getName("Wilson");
    }
    // Or just
    String name = getName("Wilson");
}

private static String getName(String name) {

    String returnValue = " "; // Setting default returnvalue as empty
    if(name.equals("Wilson"))
    {
        returnValue = name;
    }
    return returnValue;
}

I know its not necessary to put a null check but my tech lead says it doesn't hurt you to put a null check even if not necessary. How would I make him understand its not okay?

  • Does the codebase have a policy of using `@Nullable` to indicate methods which can return null? – Andy Turner May 07 '17 at 21:47
  • An option is to have the default for returnValue to be null –  May 07 '17 at 21:48
  • 2
    "it doesn't hurt you to put a null check even if necessary" I guess you really mean unnecessary. And "`Setting default returnvalue as empty`" that's not the empty string. – Andy Turner May 07 '17 at 21:50
  • What are you going to do if `getName` returns `null`? Use an empty `String`? Where does the name go? What if it goes in a table or something, where the user can see it? Now your program still has a bug, but you don't know about it until somewhere somebody down the line sees that the name cell is empty and tells you about it. The difference is that now you have no idea what actually caused the problem and it could be potentially anywhere. – Radiodef May 07 '17 at 21:55
  • I'm setting returnValue in first place as empty string so that even if any of the conditions doesn't satisfy, it would atlas return an empty String but not null. – Samuel Sunny Wilson May 07 '17 at 21:56
  • "How would I make him understand its not okay?" How about putting in lots of checks for other values it can never have? – Andy Turner May 07 '17 at 22:00
  • 2
    Yes, but suppose that through some error, your implementation of `getName` actually did return `null`, maybe because there is a bug in the method. Your lead tech checks for `null` and then does something, although we're not sure what, where in your case it would simply throw an exception. Basically your lead tech is telling you that you should silently ignore errors where they happen and propagate an invalid program state. – Radiodef May 07 '17 at 22:01
  • In my opinion, it is always appropriate to check for null. Think long term, when your condition changes and the value can become null. Think about any runtime exception in the method which will force an early return. Think about other developers that may work with your code, and maybe you know it cannot be null but they will not. – tima May 07 '17 at 22:01
  • I'm tempted to close this as a duplicate of http://stackoverflow.com/questions/271526/avoiding-null-statements but it's an inexact duplicate and I'm not confident enough to dupe-hammer it myself. @SamuelSunnyWilson, I think what you are looking for is this Q&A at programmers.se: https://softwareengineering.stackexchange.com/questions/147480/should-one-check-for-null-if-he-does-not-expect-null. – Radiodef May 07 '17 at 22:12
  • Thanks found it more useful – Samuel Sunny Wilson May 07 '17 at 22:35

0 Answers0