2 ms·
Benefits of nullable by default: - Drawbacks: numerous How is this a trade-off?
by virtualized 10y ago
Benefits of nullable by default: -
Drawbacks: numerous
How is this a trade-off?
- chvid 10y agoNull as a default is useful / a big simplification when it comes to reflection/meta-programming. Consider this piece of pseudo-Java/Spring code and think how you would do it if the platform you were using forced you to either declare a and b as optional (which they are not) or assign them some value between the IoC container instantiates the class X and wires the values for a and b. public class X { @Autowired ServiceA a; @Autowired ServiceB b; @PostConstruct void init() { // a and b are instantiated by the IoC container and // are fully valid for the rest of the execution } }
- ppcsf 10y agoConstructor injection?
- chvid 10y agoYes. But then you have to define an explicit constructor. Also X maybe auto wired into ServiceA or B making it impossible for the IoC container to create the intended object graph without splitting construction and injection into two passes.
- yazaddaruvala 10y ago1. Lombok @RequiredArgsConstructor? 2. Do you not consider cyclic dependence a code smell? At least I have always avoided cyclic dependencies.
- chvid 10y agoI personally consider the example I gave as being a nicer way of doing dependency injection than the more verbose constructor based injection. But in general yes; cyclic dependencies are a bad idea.
- deleted 10y ago[deleted]