Opinions on using My as a class name prefix

☆樱花仙子☆ 提交于 2019-12-12 17:03:39

问题


Personally, I've never liked the MyObject naming of classes. I would guess that the status quo would agree but I'd like to see the other side of the argument and if there's any validity to it.


回答1:


I've never seen it done in production code, although I dare say it exists.

It's like the metasyntactic variables "foo" and "bar" - it's usually used as a placeholder for a real name.

So for example, if I know that someone has their own class deriving from Form, but I don't know anything else about it, a code example would use:

public class MyForm : Form

I'd certainly take a firm stance against it for real code though :)




回答2:


'My' is already used by me, use something else




回答3:


I suppose one instance where it would be close to acceptable is if the class you're prefixing with "My" is an inner class (i.e a private class declared within another class). I'm not sure if there are any naming conventions governing inner classes, but this could be one way to differentiate.




回答4:


As far as I've ever seen, My is a prefix used in sample code that indicates "your stuff goes here".

It's kind of like foo. Teaching purposes only.




回答5:


I've used My in 2 situations:

  1. Way back when in my first programming classes in school
  2. When I am doing basic 'hello world' applications to learn a language

but NEVER in production or even pseudo-production code.




回答6:


I'd avoid it in production code, your type name should reflect the function its supposed to perform.

I've used it a couple of times when patching/hacking third party code, for instance replacing Controller with MyController to make it clear it is a hack, that should be approached with caution, and a bat.



来源:https://stackoverflow.com/questions/1394858/opinions-on-using-my-as-a-class-name-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!