0

I have always been tought to avoid using static methods. But i've also been tought to try to keep my code as readable and short as possible.

I have a list of proxies and for all of them i should perform 5 different validation.

For me these validation are so different that i don't feel like putting them in 1 big validation class. Especially if the validation will be extended this class could grow to large.

Now in this case i can write my validator class this way:

Public Class MyValidator1
Public Shared Sub Validate(proxy As ServiceClient, year As Integer)
    Dim args = New Arguments()
    args.Year= year 
    Try
        Console.WriteLine("Test")
        Console.WriteLine("--------------")
        Dim result = proxy.GeneratReport(args)
        Console.WriteLine("No errors")
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    Finally
        Console.WriteLine()
    End Try
End Sub
End Class

this is how it is implemented:

For Each proxy In _proxies
        Console.WriteLine(proxy.Endpoint.Address.Uri.Host)
        Console.WriteLine("------------------------")
        MyValidator1.Validate(proxy.Value, jaar)
        MyValidator2.Validate(proxy.Value, jaar)
        MyValidator3.Validate(proxy.Value, jaar)
        MyValidator4.Validate(proxy.Value, jaar)
        MyValidator5.Validate(proxy.Value, jaar)
    Next

I can offcourse make my methods non-static/shared but this would require me to create an instance of every validator.

What would be the pro and cons of each choice. For me it is easyer to read with static implementations.

JMan
  • 2,611
  • 3
  • 30
  • 51

1 Answers1

0

1)If your methods are not dependent on InstanceVariable you can make them static, and in your case since you are looking towards making them all static, Static Class maybe of your interest.

2)If your methods are not logically grouped better to have them in seperate classes, but if its only about your class growing and becoming less managable you can consider thinking of partial class for the same.

Vinay Pandey
  • 8,589
  • 9
  • 36
  • 54