As you know, in a lot of cases we need to check if an object is null or not to execute a method. It`s really not good to code, it gets ugly and repetitive. And sometimes the null objects was excepted (Search of a key in a list, or class dependency etc).
For example, using DI (using java, but can be any), as another approach that is not on constructor (method, property), we not know when the dependency will be set, and we need to check every place we must use the dependency. For example a logger:
If (logger != null ){
logger.error("blah");
}
It began to annoy me. I was thinking something to avoid this kind of situations. Maybe some operator or notation to make this kind of code not so repetitive. What do you think about some kind of ?
logger!.error("blah");
The void error will only be called if logger is not null (!. operator). Or something on field declaration.
@SafeCall //the name is just illustrative
Logger logger;
So, all the logger.*, just will be execute if the object is not null. Sure, I think that this on compile time should do what we do by hand, as the first example. Some opinion about ? Do you know some good way to avoid the repetitive code ?
Thanks