0

I have a method that can return null if a condition of the parameter is met. For example

public static ObjectA newObjectA(String param) {
    ObjectB objB = new ObjectB();
    ObjectA objA;

    if (param == null) {
        return null;
    }

    // do something else

    return objA;
}

Is it better if I move the initialization of objB after checking the null if (param == null)? It will be like this:

public static ObjectA newObjectA(String param) {
    ObjectB objB;
    ObjectA objA;

    if (param == null) {
        return null;
    }

    objB = new ObjectB();

    // do something else

    return objA;
}

Is there any improvement like avoiding memory usage for objB? Or is it even better if I move the declaration of the objects after checking null param like this?

public static ObjectA newObjectA(String param) {
    if (param == null) {
        return null;
    }

    ObjectB objB = new ObjectB();
    ObjectA objA;

    // do something else

    return objA;
}

Thank you guys.

  • Don't worry about "memory"; rather, consider the *side effects* of the work done prior to the condition (including new) implies. For example, what happened if the constructor (for some terribly obscure and not cool reason) threw an exception? As such, such a general pattern is *not equivalent*. – user2864740 Apr 05 '16 at 23:17
  • This might be helpfull http://stackoverflow.com/questions/271526/avoiding-null-statements – Sven van den Boogaart Apr 05 '16 at 23:19
  • [In Java, should variables be declared at the top of a function, or as they're needed?.](http://stackoverflow.com/questions/1411463/in-java-should-variables-be-declared-at-the-top-of-a-function-or-as-theyre-ne) – robotlos Apr 05 '16 at 23:20
  • It's always better not to allocate memory if it's not necessary, but in terms of the example you are sharing it makes not so much sense since it's pretty theoretical. At the end of the day, it always depend on what does the actual method do. The null return of the method does not only depend on the parameter, it could also return null due to objA or objB or whatever. So checking if the parameter is null at first is generally a good practice, but the fact that it's not null doesn't mean that the method won't end up returning null, you basicaly cannot assume that. – Miquel Perez Apr 05 '16 at 23:23
  • Thanks for the links. @MiquelPerez Yes, actually after I make sure `param` is not null, I check it again using `switch (param)`. If it doesn't match with any value in the `case`s, the method return null in `default` case. So, it is guaranteed that the last code `return objA` will not be null if `param` is both not null and match with any of the `case`s, because in each cases `objA` is initialized. – Untung Suryono Apr 06 '16 at 02:59

1 Answers1

0

This approach is better -

public static ObjectA newObjectA(String param) {
    if (param == null) {
        return null;
    }

    ObjectB objB = new ObjectB(); // Occupied the memory required by for member variables of ObjectB along with the references
    ObjectA objA; // Occupy 4 bytes or 8 bytes as explained below.

    // do something else

    return objA;
}

Because although the uninitialized objects do not occupy much memory but they do occupy that much amount of memory which is required to store their references. And this memory is 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Talking about such method-level *'memory optimizations'* in C# for a general application is .. moot (and cases to the contrary are best made with empirical data). Since it's a very short lived object it's a "well optimized" case for the GC to handle (assuming the constructor does nothing interesting), even if does require a teeny weeny bit extra work. – user2864740 Apr 05 '16 at 23:22
  • @user2864740 Yes I do agree with you. For the case mentioned, it doesn't matters but I just provided the general explanation for better understanding of references and objects. – Shadab Ansari Apr 05 '16 at 23:25
  • This approach is not only on a preformance level the best one, it also fulfills the fail-fast pattern. So, it is the best code design of these. – F43nd1r Apr 06 '16 at 00:08
  • I agree with fail-fast pattern. But I also need to make sure what happen to `objB` in my first approach. Suppose that the given `param` is null, so `objB` is initialized then the method returns null. When the method is finished, is `objB` still left on the memory, or is it removed from the memory immediately because it has never been used? – Untung Suryono Apr 06 '16 at 02:29