If I cannot use Null Object (which creates unnecessary small objects). What is the right way to fix this? You need to apply the basics of Object Oriented Programming. Ask yourself:
- What does it mean to have a user variable with nil value?
- If user does not exist in our system what do you want to display instead of their name?
- Is blank value acceptable in this case?
If the above code snippet is part of a method, then the precondition for the method is that user object cannot be nil. It is a contract between the client and the API. As l long as I provide a valid user the method will be able to do it's job (fulfill the contract in Design by Contract terminology) and provide me the name. It's the responsibility of the client code to provide the method a valid user. In this case the solution would be:
'User not found'
Ideally I would look for the ActiveRecord call that throws an exception instead of returning nil for a record that is not found. I would handle the ActiveRecord::NotFound exception and translate it to business specific exception like MyApp::UserNotFound class that takes the message indicating the context of the application as the exception message. So there would be no nil check required in this case.