Declarative C # item24: Declarative Programming is better than imperative Programming

Source: Internet
Author: User

Declarative Programming is a simpler and more refined way to describe the behavior of a software program. It means that you can define the behavior of a program by declaring, rather than using instructions. In C #, most programming is imperative, that is, defining the behavior of a program by writing methods. We can use features to implement Declarative Programming in C #, which is easier to implement, read, and maintain.

. Net Framework itself has provided us with a lot of features, we can use these features very convenient to implement Declarative Programming; when. when the features provided by the net framework cannot meet our needs, we can also customize our own features.

First, let's look at an example of using the features provided by the. NET Framework and look at the following code.

Code

 1     public class ClassWithAttribute
2 {
3 [Conditional("DEBUG")]
4 [Obsolete("This Method should not be called yet.")]
5 public void Ouput()
6 {
7 Console.WriteLine("Hello World");
8 }
9 }

The above Code uses two features: the first feature, conditional, indicates that the method is called only in debug mode; the second feature, obsolete, indicates that the method has been discarded, it should not be called any more. Both features are.. NET Framework, which can be easily used.

Next, let's look at an example of custom features and see the following code.

Code

     [AttributeUsage(AttributeTargets.All)]
public class TestAttribute : System.Attribute
{
private string m_strOutputField;
public string OutputField
{
get { return m_strOutputField; }
set { m_strOutputField = value; }
}

public TestAttribute(string fielaName)
{
m_strOutputField = fielaName;
}
}


[Test("Name")]
public class Employee
{
private string m_strName;
public string Name
{
get { return m_strName; }
set { m_strName = value; }
}

private string m_strSex;
public string Sex
{
get { return m_strSex; }
set { m_strSex = value; }
}

private string m_strAddress;
public string Address
{
get { return m_strAddress; }
set { m_strAddress = value; }
}

public Employee(string name, string sex, string address)
{
m_strName = name;
m_strSex = sex;
m_strAddress = address;
}
}

The above Code defines a feature named testattribute, which is derived from system. attribute, which contains an attribute named outputfield, and defines a class used to represent employee information, which contains three attributes: name, sex, and address. The class is modified by the test feature, the value in the feature constructor is "name ".

The following is a test function.

Code

  1     private static void OutputEmpInfo()
2 {
3 Employee emp = new Employee("Wing", "M", "BeiJing");
4 //get attribute info.
5   object[] arrAttribute = typeof(Employee).GetCustomAttributes(typeof(TestAttribute), false);
6 string strFieldName = string.Empty;
7 if (arrAttribute != null && arrAttribute.Length > 0)
8 {
9 TestAttribute attribute = arrAttribute[0] as TestAttribute;
10 strFieldName = attribute.OutputField;
11 }
12 if (string.IsNullOrEmpty(strFieldName))
13 {
14 return;
15 }
16 //get property by reflection.
17   PropertyInfo propInfo = typeof(Employee).GetProperty(strFieldName);
18 if (propInfo == null)
19 {
20 return;
21 }
22 //output value.
23   object outputValue = propInfo.GetValue(emp, null);
24 Console.WriteLine(outputValue.ToString());
25 }

In the above Code, an object of the employee type is defined first, and information about the test feature in the employee type is searched, the outputfield attribute of the feature is returned, and then reflected, obtain information in the employee type object and output it.

When we set the outputfield attribute in the feature to "name", the program will output the name attribute of the employee object, when we set the outputfield attribute of the feature to "sex" or "Address", the program will output the value of the attribute corresponding to the employee object.

When a simple declaration shows our intention, declarative methods can avoid repetitive code. If you only need to write some simple functions once or twice in a program, if these functions are required in a large number of places, then the algorithm-based declarative solution will save us a lot of time and effort.

 

Summary: Declarative Programming is a powerful tool. When features can be used to declare our intent, it actually avoids multiple similar manually written algorithms, possible logical errors. Declarative Programming creates code that is more readable and clearer. This means fewer errors. If we can use the features defined in the. NET Framework, we should actively use them. If not, you can choose to create our own feature class and then use it to create the same behavior in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.