Results 1 to 5 of 5

Thread: [2005] Class - Efficiency - Which is better practise

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    [2005] Class - Efficiency - Which is better practise

    Rather new to the OOP approach, I got some questions concerning efficiency.
    In my class I have some private functions. Some properties return values based upon these functions.

    Would it be best if I set a variable in my class and Initialize it in the constructor, then return this variable, like so:

    VB Code:
    1. Public Class MyMovie
    2.     Private strMovie As String
    3.     Private strName As String
    4.     Private strYear As String
    5.  
    6.     'Constructor
    7.     Public Sub New(ByVal strFullMovieName As String)
    8.         strMovie = strFullMovieName
    9.         strName = DoABunchOfStuff() 'This is a private function in my class
    10.     End Sub
    11.  
    12.     Public ReadOnly Property Name() As String
    13.         Get
    14.             Return strName
    15.         End Get
    16.     End Property
    17.  
    18. (...)

    Or by calling the function directly from the property, like so:

    VB Code:
    1. Public Class MyMovie
    2.     Private strMovie As String
    3.     Private strName As String
    4.     Private strYear As String
    5.  
    6.     'Constructor
    7.     Public Sub New(ByVal strFullMovieName As String)
    8.         strMovie = strFullMovieName
    9.     End Sub
    10.  
    11.     Public ReadOnly Property Name() As String
    12.         Get
    13.             Return DoABunchOfStuff()
    14.         End Get
    15.     End Property
    16.  
    17. (...)

    I reckon that in the latter example each time I 'm getting the value of that property, the private function has to be executed where as in the first example this only happen when creating an instance my class. The first would be more efficient, better. Am I right or ?
    Using Visual Studio 2005 / Framework 2.0

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Class - Efficiency - Which is better practise

    The first way would definitely be more efficient as long as you need to get that property more than once. If you only need it once, then the second route may be slightly more efficient.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    Re: [2005] Class - Efficiency - Which is better practise

    Quote Originally Posted by Shaggy Hiker
    The first way would definitely be more efficient as long as you need to get that property more than once. If you only need it once, then the second route may be slightly more efficient.
    Think I'll go with the first option.
    Could you elaborate a bit on why the second might be a bit more efficient when only needed once? As in Perfomance wise or otherwise?
    Using Visual Studio 2005 / Framework 2.0

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Class - Efficiency - Which is better practise

    If DoABunchOfStuff is a time-consuming operation then it may be preferable to defer its execution until it's needed, particularly if the strName variable and Name property are not needed until the user actually accesses the property. That way your object will be created more quickly and DoABunchOfStuff will never be executed unless it is required. It is possible to defer creation of some member objects but still only execute the code that creates them once like this:
    VB Code:
    1. Public Class MyMovie
    2.  
    3.     Private strName As String
    4.  
    5.     Public ReadOnly Property Name() As String
    6.         Get
    7.             If Me.strName Is Nothing Then
    8.                 Me.strName = Me.DoABunchOfStuff()
    9.             End If
    10.  
    11.             Return Me.strName
    12.         End Get
    13.     End Property
    14.  
    15. End Class
    Note that it is only worth doing this if creating the strName variable is a time-consuming operation and it may not be needed if it is never accessed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    Re: [2005] Class - Efficiency - Which is better practise

    Quote Originally Posted by jmcilhinney
    If DoABunchOfStuff is a time-consuming operation then it may be preferable to defer its execution until it's needed, particularly if the strName variable and Name property are not needed until the user actually accesses the property. That way your object will be created more quickly and DoABunchOfStuff will never be executed unless it is required. It is possible to defer creation of some member objects but still only execute the code that creates them once like this:
    VB Code:
    1. Public Class MyMovie
    2.  
    3.     Private strName As String
    4.  
    5.     Public ReadOnly Property Name() As String
    6.         Get
    7.             If Me.strName Is Nothing Then
    8.                 Me.strName = Me.DoABunchOfStuff()
    9.             End If
    10.  
    11.             Return Me.strName
    12.         End Get
    13.     End Property
    14.  
    15. End Class
    Note that it is only worth doing this if creating the strName variable is a time-consuming operation and it may not be needed if it is never accessed.
    Thanks, The private fuction indeed does quite some processing so I want to keep from executing it too much. your solution does seem the way to go.
    Thanks!
    Using Visual Studio 2005 / Framework 2.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width