#does that make sense to avoid using null keyword?

1 messages · Page 1 of 1 (latest)

echo canyon
#

does that make sense to avoid using null keyword?

tawny inletBOT
#

<@&987246399047479336> please have a look, thanks.

tawny inletBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

echo canyon
#

when i should avoid it and when i should use it

vagrant storm
#

Kinda

#

it depends

#

usually you can design in a way null wouldn't make sense in a field

#

because if a field can be null, then maybe you should have used inheritance of composition instead

#

the main problem of null is that not only it's way too easy to abuse, but it's also the most common cause of bugs

#

so as a rule of thumbs, avoid use nulls in method parameters or returns, use overloads or custom objects instead of parameters, and optional or checked exception for returns

#

the reason being that you must tell the user that this method can or cannot take null with a compilation error

#

but since it's not possible, it's better to use alternatives like I said just abive

#

because those alternatives would actually cause compilation errors

#

although it's not always possible to do that, and sometime, it makes more sense to use null :

#

in classes, sometime it makes sense to use null, and inheritance or composition can't really you, in this case you can use null, but you have to signal it, by using annotations, and doc

#

same for methods

#

and don't forget to put null checks at any point of your program where you shouldn't have nulls

#

as fail-fast measure

echo canyon
#

thanks

tawny inletBOT
#

Closed the thread.

echo canyon
#

lets say that there is some user and there is a password field in it, what if there can be guest user that dont have any password?

vagrant storm
echo canyon
echo canyon
#
public class User {
  @Getter
  private final String name;
  @Getter
  @Setter
  @Nullable
  private String pass = null;

  public User(String name) {
    this.name = name;
  }
}

lets say that its something like this

tawny inletBOT
vagrant storm
echo canyon
#

i think that i could split this into another class or something??

#

like after registeration do something to store password

vagrant storm
#

or you could make it so you have to classes

#

pre-registered user without a password

#

and actual user with a password

echo canyon
#

hmm

#

i need to think about it

echo canyon
vagrant storm
echo canyon
vagrant storm
echo canyon