TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

CamelCase vs. underscores: Scientific showdown

56 点作者 ttmarek大约 10 年前

29 条评论

libria大约 10 年前
I&#x27;m heavily biased in favor of the idiomatic style of the language I happen to be using. There&#x27;s less context switching when interfacing with external libraries that are expected to follow the same.<p>There isn&#x27;t a significant loss in velocity in adapting to use one or the other or even simultaneously (front_end_javascript, BackEndJava, SELECT * FROM blah). The most significant portion of my time is spent thinking&#x2F;designing&#x2F;debugging; I save the balls-to-wall frantic keyboard banging for NCIS cameos.
smhenderson大约 10 年前
<i>Try choosing independently of (language) convention, habit or type of the identifiers.</i><p>That&#x27;s the non-starter for me right there. I use whatever convention makes sense in the context of what I&#x27;m doing. Language, library and previous code in an existing project all dictate my choice in this matter.
评论 #9138762 未加载
评论 #9138896 未加载
danso大约 10 年前
From the OP&#x27;s list of &quot;Pro CamelCase&quot;:<p>&gt; <i>Camel case is easier to type, and underscores are hard to type.</i><p>I&#x27;ll grant it that, but in the context of programming, this does not seem to be a &quot;pro&quot;. After all, don&#x27;t we spend far more time reading code than we do writing code?<p>I&#x27;m old, but I&#x27;m still surprised at how I have to fight over my reluctance to do something in another language (such as Python) when I remember that it&#x27;s conventionally camelCase...on the other hand, I try to use it as a personal advantage...I can immediately tell in a body of code what helper functions originated from me, as I&#x27;ll habitually write them in underscore (though this probably annoys the shit out of anyone who then tries to read&#x2F;use my code).<p>Conversely, my enthusiasm for using R jumped when I read Hadley Wickham&#x27;s style guide and saw that he endorsed underscores (<a href="http://r-pkgs.had.co.nz/style.html" rel="nofollow">http:&#x2F;&#x2F;r-pkgs.had.co.nz&#x2F;style.html</a>)
评论 #9138599 未加载
Someone1234大约 10 年前
I really like underscores for two word variable names. It only becomes a problem when you have variable names which are overly long, and your eyes have to move further.<p><pre><code> if(Accounts_Orders_Invoices_Table.Order_Invoice_Number == Order_Invoice_Number){ ...} if(AccountsOrdersInvoicesTable.OrderInvoiceNumber == OrderInvoiceNumber){ ... } </code></pre> But I don&#x27;t really care that much. I&#x27;ll just do whatever everyone else is doing. At least camcelCase keeps me from reaching for the _ as much and allows me to stay on the A-Za-z more of the time...
评论 #9138705 未加载
评论 #9138800 未加载
评论 #9139112 未加载
评论 #9138755 未加载
评论 #9138893 未加载
jdontillman大约 10 年前
Very often a variable name needs to provide more context.<p>I have found it very useful to use under_scores as a form of punctuation within camelCase to visually connect related variables.<p>Like this:<p><pre><code> spectrumPlot_x spectrumPlot_y spectrumPlot_drawHarmonics qa_signalDone qa_setLoadedFlag requestTimeout_minValue requestTimeout_maxValue </code></pre> ... and so forth.<p>(Note that the opposite doesn&#x27;t work: camelCase as a punctuation within under_scores.)
评论 #9142952 未加载
lutusp大约 10 年前
Not mentioned in the article is the fact that an underscored file can be algorithmically converted to camel case, but not the reverse (with any reliability). Therefore camel case has higher entropy, lower information content, and is less flexible.<p>Also, the expression &quot;scientific showdown&quot; about studies that rely on psychological self-reporting is a bit of a stretch.
teamhappy大约 10 年前
Underscores win. Having said that; if your standard lib chose camels, that&#x27;s probably what you should be choosing too. Consistency is the most important criteria.<p>I personally favour the C-style fuck-it-all naming scheme:<p><pre><code> else if (like) { likelowbar = true; vote(lowbarfmt); } </code></pre> The narrower the columns, the easier it is for me to comprehend the algorithm. It also makes for great method names. Say &quot;isatty&quot; three times; it&#x27;s fun, isn&#x27;t it?<p>&#x2F;&#x2F; BTW: I don&#x27;t believe anyone telling me they can read camels more efficiently. Underscores add contrast, which aids your vision quite a bit.
评论 #9138960 未加载
swhipple大约 10 年前
One point not mentioned in the article is that screen readers and other assistive technologies often produce better results when parsing words separated by underscores, so the code may be more accessible if it consistently uses an underscore-based style.
untog大约 10 年前
As of right now the results are as follows:<p>- CamelCase 52.34% (4,493 votes)<p>- underscores 47.66% (4,092 votes)<p>Which kind of says it all. Use whatever style you want, stop worrying so much about what other people are doing. Variable names a <i>far</i> more important than the formatting you use with them.
评论 #9138637 未加载
评论 #9139626 未加载
评论 #9138670 未加载
64bitbrain大约 10 年前
When I am writing C, I tend to use underscore. I think I just follow the convention, or the code that already exists. Imagine the some part of the code written in camelCase and other used underscore.<p>&#x2F;&#x2F;This wont look good :)<p>int static set_my_config(char* data);<p>int static getMyConfig();
engineerdollery大约 10 年前
As long as you follow convention, that&#x27;s all that matters.
评论 #9138591 未加载
exogen大约 10 年前
Camel case results in several sources of frustration for me:<p>1. Is it userID or userId? HTTPClient or HttpClient? Leaving this additional stylistic choice up to people results in conflicting styles even within the scope of &quot;camel case&quot; everywhere! (JavaScript mostly leaves them uppercase, e.g. DOMException...then there&#x27;s XMLHttpRequest...)<p>2. Code isn&#x27;t the only place we need to make this choice: what about configuration files? I find that using camel case for configuration keys – even if the configuration will be the input to a camel-case-language program! – just feels incredibly wrong.
Someone大约 10 年前
I would like to see whether mother tongue has an effect on preference. Languages with lots of compounding (<a href="http://en.m.wikipedia.org/wiki/Compound_(linguistics)" rel="nofollow">http:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Compound_(linguistics)</a> such as German and Dutch or languages without many spaces such as Classical Latin and Greek, Egyptian hieroglyphics and, sort-of, Chinese and Japanese might make CamelCase look more familiar. I know of one data point: Guido van Rossum is Dutch, and underscores are fairly common in Python (but, on the other hand, <a href="https://docs.python.org/3/library/functions.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functions.html</a> has &#x27;isinstance&#x27;, &#x27;setattr&#x27; and &#x27;memoryview&#x27;)<p>And, by the way, middle-score as in lisp (and, from there, Dylan and Clojure) is the superior approach: easier to type than either, and makes you stand out from the crowd :-)
olav大约 10 年前
In my eyes, using CamelCase is a kind of cargo cult, based on the fact that the programming language Smalltalk used it. C++ adopted the style and moved away from the predominant use of underscores in C. So, people who want to be seen as modern and object-oriented, are biased to use CamelCase to stress their competence in things OO.
dicroce大约 10 年前
I use the convention of the language or large code base I&#x27;m working with...<p>That said, I&#x27;ve found that the style isn&#x27;t nearly as important as consistency. Generally speaking, an inconsistently styled code base is a sign that the developers didn&#x27;t really care about their work...
mturmon大约 10 年前
For math, underscores evoke the &quot;_ is subscript&quot; convention of TeX. That can be a good thing, e.g.<p><pre><code> err_total = err_intrinsic + err_extrinsic </code></pre> which might mirror the notation in a paper that the code implements.
insin大约 10 年前
Personal logic bugfix:<p><pre><code> if ( thisLooksLikeJavaOrJavaScriptOr&#x2F;* ... *&#x2F; ) { youLikeCamelCase = true; votePoll( camelCaseFormatting ); } else if ( this_looks_like_python ) { you_like_underscores = true; vote_poll( underscore_formatting ); } </code></pre> Variable assignments for this particular piece of code:<p><pre><code> thisLooksLikeJavaOrJavaScriptOr&#x2F;* ... *&#x2F; = true; this_looks_like_python = false;</code></pre>
评论 #9138692 未加载
ars大约 10 年前
I use both. I use underscore for global functions, and camel case for functions specific to the module.<p>So an underscore function can not (easily) be modified to suit a particular function since it&#x27;s used all over the place.<p>But a camel case function can be modified because it is only used in the same part of the program, so you can easily change all uses of it.
slm_HN大约 10 年前
I find that I view camelCase as one word and camel_case as two words. The &#x27;_&#x27; works as a separator not a joiner. Since we can only keep four or five words in our working memory it&#x27;s better for me to use camelCase.<p>It&#x27;s possible that with more exposure to underscore_case I would eventually start treating it as one word.
logfromblammo大约 10 年前
I can easily write a tool to automatically convert CamelCasedVariables to underscored_variables, or vice versa. The fact that I have not done so indicates that I don&#x27;t care.<p>The important thing is that the variable names be chosen to facilitate human understanding of the code.
jgrowl大约 10 年前
Would it be possible to just have the editor display to the user whatever style they prefer but keep the underlying representation consistent? I guess it would just add complexity and make it harder to just edit the files as plain text though. Probably not worth.
G3E9大约 10 年前
I use camel case when the subject needs two words in order to identify it. Underscores, I feel, can be used to separate and include multiple ideas within a single name, in order to explain or emphasize on a relationship.<p><i>country_state_city</i><p>or<p><i>unitedStates_texas_houston</i><p>or<p><i>unitedStates_newMexico_santaFe</i><p>edit: sorry, don&#x27;t know HN&#x27;s markup
josephschmoe大约 10 年前
Is there some reason we can&#x27;t just do away with both and consider spaces in languages?
评论 #9140376 未加载
cordite大约 10 年前
Personally I prefer snake-case but most languages (including the ones I use most) do not support names with dashes in them.<p>That, and it&#x27;s easier to type (reduction of combo-pressing shift and the key for _)
fnordsensei大约 10 年前
I wonder how kebab-case would do. When available, I prefer it.
murbard2大约 10 年前
An argument pro underscore: they can be displayed with 50% transparency, making them visible, yet not interfering with the reading of the identifier as much.
wyager大约 10 年前
Let&#x27;s not pretend this is anything other than an aesthetic choice. All these &quot;scientific&quot; arguments one way or the other are weak at best.
Demiurge大约 10 年前
I don&#x27;t think they are exclusive? I use CamelCase for object oriented methods, classes and under_scores for procedural code and temporary variables.
sotiris-k大约 10 年前
Funnily enough if we want to be semantically correct then CamelCase is wrong and camelCase is right.
评论 #9138618 未加载
评论 #9138791 未加载