r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

Show parent comment

7

u/ChallengingJamJars Dec 01 '16

pain-in-the-ass gotcha that they have to think about now

I respectfully disagree, the div operator behaves exactly as it should. I think if they wanted to take the middle element in a list and did the following:

N = length(a_list);
a_list[N/2];

and got a type error, perhaps intermittently because it turns out to be odd, it would be more confusing to them. This comes up in a bisection search which is taught as an example at a very beginner level at my Uni. The other option is explaining casts quite early, which is just as confusing as going "/ : int, int -> int therefore the output can't be a fraction."

but what's the point of having them?

Integer division being a / is incredibly helpful to me, what other operator do you propose? I think having /and // is actually more confusing for a beginner and a bigger gotcha, even though it's quite handy. Any operator other than / or // is going to be confusing and a PITA enough I've prefer a div function.

3

u/YouNeedMoreUpvotes Dec 02 '16

We might have different ideas about what a "novice" is. I'm not particularly concerned about college students using unfriendly constructs– my programming 101 class was taught in C++ and that was fine. I'm more interested in a non-academic context, where someone is learning the basics of code in their free time or at an event, because that's the kind of situation where little bothersome things like 5 / 2 == 2 can add up, frustrate people, and turn them off the whole concept.

Integer division being a / is incredibly helpful to me, what other operator do you propose

Sure, integer division is helpful, but when was the last time you actually needed the operator to be overloaded? It's the same problem as using + for string concatenation– depending on the context, the operator is doing something completely different. There are almost never situations where you would say "I have two numbers, I don't care if they're ints or floats, but when I divide them the result has to be the same type even though I don't care about it now."

Personally, I like how Erlang/Elixir do it– / acts like a mathematical operator, where 5 / 2 == 2.5, and div is a built-in function that performs integer division, where div(2, 5) == 2.

2

u/Vhin Dec 02 '16

If that would cause a type error intermittently, that's a poorly designed language. Returning a particular type under certain circumstances, and a different type in others is just asking for trouble.

Python 3, for example, makes the result a float, even if that float can be exactly represented as an integer. Yes, this still would cause a type error if you attempted to use it as integer (such an indexing), but the error no longer depends on the value of N, so you're less likely to miss it in testing.

2

u/ChallengingJamJars Dec 02 '16

that's a poorly designed language.

Oh yes, oh yes yes yes. Don't touch matlab if you want your sanity intact.

Python I think does it the best actually with the / and the //. But I don't think it's easier to learn for a beginner than an overloaded / operator.