Creating a custom code analyzer

The real magic of code analyzers come to the fore when you create one to suit a specific need. What would qualify as a specific need? Well anything that is specific to your own business requirements that is not covered in the out-of-the-box analyzers. Don't get me wrong; the existing analyzers that are available to developers really cover a lot of good programming practices. Just take a look on GitHub by searching for C# code analyzers.

Sometimes, however, you might have a case where something is more suited to your workflow or the way your company does business.

An example of this is could be to ensure that comments on all public methods include more information than just the standard <summary></summary> and parameter information (if any). You might want to include an additional tag with the internal task ID, for example (think Jira here). Another example is making sure that a created class conforms to a certain XML structure. Are you developing software that writes warehouse stock information to a database? Do you use non-stocked parts? How do you validate non-stocked from stocked parts in code? Code analyzers can provide a solution here.

The preceding examples are perhaps rather unique and not applicable to you or your needs at all, but that is the beauty of code analyzers. You can create them to suit your requirements. Let's take a look at a very simple example. Assume that developers in your organization need to use a specific code library. This code library is a collection of frequently used code and is well maintained. It is included in a Visual Studio template that developers use when creating new projects. We need to ensure that, if a developer creates a specific class (for purchase orders or sales orders), it implements a specific interface. The interfaces exist in the template, but the classes do not. This is because the applications will not always be using sales or purchase orders. The interface is to enable the receipt of sales and purchase orders and is called IReceivable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.70