Skip to content

Instantly share code, notes, and snippets.

@nickknw
Created March 2, 2013 04:57
Show Gist options
  • Save nickknw/5069767 to your computer and use it in GitHub Desktop.
Save nickknw/5069767 to your computer and use it in GitHub Desktop.
' Okay, so this isn't a great example and because it's so simple it's a bit unfair to the
' object-oriented one. But hopefully it gets the gist across.
' Here's a made-up class with only one important method that does a tiny bit of calculation, in
' a very object-oriented, stateful way:
Public Class Blah
Private x As Integer
Private y As Integer
Private otherProperty As Integer
Public Sub New(ByVal x As Integer, ByVal y As Integer)
Me.x = x
Me.y = y
otherProperty = 0
End Sub
Public Sub Calculate()
otherProperty = (x * 0.5) + (y * 0.6)
End Sub
End Class
' Use pattern is like so:
Dim b As Blah = New Blah(5, 7)
b.Calculate()
log(b.otherProperty)
' Here's the same thing but with a more functional approach:
Public Module Blah2
Public Shared Function Calculate(ByVal x As Integer, ByVal y As Integer)
Return (x * 0.5) + (y * 0.6)
End Function
End Module
' Use pattern is like so:
Dim otherProperty As Integer = Blah2.Calculate(5, 7)
log(otherProperty)
' So, better suited for some things (but not all). The main reason I like to use this style when appropriate is that
' it reduces the mental load - you don't have to keep track of instance variables, everything you need is an argument.
' I like it less as the number of arguments goes up. Fortunately many functions turn out to not really need that
' many arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment