For what reason do we have the lower_case_with_und

2019-03-14 14:39发布

Depending on your interpretation this may or may not be a rhetorical question, but it really baffles me. What sense does this convention make? I understand naming conventions don't necessarily have to have a rhyme or reason behind them, but why deviate from the already popular camelCase? Is there a rhyme and reason behind lower_case_with_underscores that I'm somehow missing? (and yes, I have read PEP 8 in its entirety, and yes, I do understand that it's merely a proposal, a guide, etc.)

I suppose my real question would be this: I'm writing a Python library. In fact, with any luck, it may be a rather large library, relative to my other projects. I already try to adhere to PEP 8 as much as possible, and so far I've even maintained lower_case_with_underscores as PEP 8 instructs for function and method names. But it bugs me that I have to remember to use camelCase for Twisted, camelCase for logging, and just about everything else. What naming convention should I use, and why?

It would probably amaze people that I care this much about naming, enough to write a lengthy question about it, and it amazes me, too. Perhaps I have a little OCD when it comes to these things. I don't have much of a "personal opinion" about it as much as I have a tendency to just go for whatever is used the most, which in this case, would be camelCase - but it annoys me even further to find out that I'm probably breaking some eternal laws about explicit vs implicit and the zen of python written in stone or something.

10条回答
冷血范
2楼-- · 2019-03-14 15:10

LowerCaseWithUnderScoresAreSuperiorBecauseTheTextYouNormallyReadInABookOrNewsPaperForExampleIsNotWrittenLikeThis. .

查看更多
混吃等死
3楼-- · 2019-03-14 15:13

One reason could be historically, many computers did not have mixed case capabilities. In the days of COBOL, programs were all upper case. In the early 80's many 'personal computers' only came with upper case fonts. For example, you could get a lower case extender card for the Apple II+. When programs began allowing for mixed case, camel case was not popular. Many programs took what used to be in all caps, and just converted to lower case. Through the 80's various languages attributed meaning to case, and in the 90's Java popularized the camelCase syntax. Languages which have an older history, or are tied more closely to unix system programming tend to avoid making semantic use of mixed case.

查看更多
We Are One
4楼-- · 2019-03-14 15:13

Was it Meyers who came up with aLongAndTotallyUnreadableMethodeName vs an_even_longer_but_perfectly_readable_method_name comparison?

If you are already used to one, then your favorite convention will look more natural. But new programmers may prefer underscores (sample size N = me, in the year 2001).

I think some languages use camelCase because their API can be really horrible. You need the extra space so the code can fit in the IDE without wrapping.

For example:

poly = geometryLibrary.polygonGenerator.createFromPointSet(myList.listContentsToPointset())
查看更多
我想做一个坏孩纸
5楼-- · 2019-03-14 15:18

It is just a convention, but a useful one if you work with a lot of abbreviations.

How did you write EmailConfig (or was it EMailConfig)? HTTPRequest? email_config and http_request are then much clearer convention.

查看更多
来,给爷笑一个
6楼-- · 2019-03-14 15:19

The lower case with underscores convention goes all the way back to unix apis. Their entire syscall are in this convention.

查看更多
虎瘦雄心在
7楼-- · 2019-03-14 15:21

Feel free to do whatever suits you best, but consider using one of the popular conventions. It makes it easier for other developers to get up to speed with your code.

Remember that over the life of the project, more time is spent reading the code than it takes to write it.

查看更多
登录 后发表回答