Naming convention for non-virtual and abstract methods

前端 未结 8 1125
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 02:49

I frequently find myself creating classes which use this form (A):

abstract class Animal {
  public void Walk() {
    // TODO: do something before walking

    /         


        
8条回答
  •  一整个雨季
    2021-02-05 03:19

    I prefer to name my virtual or abstract methods with the suffix Core, to indicate, that the method should contain the core logic to do something.

    All argument checks and raising possible events I do in the method, that calls the Core-Methods.

      abstract class Animal {
        public void Walk() {
          // TODO: do something before walking 
          // possible Argument checks and event raising
    
          // custom logic implemented by each subclass
          WalkCore();
    
          // TODO: do something after walking
        }
    
        protected abstract void WalkCore();
      }
    
      class Dog : Animal {
        protected override void WalkCore() {
          // TODO: walk with 4 legs
        }
      }
    
      class Bird : Animal {
        protected override void WalkCore() {
          // TODO: walk with 2 legs
        }
      }
    

    I think there is no offical naming guideline for this, and it´s up to you. But it should be consistent for all classes and virtual/abstract methods you define.

    The "Framework Design Guidelines" suggest to use the Core suffix if you follow the Template Method and want to provide extensibility points.

提交回复
热议问题