More flexible rule subsystem / engine

Sep 8, 2010 at 6:46 PM


Development of own custom rules can be quite a challenge. Been there, done that. :)

Especially when developing frameworks man (or shall I say "I") would like to enforce rules, applicable only for specific class and its descendants. Or maybe project-wide rules. Example of that kind of rules: "Each descendant of class MyBase must be decorated with MyCustomAttribute attribute." and "First statement in an overridden method MyMethod() of MyBaseClass must be call to base method implementation."

Each of these rules would have a "usage scope", that is if it can be applied to solution, project, class, type etc. They would be written in a configuration file. StyleCop would read this file, generate the code and run the rules against the target.

This is just an idea. Please feel free to comment. However, have in mind, that I do not know the internals of the StyleCop and thus I have no idea if this is even remotely possible to do.




Sep 8, 2010 at 8:59 PM
Edited Sep 8, 2010 at 9:00 PM

Hi Zvonko,

Did I understand you right, that you want to enforce such rules for yourself while developing your framework (not for another guy who uses your framework)?

If so, then your idea is quite possible to implement.

Moreover, such rules seem interesting for me.
If they are a not matter of interest to StyleCop team, I could try to implement them (eventually) in StyleCop+ (third-party plugin for StyleCop).
Just contact me (on StyleCop+ discussions page) if you like this proposal.

Best regards,
Oleg Shuruev

Sep 9, 2010 at 6:37 AM


Yes, you did understand correctly. There could also be an option to embed the rules and ship them with the framework. That would be very useful for enforcing granular rules for inheritance.

Example of a very useful rule: For better performance in structs MS suggessts they have their own implementation of equality operators. If you have just a few structs, there is no problem at all. What if you have tens of them? In this case one would create a project specific ruleset consisting of these rules:

  • struct must override the Equals(object o) method
  • struct must override the Equals(T o) method (where T is the actual value type)
  • struct must implement != operator
  • struct must implement == operator

When compiling the project, the programmer would be warned if any of these rules are violated. As we all know it's pretty easy to "miss a spot". :)

I've checked the StyleCop+ page and I really like your extended naming rule support. Especially binding these rules to specific types. Something like that (though extended and generalized) could be done for "flexible rules" I am talking about.