|
-
May 1st, 2006, 12:17 PM
#1
Thread Starter
Lively Member
[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:
Public Class MyMovie
Private strMovie As String
Private strName As String
Private strYear As String
'Constructor
Public Sub New(ByVal strFullMovieName As String)
strMovie = strFullMovieName
strName = DoABunchOfStuff() 'This is a private function in my class
End Sub
Public ReadOnly Property Name() As String
Get
Return strName
End Get
End Property
(...)
Or by calling the function directly from the property, like so:
VB Code:
Public Class MyMovie
Private strMovie As String
Private strName As String
Private strYear As String
'Constructor
Public Sub New(ByVal strFullMovieName As String)
strMovie = strFullMovieName
End Sub
Public ReadOnly Property Name() As String
Get
Return DoABunchOfStuff()
End Get
End Property
(...)
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
-
May 1st, 2006, 01:45 PM
#2
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
 
-
May 1st, 2006, 03:44 PM
#3
Thread Starter
Lively Member
Re: [2005] Class - Efficiency - Which is better practise
 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
-
May 1st, 2006, 03:57 PM
#4
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:
Public Class MyMovie
Private strName As String
Public ReadOnly Property Name() As String
Get
If Me.strName Is Nothing Then
Me.strName = Me.DoABunchOfStuff()
End If
Return Me.strName
End Get
End Property
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.
-
May 2nd, 2006, 10:04 AM
#5
Thread Starter
Lively Member
Re: [2005] Class - Efficiency - Which is better practise
 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:
Public Class MyMovie
Private strName As String
Public ReadOnly Property Name() As String
Get
If Me.strName Is Nothing Then
Me.strName = Me.DoABunchOfStuff()
End If
Return Me.strName
End Get
End Property
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|