#does that make sense to avoid using null keyword?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
when i should avoid it and when i should use it
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
thanks
Closed the thread.
ok i have one more question, so if im serializing class into json and this class has some field that is null by default, is that wrong?? like i dont see any way to work around it
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?
how are you transforming the object into json ?
jackson library
show the context please
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
Detected code, here are some useful tools:
why would it be null ?
so imagine that before user gets registered with password, i already need to track him, i have other fields here that are important
i think that i could split this into another class or something??
like after registeration do something to store password
if it makes sense to not have a password at the start, then I guess yes
or you could make it so you have to classes
pre-registered user without a password
and actual user with a password
is making Optional fields really bad idea??
yes
like what is the reason
the point of optional was to be used in return types so you have nice methods
i saw many projects with Optional in fields, it was even used in packet fields