Live data from Hacker News

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

llewellynfalco.blogspot.com

71–80 of 96 posts

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

#71

Earlier quoted context omitted.

Yeah, I was wrong. I updated my comment. I think I just meant to check if a range contains a value, as I changed it to.

You can use ~= if you're looking for a succinct way to do an operation like this: if 2...4 ~= x

I didn't know about this. Brilliant, cheers!

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

#72

Earlier quoted context omitted.

A somewhat stupid way to solve this issue (of what is likely poorly-designed API, given what you're trying to do here; optional Arrays are generally a code smell) is to abuse the nil coalescing operator: guard !(obj.someArray?.isEmpty ?? true)

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.

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

#73

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.…

> This article reads like someone who doesn't understand inequalities

Not really the point I was getting at. I don't think the author is stupid, and I don't think their style is hazardous. I think it doesn't matter.

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

#75
post #31

I had to write a scripting language for a personal project. Since I could create whatever syntax I wanted, I used this: 'if x==4' was written as 'x[4]' 'if x>2 && x 'if x>=2 && x Etc. It was very easy to read and write... at least for me.

I suppose the language has neither array subscripting nor function calls? Or did you decide to invent other creative syntax for those?

It was a cell growth simulation. Morphogenesis. The goal was to start with a single cell and see if I could make it divide and grow into something neat. The constraint was that all the cells were identical, and 'knew' only a limited amount of things about their environment: how many neighbors were touching them, and the levels of an arbitrary number of 'chemicals' that would diffuse from one cell to the other.

I made the scripting language as an easier way to program the 'rules' in the cell class. Dozens of little rules such as "if chemcial_0 is between 0.879 and .936 the increase chemical_1 by a smidgen," or "if chemical_23 equals 1.738 then undergo mitosis."

The little shorthand syntax was far easier to read than the mess of conditionals in C++.

It was pretty neat. Used Box2d for the physics part.

Got up to making a worm kinda thing with legs. But then gave up.

Trying to make identical cells differentiate into asymmetrical patterns is something that always intrigued me. Got interested after reading this: http://www.mvla.net/view/19352.pdf

How's that for a long-winded answer...

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

#77

Lately, I am enamored of Ruby x.between?(6,9) or very explicitly x.between?(5.0.next_float, 10.0.prev_float)

But you have to know if the boundaries are inclusive or exclusive, which leaves you with four different possibilities. You and everyone who reads your code needs to know this.

Ruby is my main language and I would have to look up the boundaries of this function.

Your "explicit" statement is also so much worse than using `x < 5.0` or `x <= 5.0`, depending on what you want. This is terrible advice imho.

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

#78
post #77

Lately, I am enamored of Ruby x.between?(6,9) or very explicitly x.between?(5.0.next_float, 10.0.prev_float)

But you have to know if the boundaries are inclusive or exclusive, which leaves you with four different possibilities. You and everyone who reads your code needs to know this. Ruby is my main language and I would have to look up the boundaries of this function. Your "explicit" statement is also so much worse than using `x < 5.0` or `x <= 5.0`, depending on what you want. This is terrible advice imho.

I am lately enamored of Ruby because it helps me write code that makes me happy. Calling methods on numbers is one of those things that does.

Anyway, ranges are another way of expressing the concepts in Ruby:

  (5.0.next_float..10.0.prev_float).include?(x)
or

  (5.0.next_float...10).include?(x)
if I don't mean

  (5..10).include?(x)
None of which is advice. Just remarks about why I am enamored of Ruby, lately. If I wanted to write C, I'd use Go.

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

#79

Earlier quoted context omitted.

The article was about programming style. You write about team meetings. There is a disconnect.

Unless you plan to only work on personal projects by yourself, any kind standardization in your codebase will always require some kind of team meeting.

In some organizations, I'm sure.

Where I work, it might possibly come up in a code review, or be discussed when pair programming. I think most people would agree when pointed out that this rule makes sense.

Even when I've worked under fairly strict coding rules, no one has tried to tell me how to do this particular thing yet, so I was thinking I would start practicing this on Monday, since it seems reasonable.

Though in reality it will probably only come up 1-2 times per year, and most of my coding is in Python these days, so what am I even talking about...

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

#80

Earlier quoted context omitted.

There's also `if x in range(2,4)`, which translates to `if x in the interval [2, 4)`.

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 (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

So it may be a O(1) operation under the hood.

Post reply on HN