开发者

How to name method that raises an event

开发者 https://www.devze.com 2023-04-09 03:39 出处:网络
I have a method (Read) that raise an event when there are new logs: public class LoggerModel: ILog开发者_运维问答gerModel {

I have a method (Read) that raise an event when there are new logs:

public class LoggerModel: ILog开发者_运维问答gerModel {
  public event System.EventHandler LogsChanged;
  ...
  public void Read() {
     if (ThereAreNewLogs()) {
        OnLogsChanged(System.EventArgs.Empty);
     }
  }
}

but I feel that "Read()" is not expressive...any suggestions?


I'd call it CheckIfLogChanged()


It's a matter of taste, I personally use TriggerEvent, where "event" is the name of the event of course. It's also a good place to check whether the event is not null. But it's a private method in my case, whereas your Read method is public, and if raising an event requires calling some reminder-method "manually", it is at odds with the very idea of event-based communication imho.


Yes, it's confusing... I would call it Listen() or something like that.. perhaps StartListening()

In addition you have to check if someone has registered for the OnLogsChanged Event

by calling

if(OnLogsChanged != null)
{
  OnLogsChanged(EventArgs.Empty);
}


The real scope of that method is to verify that and handle new entries in log.

So for me could be VerifyNewLogs, or something like this.

But the best solution it would be to add the documentation for it, where you will describe what the method is doing. This will for sure help you or someone else in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号