TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Don't use the greater than sign in programming

24 pointsby jaxonduover 9 years ago

18 comments

_sq6aover 9 years ago
It&#x27;s better to write your code so that it&#x27;s easy for a later person to understand it while reading.<p>To that end, use a greater-than sign if reading the code makes more sense that way.<p>E.g. if the method name is `findAllUsersOverAge18()` then it&#x27;d be more sensible to have a check such as: `if (userAge &gt; 18)` than to have a check for `if (18 &lt; userAge)`<p>Both statements are technically correct, but the one makes a huge difference in terms of reading it and understanding it, without a Q&#x2F;A: &quot;Why is this conditional turned around?&quot; &quot;Well, Steve is opposed to ever using greater-than signs so we have to invert all our logic&quot;.<p>I also can&#x27;t help but pointing out- except for certain conventions such as loop iterator variables, it&#x27;s a much worse programming practice to use non-descriptive, single variable names, such as `x`.
评论 #11151170 未加载
评论 #11151384 未加载
评论 #11150729 未加载
Lanariover 9 years ago
This post is basically saying &quot;the simpler way is what I think is the simpler way&quot;, while in reality everyone see a way simpler then other.<p>Since it doesn&#x27;t hurt the performance there&#x27;s no need to advice a way over another, the first way that come up on your head is the simpler for you. Go ahead and finish your project fast mate.
mschoebelover 9 years ago
Have you ever in real life hear someone say &quot;If 80kg is less than your weight, then you are overweight.&quot;<p>Probably not. We say: &quot;If your weight is more than 80kg then you are overweight.&quot;<p>When talking we are used to first mention &quot;x&quot;, and <i>after</i> that the numerical value we compare it to. No matter if it&#x27;s less than or larger than what we want to compare it to.<p>If you swap these around, then whoever listens to you - or reads your code - will take a few seconds longer to understand what you mean.
评论 #11149648 未加载
Claudusover 9 years ago
I spotted the incorrect one instantly. Why? Because there are two good ways to write this correctly, and he chose to not use one, and to use the other incorrectly in in his example.<p>5 &lt; x &amp;&amp; x &lt; 10<p>10 &gt; x &amp;&amp; x &gt; 5<p>Both seem equally readable to me.<p>His argument also seems predicated on the assumption that numbers are always increasing.<p>If x &gt; 10 relax() else countdown(x) &#x2F;&#x2F; easier to read &quot;if timer is greater than 10, relax, otherwise, countdown&quot;<p>If 10 &lt; x relax() else countdown(x) &#x2F;&#x2F; harder to read &quot;if 10 is less than timer, relax, otherwise, countdown&quot;
steaminghackerover 9 years ago
Rubbish! greater than is useful if it makes code clearer. Yoda conditionals, no thanks.
barrkelover 9 years ago
This leads you to use &#x27;0 &lt; x&#x27; to test if a number is positive, which to my eyes looks a bit unnatural.
评论 #11150525 未加载
giomasceover 9 years ago
In Python you even have the shortcut 5 &lt; x &lt; 10.
评论 #11150504 未加载
jonesb6over 9 years ago
Argument is for improved code clarity, not performance etc. Definitely something to think about. Thanks for the post.
garethreesover 9 years ago
I do exactly this in my own code, for one-sided bounds like &quot;if (0 &lt;= x)&quot; as well as for two-sided bounds like &quot;if (0 &lt;= i &amp;&amp; i &lt; length)&quot;.<p>But when collaborating with other programmers, I follow the majority style. There are lots of programmers who prefer to write comparisons with the varying item on the left and the constant item on the right, and &quot;varying item is on the left&quot; is just as valuable a hint to them as &quot;lower item is on the left (if expression is true)&quot; is to me.<p>So I wouldn&#x27;t elevate a personal stylistic preference to a rule: this will lead to pointless arguments. Writing code that&#x27;s clear to all the programmers on a project should be the goal.
kmmover 9 years ago
Is this satire?
ch4s3over 9 years ago
I can see the utility for intervals, but for simple checks I think clarity of intent trumps all. Sometimes if foo.value &gt; bar makes more sense. THis is especially true when foo is the subject of the method.<p>Someone already submitted a PR to the Ruby style guide for this, and I can&#x27;t express how strongly I disagree. <a href="https:&#x2F;&#x2F;github.com&#x2F;bbatsov&#x2F;ruby-style-guide&#x2F;pull&#x2F;546" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bbatsov&#x2F;ruby-style-guide&#x2F;pull&#x2F;546</a>
makecheckover 9 years ago
For the range case, languages like Python got it right because the statement &quot;1 &lt; x &lt; 5&quot; means exactly what it would seem to mean.<p>For other languages I’m not sure it matters for inequalities.<p>For equality though, I always use the form &quot;if (constant == x)&quot; as opposed to &quot;if (x == constant)&quot; because in C it <i>guarantees</i> that the compiler will trip over an accidental-assignment typo. That is, &quot;if (constant = x)&quot; is a compilation error but &quot;if (x = constant)&quot; might be missed.
评论 #11151134 未加载
dfanover 9 years ago
I picked up the &quot;always use the less-than sign when you&#x27;re comparing&quot; rule of thumb from something written by Bjarne Stroustrup, though I can&#x27;t find an online reference to cite. I don&#x27;t always do it for single comparisons, but I do whenever there are multiple ones such as checking for whether a value is in a certain range.
jensnockertover 9 years ago
Except then you start using floating-point and then the transformations do not work anymore.<p>&gt; 1 &lt; nan<p>=&gt; false<p>&gt; nan &gt; 1<p>=&gt; false
gegtikover 9 years ago
Cute, but start by lobbying the Mathematics field and once they do away with &quot;&gt;&quot; the programming field will naturally follow.
david90over 9 years ago
It&#x27;s all about programming practice. I do like the suggestion `between(x1, x2)`.
评论 #11150120 未加载
评论 #11150323 未加载
dimmanover 9 years ago
But &#x27;&gt;&#x27; &gt; &#x27;&lt;&#x27;?
draw_downover 9 years ago
Wow, I completely do not understand.