Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • 44
    And don't forget to write a unit test to check that a Car cannot be constructed without a SteeringWheel... Commented Mar 20, 2018 at 14:28
  • 13
    I might be missing something, but if it's possible and even common to create a Car without a CarRadio, wouldn't it make sense to create a constructor that doesn't require one to be passed in and not have to worry about an explicit null at all? Commented Mar 20, 2018 at 16:34
  • 16
    Self driving cars might not have a steering wheel. Just saying. ☺ Commented Mar 20, 2018 at 16:54
  • 1
    @James_Parsons I forgot to add that it was about a class which didn't have null checks because it was only used internally and always received non-null parameters. Other methods of Car would have thrown an NRE with a null CarRadio, but the relevant code for SpeedLimit wouldn't. In the case of the question as posted now you would be correct and I would indeed do that. Commented Mar 20, 2018 at 18:40
  • 2
    @mtraceur The way everybody assumed that the class allowing construction with a null argument means null is a valid option made me realize that I should probably have null checks there. The actual issue I would have then is covered by a lot of good, interesting, well-written answers here which I don't want to ruin and which did help me. Also accepting this answer now because I don't like to leave things unfinished and it's the most upvoted (although they all were helpful). Commented Mar 21, 2018 at 11:19