473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Meth od1 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 1039
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********@nos pam.nospam> wrote in message
news:%2******** ********@tk2msf tngp13.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.Meth od1 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********@nos pam.nospam> wrote in message
news:%2******** ********@tk2msf tngp13.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.Meth od1 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******** ******@TK2MSFTN GP09.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********@nos pam.nospam> wrote in message
news:%2******** ********@tk2msf tngp13.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.Meth od1 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******** ******@TK2MSFTN GP09.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********@nos pam.nospam> wrote in message
news:%2******** ********@tk2msf tngp13.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.Meth od1 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.mic rosoft.com> wrote in message
news:8j******** ******@cpmsftng xa06.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
5097
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 to the browser as it generates, and I've used "table-layout: fixed" for the table's CSS class. It works pretty well in Netscape (7.1 and higher) - the table is drawn pretty much as the rows are received by the browser. However, Internet...
32
3810
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 it, and impossible to guarantee that it won't happen. Therefore, i think it's surprising that this function has not been deprecated. The C++98 Standard keeps it from the C89 standard. The C99 Standard has kept it :-o.
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.