473,320 Members | 2,112 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,320 software developers and data experts.

Best practice with get

Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.

So is the following TestDirectory get bad practice?

class Framework
{
string directory;

static string testFolder = "Test";
public static string TestFolder { get { return testFolder; }}

public static string TestDirectory{ get { return directory
+@"\"+testFolder; }}

public Framework(string directory)
{
this.directory = directory;
}
}

should I do this instead:

public static string GetTestDirectory()
{
return directory+@"\"+testFolder;
}

Thanks,

Aine

Oct 24 '07 #1
10 1389
On Oct 24, 1:12 pm, aine_ca...@yahoo.com wrote:
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.

So is the following TestDirectory get bad practice?
<snip>

Seems fine to me, although TestDirectory and TestFolder aren't very
clearly distinguished - I wouldn't know what to expect the difference
to be just based on the name.

Jon

Oct 24 '07 #2
On Oct 24, 8:12 am, aine_ca...@yahoo.com wrote:
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.

So is the following TestDirectory get bad practice?

class Framework
{
string directory;

static string testFolder = "Test";
public static string TestFolder { get { return testFolder; }}

public static string TestDirectory{ get { return directory
+@"\"+testFolder; }}

public Framework(string directory)
{
this.directory = directory;
}

}

should I do this instead:

public static string GetTestDirectory()
{
return directory+@"\"+testFolder;

}

Thanks,

Aine
Hi Aine,
I think it's fine to make it a property. One minor change is that I
would probably use Path.Combine( ) to concatenate directory and
testFolder (instead of +@"\"+), which will handle cases where
directory has a trailing backslash. I would also probably save the
result in a member varialbe so you don't end up combining the path
every time someone calls Framework.TestDirectory.
John

Oct 24 '07 #3
On 24 Okt, 14:17, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 24, 1:12 pm, aine_ca...@yahoo.com wrote:
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.
So is the following TestDirectory get bad practice?

<snip>

Seems fine to me, although TestDirectory and TestFolder aren't very
clearly distinguished - I wouldn't know what to expect the difference
to be just based on the name.

Jon
..
Yeah, I agree but the comments will clear that up. For me, a path is a
directory or file, a directory is: C:/folder1/folder2/folder3, and a
folder is folder1, folder2 or folder3.

Oct 24 '07 #4
John Duval wrote:
One minor change is that I
would probably use Path.Combine( ) to concatenate directory and
testFolder (instead of +@"\"+), which will handle cases where
directory has a trailing backslash. I would also probably save the
result in a member varialbe so you don't end up combining the path
every time someone calls Framework.TestDirectory.
I was thinking of the exact same two things when reading the code. :)

--
Göran Andersson
_____
http://www.guffa.com
Oct 24 '07 #5
On Oct 24, 1:29 pm, aine_ca...@yahoo.com wrote:

<snip>
Yeah, I agree but the comments will clear that up. For me, a path is a
directory or file, a directory is: C:/folder1/folder2/folder3, and a
folder is folder1, folder2 or folder3.
Just be aware that your terminology isn't universally known -
"relative" and "absolute" are more common ways of describing the
difference.

Jon

Oct 24 '07 #6
On Oct 24, 1:24 pm, John Duval <JohnMDu...@gmail.comwrote:
I think it's fine to make it a property. One minor change is that I
would probably use Path.Combine( ) to concatenate directory and
testFolder (instead of +@"\"+), which will handle cases where
directory has a trailing backslash.
Agreed.
I would also probably save the
result in a member varialbe so you don't end up combining the path
every time someone calls Framework.TestDirectory.
That sounds like a premature optimisation to me - I'd wait until there
was any indication that it was a problem before doing that. In
particular, if you're finding a directory name you're probably going
to do some IO, and that's bound to dwarf the cost of Path.Combine.

Jon
Oct 24 '07 #7
I would also probably save the result in a member
varialbe so you don't end up combining the path
every time someone calls Framework.TestDirectory.
This is one of the vexing problems about using properties IMHO. It's
tempting to apply get/set to anything that has the semantics of a property.
If the property does much more than just return a member variable however
then it can become expensive to use. Users of the property may not be aware
of this expense however nor should they. That is, because it's a property
and because it's syntactically so easy to use, it's just assumed to be
cheap. People will therefore (often) call it frequently and/or multiple
times within the span of several lines. The onus is therefore on the
programmer to:ensure that the property *is* cheap. This may not be
possible in all cases however (e.g., the value has to be dynamically
calculated or retrieved on each call), or you're otherwise forced to cache
the value in a member variable. In the former case you may be forced to
abandon the property in favour of a regular function and in the latter case
you may need to do the same thing (rather than cache every property since it
may create unaceptable overhead/clutter). In either case you may be forced
to use a regular function where a property really appears to be the more
natural choice. It's an inherent problem without a simple solution.

Oct 24 '07 #8
Larry Smith wrote:
times within the span of several lines. The onus is therefore on the
programmer to:ensure that the property *is* cheap. This may not be
possible in all cases however (e.g., the value has to be dynamically
calculated or retrieved on each call), or you're otherwise forced to cache
the value in a member variable. In the former case you may be forced to
I don't think it's necessary to always make a property cheap. Sometimes
it makes sense in cases where it is possible to be cheap, but you know
that setting this property is usually followed by an action. An example
of this would be setting the DataSource on a DataGridView. If I set the
DataSource property, it does a lot of things like raising an event
(DataSourceChanged), creating its columns, populating them with data,
and so on. This is because it makes perfect sense, even though it's not
necessary to do this. It could just have a method like
ActivateDataSource() or something which would perform all these steps,
but 99 times out of 100, when you set the DataSource you'd be calling
this method anyway.

That's just one example, but there are several similar ones in the
framework. For the most part, I'd use the framework as a guideline on
issues such as this. URI might be a good comparable example -- anyone
know how it handles things internally?

Chris.
Oct 24 '07 #9
On Oct 24, 9:08 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 24, 1:24 pm, John Duval <JohnMDu...@gmail.comwrote:
I think it's fine to make it a property. One minor change is that I
would probably use Path.Combine( ) to concatenate directory and
testFolder (instead of +@"\"+), which will handle cases where
directory has a trailing backslash.

Agreed.
I would also probably save the
result in a member varialbe so you don't end up combining the path
every time someone calls Framework.TestDirectory.

That sounds like a premature optimisation to me - I'd wait until there
was any indication that it was a problem before doing that. In
particular, if you're finding a directory name you're probably going
to do some IO, and that's bound to dwarf the cost of Path.Combine.

Jon
True, it's very unlikely to have any noticable performance impact. I
agree that it's best to take the "optimize when necessary" approach,
and prefer to stick with the simpler code unless you can measure a
performance difference.

Oct 24 '07 #10
Jon Skeet [C# MVP] wrote:
On Oct 24, 1:29 pm, aine_ca...@yahoo.com wrote:

<snip>
>Yeah, I agree but the comments will clear that up. For me, a path is a
directory or file, a directory is: C:/folder1/folder2/folder3, and a
folder is folder1, folder2 or folder3.

Just be aware that your terminology isn't universally known -
"relative" and "absolute" are more common ways of describing the
difference.
Or alternatively, if "Folder" is always going to be the very last
directory name in the path, a name indicating that could be at least as
useful as describing it as "relative". For example, TestFolderName, or
something similar making it clear that the "folder" property is the name
of a single object.

Pete
Oct 24 '07 #11

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

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
8
by: Fredrik Melin | last post by:
I have a "Inventory" Object that contains the product and all its fields. The problem is that I am getting soooooo many functions under main Inventory class so it becames impossible to initalize...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
4
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
2
by: MikeG | last post by:
When creating a class library is it wrong or not a 'Best Practice' to reference a property of an object from within a constructor or method of that object? I recall being told not to do this but I...
2
by: kbutterly | last post by:
All, I have a menu which contains Category as the master and Product as the child. When I click on a Category in the menu, I want one formView control, fvpc, to show, and then when I click on...
9
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.