Simplified syntax for input properties

Questions about MultiCharts .NET and user contributed studies.
Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Simplified syntax for input properties

Postby Xyzzy » 01 Aug 2012

I've been playing with MC.net. It looks great!

I have one nit. I noticed that the code for the built-in indicators and signals generally uses this style for properties, like this code from MovAvg_Cross_LE:

Code: Select all

private int m_length = 9;

[Input]
public int length{
get { return m_length; }
set { m_length = value; }
}
This seems rather verbose, particularly since it requires six lines to declare an input property that could be declared through a single line in EasyLanguage. This sort of complexity might frighten away potential users who are more familiar with EasyLanguage.

I did some hacking, and I found a shorter way to declare the same property:

Code: Select all

[DefaultValueInput(9)]
public int length{ get; set; }
I tested this in MC.net, and it works great. I've attached a revised version of the MovAvg_Cross_LE signal, in which I replaced three properties using this syntax. It's quite a bit shorter and, in my opinion, simpler and easier to read.

I based this code on two features of C#:

1. There's a simplified way of declaring properties that only have simple getters and setters, known as "auto-implemented properties." That's described here: http://msdn.microsoft.com/en-us/library ... .110).aspx

2. By default, auto-implemented properties can't have default values. However, I tweaked this by changing the "Input" attribute to take a default value, and then used the technique described here to set the properties to their specified default values in the constructor: http://www.codeproject.com/Tips/310476/ ... implemente

To support this, I had to create two derived classes with some helper code, one for the "SignalObject" base class and one for the "InputAttribute" base class (called "DefaultValueInputAttribute" and"DefaultValueSignalObject," attached). To run this code, put the two derived classes into the "Functions" folder, and the "MovAvg_Cross_LE_Rev" file into the "Signals" folder.

Note that it would be possible for MC.net to include this code in the underlying SignalObject and InputAttribute classes. This would eliminate the need to have the additional derived classes, and allow for shorter syntax like this:

Code: Select all

[Input(9)]
public int length{ get; set; }
Would it be possible for MultiCharts to consider using this syntax or something similar for future releases of MC.net? I generally think that shorter, simpler code is a good thing, especially to help people who might be transitioning from EasyLanguage to C#.
Attachments
MovAvgCross LE Rev.zip
(2.36 KiB) Downloaded 367 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Simplified syntax for input properties

Postby Henry MultiСharts » 02 Aug 2012

Hello Xyzzy,

We will think about it.

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Simplified syntax for input properties

Postby riverTrader » 03 Aug 2012

There is one subtlety with properties. Properties are public accessor methods; as such they are much slower than direct variable access.

So.. if you are going to be accessing the property internally in your class, it is better to use the verbose version and access the private members (for simple accessors).

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Simplified syntax for input properties

Postby Xyzzy » 03 Aug 2012

RiverTrader,

I'm not sure if that's correct. There are two ways of declaring properties in C#. The first is the verbose way:

Code: Select all

private int m_length = 9;

public int length{
get { return m_length; }
set { m_length = value; }
}
The second is the auto-implemented way:

Code: Select all

public int length{ get; set; }
The auto-implemented way is actually semantically equivalent to the verbose way. When it's compiled to MSIL (the intermediate language), the compiler compiles it to a form that's equivalent to the verbose way, with a hidden backing variable. Consequently, there isn't any performance difference.
1. Can “Auto-implemented Properties” improve the performace?

No. It is just helping you to type shorter code but it doesn’t improve the performance since the compiler will generate the same as the way what it generates for normal class that doens’t have auto-implemented properties.
http://michaelsync.net/2008/02/29/c-30- ... properties

You might be thinking of using public variables, as opposed to a public property, like this. That way, you wouldn't need any accessors at all:

Code: Select all

[input]
public int Length = 9;
I actually think that this approach would be better than the method I suggested above -- it's cleaner and easier to read. Unfortunately, the "[Input]" attribute in MC .net doesn't currently work for public variables. Perhaps MultiCharts could support this in a future release.

There are some object-oriented "purists" who say that it's bad form to use public variables instead of public properties, but those rationales don't really apply when you just have basic getters and setters that don't perform any validation logic -- like the "verbose" form above. There's a good post about that here from Jeff Atwood:
When is a property not a property? When it's a glorified public variable.
Why waste everyone's time with a bunch of meaningless just-in-case wrapper code? Start with the simplest thing that works-- a public variable. You can always refactor this later into a property if it turns out additional work needs to be done when the name value is set. If you truly need a property, then use a property. Otherwise, KISS!
http://www.codinghorror.com/blog/2006/0 ... ables.html

However, note that there isn't a meaningful performance difference between simple properties and public variables. The JIT compiler recognizes when you're using a simple public property, and ends up performing some in-line optimizations that make the code run just as fast as a public variable. That's discussed here:
http://stackoverflow.com/a/3265377

bluejack
Posts: 42
Joined: 02 Aug 2012
Has thanked: 25 times
Been thanked: 27 times

Re: Simplified syntax for input properties

Postby bluejack » 03 Aug 2012

There is one subtlety with properties. Properties are public accessor methods; as such they are much slower than direct variable access.

So.. if you are going to be accessing the property internally in your class, it is better to use the verbose version and access the private members (for simple accessors).
This is not true. Simple accessors get inlined by the C# compiler. You will see no performance improvement by directly accessing private variables.

bluejack
Posts: 42
Joined: 02 Aug 2012
Has thanked: 25 times
Been thanked: 27 times

Re: Simplified syntax for input properties

Postby bluejack » 03 Aug 2012

Another way to initialize the auto property is by setting the value in the constructor.

Code: Select all

public TestStrategy(object _ctx):base(_ctx)
{
AtrLength = 10;
}

[Input]
public int AtrLength { get; set; }

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Simplified syntax for input properties

Postby riverTrader » 04 Aug 2012

please disregard my previous comment:
There is one subtlety with properties. Properties are public accessor methods; as such they are much slower than direct variable access.

So.. if you are going to be accessing the property internally in your class, it is better to use the verbose version and access the private members (for simple accessors).
I had not realized that simple accessors get inlined by the JIT compiler.

I stand corrected.

User avatar
gym.prathp
Posts: 1
Joined: 09 Nov 2012
Location: Chennai
Contact:

Re: Simplified syntax for input properties

Postby gym.prathp » 09 Nov 2012

Auto Implemented properties is implemented without having explicitly defined the member variable which is maintained by the property. C# compiler automatically implements the underlying member variable. In C#, it is called as hidden backing field. C# compiler defines the private backing field at the compile time. Due to that the name of the auto implemented private backing field is not visible within your C# code base (at the development time). So, it is not accessible. So, you have to manually initialize in some place in your code.


Return to “MultiCharts .NET”