4 ms·
Nulls are not a mistake. They are a trade-off as so many other things in language design.
by chvid 10y ago
Nulls are not a mistake. They are a trade-off as so many other things in language design.
- virtualized 10y agoBenefits 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]
- adwn 10y ago> Nulls are not a mistake. They are a trade-off as so many other things in language design. A bad trade-off – i.e., one where the downsides outweigh the upsides – is a mistake. Nulls are not a necessity: Several languages demonstrate that there are better alternatives.
- josephcooney 10y agoIf they guy who invented them calls them a mistake then maybe they are, or were at least to him.
- chvid 10y agoI was talking about them in a general context; for a particular language / a particular setting of course they may be a mistake.