-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Design.PreferEventsOverMethodsRule(git)
Sebastien Pouliot edited this page Mar 2, 2011
·
1 revision
Assembly: Gendarme.Rules.Design
Version: git
This rule checks for method names that suggest they are providing similar functionality to .NET events. When possible the method(s) should be replaced with a real event. If the methods are not using or providing event-like features then they should be renamed since such names can confuse consumers about what the method is really doing.
Bad example:
public delegate void MouseUpCallback (int x, int y, MouseButtons buttons);
public class MouseController {
private MouseUpCallback mouse_up_callback;
public void RaiseMouseUp (Message msg)
{
if (mouse_up_callback != null) {
mouse_up_callback (msg.X, msg.Y, msg.Buttons);
}
}
public void ProcessMessage (Message msg)
{
switch (msg.Id) {
case MessageId.MouseUp: {
RaiseMouseUp (msg);
break;
}
// ... more ...
default:
break;
}
}
}
Good example:
public class MouseController {
public event EventHandler<MessageEvent> MouseUp;
public void ProcessMessage (Message msg)
{
switch (msg.Id) {
case MessageId.MouseUp: {
EventHandler<MessageEvent> handler = MouseUp;
if (handler != null) {
handler (new MessageEvent (msg));
}
break;
}
// ... more ...
default:
break;
}
}
}
You can browse the latest source code of this rule on github.com
Note that this page was autogenerated (3/17/2011 1:55:44 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!