Live data from Hacker News

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

llewellynfalco.blogspot.com

81–90 of 96 posts

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

#81

I really think this is just bikeshedding over style. If we were optimizing for readability and predictability, we'd standardize on the most commonly used format. ``if (x > 5 && x 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's just as good as getting rid of ``>``. It also has the advantage of being by far the most widesp…

But by far the best option is to not have a meeting at all and to not care about things like this. Indeed. This article reads like someone who doesn't understand inequalities, and proposes a quite possibly hazardous shortcut to it instead. To not understand such basic maths is baffling (this is primary-school stuff), and I wonder at the state of maths education these days to see adults struggling with these concepts.…

Agreed.

That said, I've come across code where the variable is always on the right. This often seems to be a throwback for some devs to alleviate the problem of mixing up equality and assignment.

That is, it seems that some people have been taught to use this expression (& stick to it religiously):

if (5 == x) ...

simply so that if they make a mistake and type

if (5 = x) ...

the (C/C++) compiler will reject it. So, these people always write the slightly more awkward case (imho) of:

if (5 x) ...

but to me it doesn't read as cleanly, because we don't say it that way.

Of course, there's the problem of understanding the meaning of 'between' which can be inclusive of the end points, or exclusive...and you really can't tell from the english. In every case, it needs to be clarified. (eg 'pick a number between 1 and 10'):

if (x>=1 && x<=10)...

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

#82

Earlier quoted context omitted.

Is this efficient? I was under the impression (which is likely inaccurate, because I'm no expert on Python) that range returned a generator or a list outright, making this an O(n) operation.

You're correct. It returns a list from from [a, b), but it's just another example of some expressive (albeit computationally inefficient) notation Python has. Edit: I take that back. Here's a section from Python's range() documentation: > The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents…

No, what the docs say is that range is a generator. It never stores the entire list of values, but it does iterate through all the numbers, spitting them out one by one (hence it uses O(1) memory, but O(n) computation).

Equivalent pseudocode:

  function in_range(x, a, b):
    for i=a; i

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

#83

Earlier quoted context omitted.

which is also the proper way you would read in any scientific paper, and very easy to read imho. 5 < x && x < 10 is the attempt to replicate this in other languages but falls a bit short

> 5 On the contrary, it's Python that added the shorthand, mathematical-looking notation because the longer "desugared" form wasn't great.

That's not the contrary, it is exactly what the other comment said ;). "To fall short" == "to not be great"

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

#86

Earlier quoted context omitted.

Well sure, except if the array is optional (and I agree it's code smell, but that's what you get when you use other peoples' libraries, I guess) that statement doesn't unwrap the array for me to use; I'll either need to do it manually on a second line (waste of effort) or every time I try accessing that variable. If I access it a lot, this is inefficient duplication. Plus, I don't like abusing the nil coalescing oper…

In your specific example, it didn't look like you wanted to use that array, so I thought I could save you a variable. If you are planning to use it, then by all means bind it to a variable for reuse.

What other use is there for a conditional let?

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

#87
Another article along the lines of "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".

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 "x > 0" more legible than "0 A lot of "Don't do" or "X considered harmful" article writers should really be a bit more careful before generalizing from "I like" to "everybody should" so quickly.

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

#88

Earlier quoted context omitted.

In your specific example, it didn't look like you wanted to use that array, so I thought I could save you a variable. If you are planning to use it, then by all means bind it to a variable for reuse.

What other use is there for a conditional let?

I thought your only use for the array was to check its count. Now that I think about it, that was probably a stupid assumption.

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

#89

Earlier quoted context omitted.

Does that work for floats?

Yes, if you use the pattern matching operator I suggested to the original comment's author: $ swift Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance. 1> let x = 3 x: Int = 3 2> let y = 3.0 y: Double = 3 3> 2...4 ~= x $R0: Bool = true 4> 2...4 ~= y $R1: Bool = true

Floats that aren’t equivalent to ints: 2.5.

In some languages, similar constructs test membership in a list/range, rather than an inequality.

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

#90
post #2

Ha, I've been using this trick for a while, though I wouldn't go so far as to say "don't use the greater-than-sign." Honestly I just wish languages had a cleaner way to write "x is within this range," e.g. "if x in [2,4)"

In C# this would be easy to implement using an extension method. Not a bad idea actually.
Post reply on HN