10 ways to improve the quality of C # Public program libraries (RPM)

Source: Internet
Author: User
Tags pear

Source: http://www.cnblogs.com/JamesLi2015/p/3140897.html

Recently refactoring a set of code, using the following several methods for reference.

1 Public methods use the cache as much as possible

Static list<string> Getregisteredcompany ()        {            "select Companycode from dbo. Company WHERE suspended= ' N ' ORDER by Companycode ";            DataTable table = SqlHelper.ExecuteDataset (Basecommon.getsystemconnectionstringbuilder (). ConnectionString, SQL). Tables[0];            In table. AsEnumerable () Select item. field<string> ("Companycode")). ToList (); }

The value returned by this method may be used in multiple places. The value of this method should be cached for use by the program multiple times.

2 using the Narrange format code, this tool can format the code, separated by region.

Narrange 0.2.9.0
____________________________________________________________
Copyright (C) 2007-2009 James Nies and Narrange contributors.
All rights reserved.
Http://www.NArrange.net

ZIP functionality courtesy of Ic#code (Mike Krueger, John Reilly).

After formatting, as shown, it is divided into several areas, looking more organized.

3 Find the duplicated code and extract it into the public method library.

For duplicate code in the Finder, there are tools to detect duplicate code

1) Simian http://www.harukizaemon.com/simian/

2) Team City http://www.jetbrains.com/teamcity/

3) Resharper plugin Agentralph http://agentralphplugin.googlecode.com/

4) DevExpress CodeRush

5) Resharper find Similar code looks for similar codes

6) Ccfinder

7) Duplo http://sourceforge.net/projects/duplo/

8) Visual Studio 2012

9) Clone Detective for Visual Studio Http://www.codeplex.com/CloneDetectiveVS

Ten) Atomiq http://getatomiq.com

One) NDepend http://www.ndepend.com/

) Clonedr Http://www.semanticdesigns.com/Products/Clone

There are quite a few tools, if developed using VS2012, this feature is already built in.

4 Add test code to test the functionality of the common code in various situations.

[TestMethod]  void Emplicensetestmethod ()        {            datetime expired=new DateTime (2013,12,1);            "Administrator";            string company=if (dlg. ShowDialog () = = DialogResult.OK) {doc. Save (dlg. FileName); }}

Visual Studio's built-in test tools and techniques, coupled with resharper tools, are a fairly easy way to implement.

5 use code to improve efficiency, replace inappropriate code, or apply. NET Framework method overrides a custom method.

Efficiency improvement Code, for example

1 do not use string ax= "" to determine whether ax is empty, ax. Length==0 is more efficient than ax==string. Emptyu is generally used as. IsNullOrEmpty
2 for variables that do not change, use constants instead of
3 using LINQ instead of a large number of foreach queries
4 MyType t= (MyType) T and T as MyType efficiency comparison, the latter efficiency is good, and conversion failure will not throw an exception
5 list<t> instead of array,haset<t>,dictionary<k,v> instead of Hashtable

. NET's built-in method, string. Join can concatenate a string array into a string that is concatenated by a delimited symbol. Like what

For example, if separator is "," and the element of value is "apple", "orange", "Grape", and "pear", then Join (separator, value) returns "Apple, orange, grape, pear".

If separator is a nullnothingnullptrnull reference (nothing in Visual Basic), use an empty string (empty) instead.

This method, we often achieve ourselves. should be used instead. NET built-in methods.


6 improving system efficiency with parallel technology

Parallel.ForEach can take full advantage of multicore and provide system performance. This method can be improved for some places where there is no need to consider the pre-and post-order operation of the data. For example, delete temporary files. For data that needs to be considered in the order of operations, such as emptying data table data, clear the child table before deleting the main table, otherwise it will cause a foreign key conflict. You should lock the data table that you want to delete with lock, extract the data, and then delete it.

7 using LINQ and lambda expressions to improve system notation

For only a few lines of methods, you can use lambda instead. If the method is universal, it can be written as a common method. Iterating through the DataTable, the preferred LINQ technology, reduces the chance of errors and simplifies the code.

8 String manipulation

Simple operations do not change, complex matches and lookups instead of regular expressions, while taking into account the encoding problem.

9 methods for extracting and extending public methods

If there are too many public methods for the static type, because the GC cannot reclaim memory, it can affect the efficiency of the system. You can consider using extension methods instead, which are directly replaced by static method calls of the original type. The type of the general Add extension method is. NET system types, custom types are added with few extension methods.

10 give the method comments as much as possible

Use the Vsdocman tool to write comments as much as possible for public methods, add use Cases

10 ways to improve the quality of C # Public program libraries (RPM)

Related Article

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.