Why I don’t like Visual Basic.NET – Part #2

As I have mentioned some time ago – I’m not a fan of VB.NET. Here is another example why:
Let say we have such function:

    Function CalculateAmount(ByVal items As List(Of Item)) As Decimal
        Dim amount As Decimal = GetSum(items) ''returns 15

        Dim tax As Decimal = CalculateTax(amount, items) ''returns 5
        Dim serviceCharge As Decimal = CalculateSC(amount, items) ''returns 2

        Return amount + tax + serviceCharge ''returns 27
    End Function

at first look everything looks fine. But, why there 27 as result, and not 22?
Oh, that’s obvious. Someone declared CalculateTax as:

    Function CalculateTax(ByRef amount As Decimal, ByVal items As List(Of Item)) As Decimal
        Dim tax As Decimal
        ''some magic
        amount += tax
        Return tax
    End Function

In above example it might be easy to find issue. But in more complex code, where developers don’t always care about it…

C# made it easier, you can either use ref or out, and you have to add this modifier when function is called. So much easier to read.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *