Thread: Programming!
View Single Post
Old March 2nd, 2013 (10:56 PM). Edited March 2nd, 2013 by twocows.
twocows's Avatar
twocows twocows is offline
Mostly Benign
  • Gold Tier
Join Date: Mar 2009
Location: Michigan
Age: 27
Gender: Male
Nature: Lax
Posts: 4,172
Originally Posted by Archenoth View Post
Cool... Visual Basic is fantastic for hammering out really fast graphical programs... And Intellisense can really help you out with it's completions since the language is really simple.

The thing about that style of operator overloading is that C++ isn't the only language that has it, it's in other C-like languages as well. Java has "string" + "string" concatenation overloading for example, and the other overloads I saw mentioned are in C to begin with. (Like var++, or *pointer)

DISCLAIMER: The following is merely my opinion... One we obviously don't share.

The way I see it, provided you have good type handling or management, you can use overloaded operators in ways that are quite understandable and not horrible. It's all in how you present your code. For example, I can write C code that is non-obvious in it's operator usage too, consider the following:
int i = 10;
while(i --> 0) // While i goes to zero
  printf("%d\n", i);
Know why this works? (Try to figure it out before clicking the spoiler.)
This may make more sense:

while(i-- > 0) // While i goes to zero
  printf("%d\n", i);

Another example:
result = value**count;
That is multiplying "value" to the value that a pointer called "count" is pointing to. And not dereferencing a pointer to a pointer. Sure you probably knew that, but it is more readable to do the following:
result = value * *count;
The same applies for overloaded operators. For example, in JavaScript, if you saw the following code:
string = "That " + name + " person is a cool guy";
You could very easily determine that that was string concatenation in the same way where if you saw this:
total = numGuests + numStaff;
You could reasonably assume that it is arithmetic.

Anything can be horrible if used horribly, but if you use the features you have available to your advantage, you can write good code in mostly anything with structure. You just need to write your code in a way that is intuitive.

In the end, the things that are horrible to you end up coming down to your own preferences. I can respect that though.
Your examples seem more like misleading spacing than anything. The problem I have with operator overloading is that it introduces ambiguity with regards to a given operator. You have to look at the context to figure out what it does, and that's just stupid. What an operator does should be self-evident; it should mean the same thing in every context. Overloading string concatenation is fine I guess because it doesn't do this; you're still essentially "adding" two things, even if internally it's completely different. But a lot of times this isn't what happens; operators are overloaded to mean all sorts of stuff that isn't even remotely related. This is horrible. And letting the programmer do it at his leisure is even worse; at least in C you can guarantee that * is going to mean one of three things and not fifteen other things that the programmer might have defined somewhere.

Speaking of *, C is not immune from this criticism; I already hate that * is overloaded to mean three different things in C by default. * means "multiplication" in almost every mathematical or programming language in some context, so that's fine. However, it's also taken to mean "dereference" in another context. I don't like that; there should be a symbol for dereferencing unique from one already widely used. It's confusing. However, that's not even the worst part. The absolute worst part is that it's overloaded again to mean "reference (noun)." This is completely ridiculous. If you're going to overload an operator to mean "the following thing is a reference," DON'T MAKE IT THE OPPOSITE OF WHAT IT MEANS IN ANOTHER CONTEXT! Overload & if you're going to do that! & already means "reference (verb)," at least stay consistent! This is probably why so many newcomers to C are confused by pointers (it's certainly why I was thinking back): the syntax is utterly absurd. The worst part is, if operator overloading didn't exist, this problem wouldn't exist at all. You could identify at a glance what the purpose of an operator is.

For instance, let's assign a unique operator to each of those operations.
In ALL contexts:
* means multiplication
& means reference (verb)
# means dereference (verb)
@ means reference (noun)

Wow! That was so hard! These are all symbols already conveniently available and, based on a quick glance at this page, unused. Let's take a look at some arbitrary C code and see how this changes things.

int main()
    int a = 5;
    int b = 6;
    int c = a * b;
    int @cr;
    cr = &c;
    a = #cr;
That might seem kind of weird at first because you're used to different uses, but look at it closer. Everything makes sense at a glance; you don't have to look at what's surrounding the operator to figure out what it means. This makes code so much more readable. It's impossible to get confused by this unless you just don't understand the underlying concepts.

Again, though, I'm... all right with operator overloading when it's intuitive. If you wanted to change @ to an overloaded version of &, that's fine with me, since it's intuitive; & means "address" in my head, whether it's "address of" or "this is an address." Past that, though, it just gets messy. And it certainly shouldn't be in the hands of the programmer; it should be defined at the language level and not be changeable.
Political Simulator
Reply With Quote