0

Which of these would be considered a better coding habit? These all achieve the same results (and is obviously an extremely simple example) but I didnt know if it was better to try and maintain function portability.

1

Public Sub myFunction()
    classInteger = GetLength(classString)
End Sub

Public Function GetLength(ByVal localString As String) As Integer
    localInteger = localString.Length
    Return localInteger
End Function

2

Public Sub myFunction()
    GetLength(classString)
End Sub

Public Sub GetLength(ByVal localString As String) As Integer
    classInteger = localString.Length
End Sub

3

Public Sub myFunction()
    classInteger = GetLength()
End Sub

Public Function GetLength() As Integer
    localInteger = classString.Length
    Return localInteger
End Function

4

Public Sub myFunction()
    GetLength()
End Sub

Public Sub GetLength() As Integer
    classInteger = classString.Length
End Sub
1
  • 1
    It's not just a "simple" example, it's so completely abstract (and incomplete) that it's virtually impossible to tell what you're asking. If you can provide a more real-world example (including the declarations), we might be able to help. Dec 15, 2012 at 22:22

1 Answer 1

0

The examples are too simple, so neither of the cases makes sense. You wouldn't have either of the methods, you would just do:

classInteger = classString.Length;

Even that is pointless, there is no reason to store a property from one member in another member.

To comment on the possible uses of the examples:

  1. This is the most flexible, and would be used if you need to do the same thing to different members and store in different other members. The function should be static (Shared) though.

  2. This would be used if you do the same thing to different members, but the result always goes in the same member.

  3. This would be used if you do something to a member, and sometimes store it in another member, sometimes do something different with it.

  4. This is pointless. There should be only one method in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.