HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Rewrite 2 repeating With-blocks

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withblocksrepeatingrewrite

Problem

Got the following piece of code:

If boolFlag Then
              With offer.Person1
                entity.Birthdate = .BirthDate

                entity.FirstName = .FirstGivenName
                entity.LastName = .FamilyName

                entity.Street = .StreetName
                entity.HousNr = .HouseNumberIdentifier
                entity.BoxNr = .BoxNumberIdentifier
                entity.PostalCode = .PostalCode
                entity.Municipality = .CityName
            End With
        Else
            With offer.Person2
                entity.Birthdate = .BirthDate

                entity.FirstName = .FirstGivenName
                entity.LastName = .FamilyName

                entity.Street = .StreetName
                entity.HousNr = .HouseNumberIdentifier
                entity.BoxNr = .BoxNumberIdentifier
                entity.PostalCode = .PostalCode
                entity.Municipality = .CityName
            End With
        End If


Objects Person1 and Person2 are of different types, and those types do not share a common Interface / class. (And that can't be changed)

How do I avoid the duplicate code inside the With-blocks?

Solution

Unless you can make them share a common interface, it is not desirable to eliminate the code duplication -- best case (vb.net late binding on Object), trades your code size for the compilers. Doing the work yourself, simply means more code for you.

That said, where the code duplication is, is something you might want to control. Extract each assignment out into a similarly named method, and call the appropriate one.

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person1)
    With person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person2)
    With Person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

        If boolFlag Then
            UpdateEntityFromPerson(entity, offer.person1)
        Else
            UpdateEntityFromPerson(entity, offer.person2)
        End If


All of the code is still duplicated, but it type safe, clear, and easy to use and understand. Of course, the best thing to do is probably to have an entity constructor that takes one or the other, or a builder that does so, but that depends upon other factors, outside your snippet...

Code Snippets

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person1)
    With person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person2)
    With Person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

        If boolFlag Then
            UpdateEntityFromPerson(entity, offer.person1)
        Else
            UpdateEntityFromPerson(entity, offer.person2)
        End If

Context

StackExchange Code Review Q#10289, answer score: 2

Revisions (0)

No revisions yet.