Stop Thinking and Get Coding: Property Parameters
One of the cool things about C# is the way it handles "properties". When writing set statements, there is an implicit argument named "value" that holds the value passed to the property. Hence, within your implementation of the property, to get the value passed to the property you use the "value" variable, like so:
-
public string Name
-
{
-
get { return _Name; }
-
set { _Name = value; }
-
}
What's good about this? Well, you just didn't have to think of a name for the variable, like newName, or new_name, or name. Which shaved precious seconds of (wasted) thought off your coding.
Can we do this in Actionscript? Well, not exactly - there's no language support for it. But, if you think about it (and stop thinking about it), you can do it quite easily: use "value" as the name of the variable passed to every set method you write for the rest of your life.
-
public function get Name() : String { return _Name; }
-
public function set Name( value:String ) { _Name = value; }
This has a secondary (though equally if not more important) effect: it forces you to ensure the name of your property is sensible. You shouldn't need the parameter name of a property to clarify the intent of the property. If you do, you'll probably want to rethink your property name, which is way more important than deciding between new_name and newName. After all, when someone else is coding with or to your API, you don't want them to have to think about it, right?
Heck, take it a step further and use it in Javascript, too. Just for the fun of it.
Have a great weekend...





DustyPixels.com Blog » Blog Archive » FDT - Templates are your friend! wrote:
[…] Jason Nussbaum suggests on his blog the use of a standard naming convention in all your get and set functions to save you a few seconds thinking time. This is all good, but how about taking it one step further by ensuring you always use standard names while also saving some typing? If your using FDT I just might be able to help… […]
Posted on 22-Jul-06 at 2:53 pm | Permalink
jason wrote:
Yeah, if I wasn’t in such a rush posting that I probably would have added that snippets are a great way to make that more a part of your routine…
Posted on 23-Jul-06 at 4:16 pm | Permalink
Daniel Weltch wrote:
i play only vb so try to use c sharp convertor. thx
Posted on 27-Feb-08 at 8:46 am | Permalink