What does `m_` variable prefix mean?

前端 未结 9 757
太阳男子
太阳男子 2020-12-22 18:47

I often see m_ prefix used for variables (m_World,m_Sprites,...) in tutorials, examples and other code mainly related

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 19:13

    In Clean Code: A Handbook of Agile Software Craftsmanship there is an explicit recommendation against the usage of this prefix:

    You also don't need to prefix member variables with m_ anymore. Your classes and functions should be small enough that you don't need them.

    There is also an example (C# code) of this:

    Bad practice:

    public class Part
    {
        private String m_dsc; // The textual description
    
        void SetName(string name)
        {
            m_dsc = name;
        }
    }
    

    Good practice:

    public class Part
    {
        private String description;
    
        void SetDescription(string description)
        {
            this.description = description;
        }
    }
    

    We count with language constructs to refer to member variables in the case of explicitly ambiguity (i.e., description member and description parameter): this.

提交回复
热议问题