Archivos para 28/04/11
[VS2010] Introduction to StyleCop
Publicado por elbruno en CodePlex, StyleCop, Tutorial, Visual Studio 2010 el 28 abril, 2011
Good,
StyleCop is a tool that analyzes C source code # and applies a set of style and consistency rules. You can run from within Visual Studio or integrated into a project of TeamBuild. It is quite useful when you want to ensure that the “aesthetic of your code” is consistent among themselves as well as having a good design of the architecture of your solution, respect the rules of design SOLID. This post is based on the 4.5RC version which can be downloaded from here.
A detail to keep in mind is that during the installation of the product, you can select the integration with Visual Studio 2010and also if you want the necessary files for MSBuild to add validations of StyleCop for compilations of Team Build.
For this example, I selected the 2 options.
We are on the first example of code that let us see what says StyleCop thereon. If we create a class library project with your class by default we find the following code:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace StyleCopDemo01
7: {
8: public class Class1
9: {
10: }
11: }
And StyleCop tells us with 6 Warnings of the following details to take into account:
If we “move” the using within the Namespace, as indicated by the recent warnings, the code is in the following way:
1: namespace StyleCopDemo01
2: {
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: public class Class1
9: {
10: }
11: }
And actually, now we just have 2 warnings for those who worry about:
Add a bit of code over the example:
1: public string Foo(int firstArgument, int secondArgument)
2: {
3: var res = "no";
4: if (firstArgument == secondArgument)
5: {
6: res = "yes";
7: }
8: if (firstArgument > secondArgument)
9: {res = "maybe";}
10: return res;
11: }
And now we’ll see how we have to worry about the fifth after the keys, the spaces between the elements between the keys, etc.
By default, StyleCop has enabled all the rules that have, which can be quite annoying; in particular with regard to the comments, as it not only recommend add tags in comments in all classes, but it also recommends headers in classes, sections of copyright, etc.
If you want to configure the set of rules with which we work, we can do so from the project in question. Select the same, deploy the contextual menu and select the option [StyleCop Settings]
In this form, we will see the different types of rules that apply during the analysis of StyleCop. My recommendation > disable those of documentation ![]()
In the coming posts a little more about StyleCop and Team Build.
Greetings @ Here
The Bruno
[VS2010] Introduccion a StyleCop
Buenas,
StyleCop es una herramienta que analiza código fuente C# y aplica un conjunto de reglas de estilo y consistencia. Se puede ejecutar desde dentro de Visual Studio o integrado en un proyecto de TeamBuild. Es bastante útil cuando además de contar con un buen diseño de la arquitectura de tu solución, de respetar las reglas de diseño SOLID, quieres garantizar que la “estética de tu código” sea coherente entre sí. Este post se basa en la versión 4.5RC que se puede descargar desde aquí.
Un detalle a tener en cuenta es que durante la instalación del producto, puedes seleccionar la integración con Visual Studio 2010 y además si quieres los archivos necesarios para MSBuild para agregar validaciones de StyleCop durante compilaciones de Team Build.
Para este ejemplo, he seleccionado las 2 opciones.
Vamos por el primer ejemplo de código para que veamos lo que dice StyleCop al respecto. Si creamos un proyecto de bibloteca de clases con su clase por defecto nos encontramos con el siguiente código:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace StyleCopDemo01
7: {
8: public class Class1
9: {
10: }
11: }
Y StyleCop nos avisa con 6 Warnings de los siguientes detalles a tener en cuenta:
Si “movemos” los using dentro del Namespace, como indican los últimos warnings, el código queda de la siguiente forma:
1: namespace StyleCopDemo01
2: {
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: public class Class1
9: {
10: }
11: }
Y efectivamente, ahora solo tenemos 2 warnings por los que preocuparnos:
Agregamos un poco de código más al ejemplo:
1: public string Foo(int firstArgument, int secondArgument)
2: {
3: var res = "no";
4: if (firstArgument == secondArgument)
5: {
6: res = "yes";
7: }
8: if (firstArgument > secondArgument)
9: {res = "maybe";}
10: return res;
11: }
Y ahora veremos como tenemos que preocuparnos por los espaciós después de las llaves, los espacios entre los elementos entre las llaves, etc.
Por defecto, StyleCop tiene activadas todas las reglas que posee, lo cual puede ser bastante molesto; en especial en lo referido a los comentarios, ya que no solo recomienda agregar tags de comentarios en todas las clases, sino que también recomienda headers en las clases, secciones de copyright, etc.
Si queremos configurar el set de reglas con el que queremos trabajar, podemos hacerlo desde el proyecto en cuestión. Seleccionamos el mismo, desplegamos el menú contextual y seleccionamos la opción [StyleCop Settings]
En este formulario veremos los diferentes tipos de reglas que se aplican durante el análisis de StyleCop. Mi recomendación > desactivar las de documentación ![]()
En los próximos posts un poco más sobre StyleCop y Team Build.
Saludos @ Here
El Bruno






SocialVibe