null values vs "empty" singleton for optional fields
        Posted  
        
            by 
                Uko
            
        on Programmers
        
        See other posts from Programmers
        
            or by Uko
        
        
        
        Published on 2012-11-23T09:16:10Z
        Indexed on 
            2012/11/23
            11:15 UTC
        
        
        Read the original article
        Hit count: 391
        
First of all I'm developing a parser for an XML-based format for 3D graphics called XGL. But this question can be applied to any situation when you have fields in your class that are optional i.e. the value of this field can be missing.
As I was taking a Scala course on coursera there was an interesting pattern when you create an abstract class with all the methods you need and then create a normal fully functional subclass and an "empty" singleton subclass that always returns false for isEmpty method and throws exceptions for the other ones.
So my question is: is it better to just assign null if the optional field's value is missing or make a hierarchy described above and assign it an empty singleton implementation?
© Programmers or respective owner