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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Don't use the greater-than sign in programming (2016)

48 点作者 sheharyarn超过 6 年前

23 条评论

danShumway超过 6 年前
I really think this is just bikeshedding over style.<p>If we were optimizing for readability and predictability, we&#x27;d standardize on the most commonly used format.<p>``if (x &gt; 5 &amp;&amp; x &lt; 10)``<p>Putting a rule that variables should always be on the left hand side of a comparison also gives you only 2 ways to write any conditional, so it&#x27;s just as good as getting rid of ``&gt;``. It also has the advantage of being by far the most widespread way that people already write conditionals, so you don&#x27;t need to hold a team meeting where you explain to a bunch of grumpy senior programmers why even though they <i>think</i> their coding style is easier to read it actually isn&#x27;t.<p>But by far the best option is to not have a meeting at all and to not care about things like this. In the absence of real, tangible data that conditional styles are causing bugs, these kinds of debates are very often a pre-optimization, and pre-optimization should be avoided.<p>When I put the effort into tracking and ordering the root causes of the majority of bugs in my software (both in personal projects and in large corporate environments) I am often surprised at the results. Very rarely are they consistent with the causes I would have predicted.<p>That&#x27;s why I call things like this bikeshedding. In a large organization, it is probably more productive for you to hold another meeting about encapsulation and code reviews than it is for you to start up a Slack discussion about what style people use on their conditionals.
评论 #18639520 未加载
评论 #18639472 未加载
评论 #18639454 未加载
评论 #18639500 未加载
fl0wenol超过 6 年前
Qualification: Don&#x27;t use the greater sign when doing range checks.<p>Counter-point: When checking if a variable is greater than a constant, using the less than is very confusing since we read left to right.<p>if(x &gt; LIMIT) {<p>Read &quot;If x exceeds LIMIT, then&quot;<p>The opposite reads like &quot;If the LIMIT is less than x&quot;, which sounds strange and emphasizes the wrong thing, implying LIMIT changed, not x.
评论 #18639517 未加载
评论 #18639492 未加载
评论 #18639473 未加载
nostrebored超过 6 年前
This is incredibly poorly thought out. You don&#x27;t only use conditionals while iterating. Even if you did, wanting to have a math like notation isn&#x27;t necessarily reasonable. When reading a simple multi-part inequality like 5 &lt; x &lt; 10 I don&#x27;t think &quot;when 5 is less than x&quot;, I still think about it in terms of the variable. The benefit of arranging things in a simple inequality like above is the shorthand notation. You don&#x27;t get that with:<p>5 &lt; x &amp;&amp; x &lt; 10 vs. x &gt; 5 &amp;&amp; x &lt; 10
jamesbrennan超过 6 年前
Clojure has a nice way of achieving<p><pre><code> (5 &lt; x &amp;&amp; x &lt; 10) This is a nice way of expressing &quot;x is between 5 and 10&quot; because it is literally between 5 and 10. </code></pre> which is that the &lt; function can take any number of arguments:<p><pre><code> (&lt; 5 x 10) </code></pre> <a href="https:&#x2F;&#x2F;clojuredocs.org&#x2F;clojure.core&#x2F;%3C" rel="nofollow">https:&#x2F;&#x2F;clojuredocs.org&#x2F;clojure.core&#x2F;%3C</a>
fooker超过 6 年前
Ah, <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Yoda_conditions" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Yoda_conditions</a>
评论 #18639412 未加载
deathanatos超过 6 年前
I use this. I can read lines reading left to right with only &lt;&#x2F;&lt;= <i>very</i> quickly now, because of the visualization of a numberline in my head. Any other arrangement, and I have to slow down to my old, normal processing, and reason <i>very</i> carefully about the code, lest a subtle bug slip by.<p>For similar reasons, I find this notation helps me prevent errors, because they become visually detectable.
评论 #18639433 未加载
lurquer超过 6 年前
I had to write a scripting language for a personal project. Since I could create whatever syntax I wanted, I used this:<p>&#x27;if x==4&#x27; was written as &#x27;x[4]&#x27;<p>&#x27;if x&gt;2 &amp;&amp; x&lt;4&#x27; was written as &#x27;x(2,4)&#x27;<p>&#x27;if x&gt;=2 &amp;&amp; x&lt;4&#x27; was written as &#x27;x[2,4)&#x27;<p>Etc.<p>It was very easy to read and write... at least for me.
评论 #18639526 未加载
评论 #18639474 未加载
nemo1618超过 6 年前
Ha, I&#x27;ve been using this trick for a while, though I wouldn&#x27;t go so far as to say &quot;don&#x27;t use the greater-than-sign.&quot; Honestly I just wish languages had a cleaner way to write &quot;x is within this range,&quot; e.g. &quot;if x in [2,4)&quot;
评论 #18639364 未加载
评论 #18639332 未加载
评论 #18639355 未加载
评论 #18641118 未加载
giomasce超过 6 年前
Another article along the lines of &quot;I personally prefer to use a syntax in a certain case, therefore everybody should use the same syntax in every case, and really I do not understand why the language creators even allowed for the other syntax&quot;.<p>No, your are not smarter than everybody else. You just have different tastes, and you are not even covering all possible use cases (I would say that most people find &quot;x &gt; 0&quot; more legible than &quot;0 &lt; x&quot;).<p>A lot of &quot;Don&#x27;t do&quot; or &quot;X considered harmful&quot; article writers should really be a bit more careful before generalizing from &quot;I like&quot; to &quot;everybody should&quot; so quickly.
johnday超过 6 年前
This just seems like an argument against putting the variable on the right hand side of the comparator.<p>You <i>also</i> only end up with two options, in the same way, and IMO (x &gt; 5 &amp;&amp; x &lt; 10) is much more readable than (5 &lt; x &amp;&amp; x &lt; 10) .
nimbius超过 6 年前
as a CS major im sure this has academic value. as someone who is just learning python outside of a trade job as an engine mechanic, this idea makes me want to glass someone and im not sure why.<p>(5 &lt; x &amp;&amp; x &lt; 10)<p>if we&#x27;re talking about X, whats wrong with defining the parameters of its constraints in terms of X instead of dancing around the numbers? 5 &lt; X makes it sound like im setting constraints on the number 5.
评论 #18639350 未加载
评论 #18639470 未加载
fouc超过 6 年前
This article is too basic.<p>&gt; (x &lt; 10 &amp;&amp; 5 &lt; x) which is a stupid option because it implies 10 &lt; 5<p>What? no it doesn&#x27;t!<p>&gt; (5 &lt; x &amp;&amp; x &lt; 10)<p>It&#x27;s already very obvious that one can take 5 &lt; x &lt; 10 and split it into 5 &lt; x &amp;&amp; x &lt; 10 right off the bat. Loads of examples of that out there.
dextersgenius超过 6 年前
<a href="http:&#x2F;&#x2F;4.bp.blogspot.com&#x2F;-vglPHDBuUgI&#x2F;VsniES7XnKI&#x2F;AAAAAAAAD9k&#x2F;7Evjten2ujI&#x2F;s1600&#x2F;Screen%2BShot%2B2016-02-21%2Bat%2B11.11.06%2BAM.png" rel="nofollow">http:&#x2F;&#x2F;4.bp.blogspot.com&#x2F;-vglPHDBuUgI&#x2F;VsniES7XnKI&#x2F;AAAAAAAAD9...</a><p>The number line representation is incorrect. (5 &lt; x &amp;&amp; x &lt; 10) represents x has possible values of 6,7,8,9 (assuming x is int), whereas the number line shows that 5 and 10 are inclusive, but that&#x27;s not the case as per the provided example.
评论 #18639538 未加载
benblu超过 6 年前
&quot;Go ahead and find out which one, I&#x27;ll wait&quot; ... As if it required herculean effort.<p>&quot;which is a stupid option because it implies 10 &lt; 5&quot; No it doesn&#x27;t.<p>And I stopped reading.
评论 #18641477 未加载
userbinator超过 6 年前
The bigger problem here is that if you say...<p><i>Let&#x27;s say that I want to check that something is between 5 and 10.</i><p>...you did not specify explicitly whether the ends are inclusive&#x2F;exclusive, and IMHO that (and the off-by-one errors it causes) is far more important.<p>Related: I&#x27;ve more than once had to ask someone whether &quot;I will be away from 23 to 27&quot; was inclusive or exclusive for the 27, because e.g. &quot;meeting from 10 to 11&quot; usually is <i>exclusive</i> at the upper end.
shmerl超过 6 年前
Removing something doesn&#x27;t necessarily make it more convenient to use. I&#x27;d find it bizarre if some language won&#x27;t provide &gt; using this logic.
shurcooL超过 6 年前
I prefer to use the “5 &lt; x &amp;&amp; x &lt; 10” style as well.<p>I’ve arrived to the same general conclusion independently, which is reassuring.
评论 #18639408 未加载
brudgers超过 6 年前
Lately, I am enamored of Ruby<p><pre><code> x.between?(6,9) </code></pre> or very explicitly<p><pre><code> x.between?(5.0.next_float, 10.0.prev_float)</code></pre>
评论 #18639610 未加载
评论 #18639525 未加载
zapzupnz超过 6 年前
This all looks good to me until I&#x27;m unwrapping an optional and checking it&#x27;s length in Swift, which normally looks like:<p><pre><code> guard let array = obj.someArray, array.count &gt; 0 else { throw SomeError }</code></pre>
评论 #18639393 未加载
评论 #18639411 未加载
评论 #18639374 未加载
karmakaze超过 6 年前
For normal scalars any are fine and I can see the number line merit of the post. Where I find much greater value is when dealing with points in time. Write x.isBefore(y) rather than y.isAfter(x) especially in compound contions.
dang超过 6 年前
Discussed at the time: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11148903" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11148903</a>
SilverSlash超过 6 年前
Claiming you shouldn&#x27;t use the greater than sign in conditionals because sometimes it&#x27;s confusing is going too far.<p>Also in python you can just do `if 5 &lt; x &lt; 10` :)
noobly超过 6 年前
..but the greater than sign is still being used?
评论 #18639373 未加载