4 comments on “Flag Parameters and Overloading in Python, Java, and Kotlin

  1. As for the Kotlin case: overload resolution ambiguity is a bug that we are going to fix, but this kind of overloading doesn’t seem like a good practice anyways

    Like

  2. How about…

    
    def lookUpPerson(id, cache_result):
        person = simpleLookupPerson(id)
    
        if cache_result:
            cachePerson(person)
    
        return person
    
    def lookUpAndCachePerson(id):
        return lookUpPerson(id, True)
    
    def simpleLookUpPerson(id):
        # looks up the person
        return person
    
    def cachePerson(person):
        # cache person
    

    I prefer this approach.
    1. lookUpAndCachePerson just becomes an alias.
    2. The two actual responsibilities are separated, so SRP is even more prominently adhered to.
    3. lookUpPerson becomes a readable and understandable set of instructions
    4. I wrote it, so of course it’s awesome.

    You could just add more detail to your implementation:

    
    def lookUpPerson(id, cache_result):
        if cache_result:
            return lookUpAndCachePerson(id)
        else:
            return simplePersonLookup(id)
    
    def lookUpAndCachePerson(id):
        person = simpleLookUpPerson(id)
        cachePerson(person)
        return person
    
    def simpleLookUpPerson(id):
        # looks up the person
        return person
    
    def cachePerson(person):
        # cache person
    

    This would render my #2 and #3 points mute. Instead of using lookUpAndCachePerson as an alias for a specific way of using lookUpPerson, this code uses lookUpPerson as a facade so you only need to know one method signature for both cases rather than 2. Whatever floats your boat.

    Either way, this doesn’t really have to do with your article since this is just affecting internals and you were more focused on external API options for the consumers.

    Like

    • I had actually thought of that second solution and left it out, because I figured that, in writing the look up and cache function, a good programmer would probably separate that anyway. Also, it would have ended up distracting from the real point anyway, which is that you can use functions with flag parameters, but you should have separate functions too for each of the possibilities from the flags.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s