473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

local paramater versus several "gets"

Hello,
I am new to object oriented programming and .net. I have a question
concerning using singletons.

Lets say I have the following:

public class Engine
{
public static readonly Engine Instance = new Engine();//singleton
private Device device;

public Device Device
{
get{return device;}
}

//other stuff omitted
}

public class OtherClass
{
public void Method1()
{
Engine.Instance.Device.Method1
Engine.Instance.Device.Method2
Engine.Instance.Device.Method3
Engine.Instance.Device.Method4
}

}

Calling OtherClass.Method1 implemented this way, requires a device "get" 4
different times.

Would it be faster and/or better programming to create a local device
variable at the start of Method1 and set this equal Engine.Instance.Device?
public void Method1()
{
Device device = Engine.Instance.Device;
device.Method1
device.Method2
device.Method3
device.Method4
}
Any experienced advice would be greater appreciated.

Thanks,
Dan

Nov 16 '05 #1
6 1026
Of course it would... for the reason you gave, the alternative is for the
get function to be invoked many times. It also increases readability and
maintainability.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

<Da********@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,
I am new to object oriented programming and .net. I have a question
concerning using singletons.

Lets say I have the following:

public class Engine
{
public static readonly Engine Instance = new Engine();//singleton
private Device device;

public Device Device
{
get{return device;}
}

//other stuff omitted
}

public class OtherClass
{
public void Method1()
{
Engine.Instance.Device.Method1
Engine.Instance.Device.Method2
Engine.Instance.Device.Method3
Engine.Instance.Device.Method4
}

}

Calling OtherClass.Method1 implemented this way, requires a device "get" 4
different times.

Would it be faster and/or better programming to create a local device
variable at the start of Method1 and set this equal Engine.Instance.Device? public void Method1()
{
Device device = Engine.Instance.Device;
device.Method1
device.Method2
device.Method3
device.Method4
}
Any experienced advice would be greater appreciated.

Thanks,
Dan

Nov 16 '05 #2
Here's a description of when it's better to use a property over a method.
It comes from FxCop and I've found it helpfull in the past:

In most cases, properties represent data, while methods perform actions.
Properties are accessed like fields, which makes them easier to use. If a
method takes no arguments and returns an object's state information, or
accepts a single argument to set some part of an object's state, it is a
good candidate for becoming a property.

Properties should behave as though they are fields; if the method cannot, it
should not be changed to a property. Methods are preferable to properties in
the following situations:

a.. The method performs a time-consuming operation. The method is
perceivably slower than the time it takes to set or get a field's value.
b.. The method performs a conversion. (Accessing a field does not return a
converted version of the data it stores.)
c.. The "Get" method has an observable side effect. (Retrieving a field's
value does not produce any side effects.)
d.. The order of execution is important. (Setting the value of a field
does not rely on other operations having occurred.)
e.. Calling the method twice in succession creates different results.
f.. The method is static but returns an object that can be changed by the
caller. (Retrieving a field's value does not allow the caller to change the
data stored by the field.)
g.. The method returns an array.

<Da********@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,
I am new to object oriented programming and .net. I have a question
concerning using singletons.

Lets say I have the following:

public class Engine
{
public static readonly Engine Instance = new Engine();//singleton
private Device device;

public Device Device
{
get{return device;}
}

//other stuff omitted
}

public class OtherClass
{
public void Method1()
{
Engine.Instance.Device.Method1
Engine.Instance.Device.Method2
Engine.Instance.Device.Method3
Engine.Instance.Device.Method4
}

}

Calling OtherClass.Method1 implemented this way, requires a device "get" 4
different times.

Would it be faster and/or better programming to create a local device
variable at the start of Method1 and set this equal
Engine.Instance.Device?
public void Method1()
{
Device device = Engine.Instance.Device;
device.Method1
device.Method2
device.Method3
device.Method4
}
Any experienced advice would be greater appreciated.

Thanks,
Dan

Nov 16 '05 #3
John,
Thanks for your reply. Out of curiousity, does anyone know if the compiler
will optimize the first version?

Thanks,
Dan
"John Wood" <j@ro.com> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
Of course it would... for the reason you gave, the alternative is for the
get function to be invoked many times. It also increases readability and
maintainability.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

<Da********@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,
I am new to object oriented programming and .net. I have a question
concerning using singletons.

Lets say I have the following:

public class Engine
{
public static readonly Engine Instance = new Engine();//singleton
private Device device;

public Device Device
{
get{return device;}
}

//other stuff omitted
}

public class OtherClass
{
public void Method1()
{
Engine.Instance.Device.Method1
Engine.Instance.Device.Method2
Engine.Instance.Device.Method3
Engine.Instance.Device.Method4
}

}

Calling OtherClass.Method1 implemented this way, requires a device "get" 4 different times.

Would it be faster and/or better programming to create a local device
variable at the start of Method1 and set this equal

Engine.Instance.Device?
public void Method1()
{
Device device = Engine.Instance.Device;
device.Method1
device.Method2
device.Method3
device.Method4
}
Any experienced advice would be greater appreciated.

Thanks,
Dan


Nov 16 '05 #4
Thx,
That helps alot!
Dan
"Rhy Mednick" <rh*@rhy.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
Here's a description of when it's better to use a property over a method.
It comes from FxCop and I've found it helpfull in the past:

In most cases, properties represent data, while methods perform actions.
Properties are accessed like fields, which makes them easier to use. If a
method takes no arguments and returns an object's state information, or
accepts a single argument to set some part of an object's state, it is a
good candidate for becoming a property.

Properties should behave as though they are fields; if the method cannot, it should not be changed to a property. Methods are preferable to properties in the following situations:

a.. The method performs a time-consuming operation. The method is
perceivably slower than the time it takes to set or get a field's value.
b.. The method performs a conversion. (Accessing a field does not return a converted version of the data it stores.)
c.. The "Get" method has an observable side effect. (Retrieving a field's value does not produce any side effects.)
d.. The order of execution is important. (Setting the value of a field
does not rely on other operations having occurred.)
e.. Calling the method twice in succession creates different results.
f.. The method is static but returns an object that can be changed by the caller. (Retrieving a field's value does not allow the caller to change the data stored by the field.)
g.. The method returns an array.

<Da********@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,
I am new to object oriented programming and .net. I have a question
concerning using singletons.

Lets say I have the following:

public class Engine
{
public static readonly Engine Instance = new Engine();//singleton
private Device device;

public Device Device
{
get{return device;}
}

//other stuff omitted
}

public class OtherClass
{
public void Method1()
{
Engine.Instance.Device.Method1
Engine.Instance.Device.Method2
Engine.Instance.Device.Method3
Engine.Instance.Device.Method4
}

}

Calling OtherClass.Method1 implemented this way, requires a device "get" 4 different times.

Would it be faster and/or better programming to create a local device
variable at the start of Method1 and set this equal
Engine.Instance.Device?
public void Method1()
{
Device device = Engine.Instance.Device;
device.Method1
device.Method2
device.Method3
device.Method4
}
Any experienced advice would be greater appreciated.

Thanks,
Dan


Nov 16 '05 #5
Hi John,

I'm not quite sure whether it will be optimized, because it is very
specific to compiler implementation.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #6
Thanks,

Dan
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:8j**************@cpmsftngxa06.phx.gbl...
Hi John,

I'm not quite sure whether it will be optimized, because it is very
specific to compiler implementation.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rob Freundlich | last post by:
I have some servlet-generated tabular data that I need to present, so I'm using an HTML Table. In some cases, it can be quite large. I'm flushing the servlet output every N lines to push the data...
32
by: Marcus | last post by:
We all know that the "gets" function from the Standard C Library (which is part of the Standard C++ Library) is dangerous. It provides no bounds check, so it's easy to overwrite memory when using...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.