- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
In a language that allows variable numbers of method parameters, your example could read like that:
cout.write(Stream.hex, a, "--", b, Stream.dec, c, Stream.endl)
;Admin
Grrr, the forum ate the 2nd part of my post...
On the other hand, if the toolset is too rich, there are too many different ways to do something, so programmers have a hard time reading other programmers' code. Most programmers choose their subset of the toolset, so they are not familiar with all features of overly comprehensive languages like C++
Admin
Just to be argumentative, 1.5 has autoboxing, so the int would be promoted to an Integer, and .toString() would work. (I think so, anyway... it may not be promoted when you try to call a method on it; I don't particularly care for Java, though, so I won't test it).
Admin
Elegant. And unfortunately unusual in "live" languages.
Philosophical dispute: "freedom" versus "safety". Of course, if there was infinite ways of doing something you'd have an infinate number of different solutions. However, we do not see more of this in C/C++ production code than in other languages. The larger toolset of C++ means pragmatically that a wider set of problems can be effectively addressed in the best way, not that the code will tend toward chaos. Yes, even though you become advanvced in C++ you might still find things you do not know about, but this does not mean that you (1) will be unable to understand the code, nor (2) that you personally would be less productive for not knowing about that feature beforehand. Think of it like this, would you prefer to speak a natural language, say English, restricted to a subset deemed easy and harmless enough for someone's definition of "proper use", or accept a more unrestricted tounge for greater application, freedom and efficiency in particular areas at the price of knowing that there are aspects you might not understand? This captures the clash of philosophies; some would prefer the narrower language, while some prefer the wider. And some are forced to use the narrower but work from within to extend it, which, if they are successful, often results in the extensions breaking the narrower language's original principles.
Admin
Java5 as well as C# allow variable length argument lists, as well as auto boxing, so in those languages it's an easy execise to implement that.
You don't need knowledge of templates to write C++ programs, in fact, they did not work well in some of the early compilers.
But without that knowledge I doubt you can understand programs that use templates extensively.
Adressing everything in the best way is a desireable goal, but IMO readability is the most important goal, since most programs are more often read than written. Having too many features in a language threatens readability just as much as a lack of good (powerfull, elegant) features.
Since I'm not a native speaker, my usage of the English language is naturally limited to a subset known as "broken international English" ;-)
Keeping the balance is one of the more important tasks of any creator.
Admin
Apparently you do not even know what "syntactic sugar" means. Operator overloading IS syntactic sugar, period. BTW, "Feature X must be good because language Y has it" is no argument at all, and neither is appeal to authority.
Funny then that you are unable to argue against it, and instead change the topic.
Again you have to change the topic to make an argument, but fine: I disagree strongly. Descriptive names are the single most important aspect of clear and maintainable code. Comments can complement them and add detail, but the best comment is one that is unnecessary because names of the method and its parameters make the functionality obvious.
Admin
Interesting, but have you thought about why they do that only for such small numbers? I can tell you: because it can then be implemented by indexing into a table of premade Integer objects, which is faster than a hash lookup you'd otherwise have to do, and (far more importantly) because it limits the number of objects you have to keep in memory to do it. You do NOT want to keep millions of Integer objects in memory when someone creates throwaway objects in a loop, just because some of them may reoccur.
Admin
No, it is your statement here which is false whichever way you look at it.
parseInt() of course does not create an object, since it returns a primitive, which is why I said we should be talking about Integer.valueOf() instead, because you cannot invoke toString() on an int (not even with autoboxing). I think you mean valueOf() when you refer to the API doc, because it says something about caching, but if you look at the source code (in 1.5.0 at least) you'll see that, just like what we talked about in the other posting, it only caches values between -128 and 127.
But even then, Intege.toString() creates a new Object each time it is executed, at least in 1.5.0.
Admin
Good to hear! That would indeed be one way around some of the limitations, though I still hold that proper use of operator overloading would is more appealing. However, it is gratifying to hear that, for certain areas at least, roughly similar functionality exists.
The fundamental principles of C++ are efficiency, versatility and flexibility. Native language-like readability is not. These principles have never been compromised and constitute great aesthetics for me. Other languages might have different principles, like easy beginner accessibility or readability, and must thus be judged according to their own principles. (Interestingly enough, in the context of the Java vs. C++ origin of the heated debate (continuing around us as we speak), is that Java increasingly are losing grasp of its own original principles.)
Anyway, do you mean you think that templates is a superfluous "feature", an unnecessary bloat, on C++? I, and many with me, including many of the more influential bigwigs, consider templates to be one of the most powerful language innovations ever. There is no single feature in any language ever that has allowed me such code reuse, such generic programming, such flexiblity, and so aesthetic solutions as templates. The most problematic compiler, as usual, is MS's VC++; that one always lags quite far behind the Standard, which is a bother since it is a big platform that has to be considered.
Hehe, funny argument in context of my analogy :) Reminds me of the quote "the international language of academic research is Bad English".
Aye, however, most of us recognise that we are quite incapable of determining what would be a good balance. Thus, we make languages that suit ourselves, at the moment, and arrogantly believe that our preferences are representative of everyone's best. That is why I respect Stroustrup, because he accepted that there is no One Best Style (no absolute "balance", if you will), and accomodated most everything by leaving that to the programmers. The lack of such freedom is probably the reason for the big feuds from and within the Java camp: some people have found what they like and defend it entrenchedly because they are simply incapable of understanding that other people have different problems, needs or preferences, or don't consider it the best thing since toast; others continuously revolt due to feelings of claustrophobia. Basically, it has nothing to do with which language is "better", they're for different things!, it has to do with philosophies that has been channeled into a medium: some who loathed C++ due to agoraphobia loves Java for its clearer delimitations; conversly, some loathe Java due to claustrophobia.
Good discussion, btw.
Admin
IMO C++ carries too much C legacy with it. That hurts, especially since C++ is already bloated. As a result, some basic things (that were poorly implemented in C) like strings are re-invented in every framework.
I aggree that templates are a powerfull and usefull feature, but I think that they also add a great amount of complexity to C++ programs using them. But, as you have noted, a similar feature - generics - has also appeared in supposedly "clean" languages like C# and Java.
I see you point, but on the other hand, by leaving it all to the programmers (and frameworks like MFC), the style of C++ is somewhat blurry; actual usage, frameworks and "best practices" determine the actually used language, which can greatly differ between one site and another.
Admin
Hey, I like syntatic sugar. Writing
j = "[" + foo + "]"
to put brackets around a string is nice and simple. I never really got into Javascript too much, but I can recall doing something like
j = "[".concat(foo.concat("]"))
which isn't nice and simple. I've seen some hideous usages of overloaded operators, but when used in a logical manner, it's nice and simple. You can do hideous things with nearly any feature a language gives you.
(Case in point,TinyP2P - code that abuses some features in Python to produce a working Bittorrent client in fifteen lines, but would probably make Guido cry as it completely ignores the why of Python syntax and semantics.)
Technically dot notation is also syntatic sugar. I can access a GUI widget's attributes via
mainframe.panels.leftpanel.buttons.quitbutton.text
or
mainframe["panels"]["leftpanel"]["buttons"]["quitbutton"].text
If the framework designer has been nice, I can use dot syntax which looks cleaner and is a bit more intuitive. And yes, that's an incredibly obtuse example, but I'm just trying to make a point here.
Whole-heartedly agree. Hence why I hate languages that make get/sets compulsory, because then when I'm looking at a class description there's a bunch of methods under g and s that I have to scroll past. But logical attribute & method names free of weird prefixes are desirous.
Admin
Yeah, this is a common grieviance. I agree to an extent, the C legacy-compatibility requirement put some unfortunate restrictions on C++. So what is the conclusion here? All languages entail certain restrictions. In this light it should be considered that, originally, Java was a more aesthetic language in the sense that it was more strictly defined. This, however, is no longer true, and recent trends seems to indicate that it will grow increasingly less so.
As to that string classes are reinvented in every framework, that is a practice which should have ended with the STL string. MFC introduced its own CString (I think it is called) to the great consternation of most Win32 programmers, but then again MFC is not standard in any way other than that many Windows programmers are forced to deal with it.
Back to the topic: Cyresse provided some good examples for the inclusion and against the abscence of operator overloading in his post above. As a large part of this entire debate seems to revolve around the argument that operator overloads are really "nothing but" syntactic sugar, it should be considered that a large extent of what we now use in our daily programming is "mere" syntactic sugar. Assembler is sugar for opcodes. Englilshish commands are sugar for opcodes and assembler commands. Functions are sugar for jump statements. Classes are sugar for data structuring.
Yet, how many here would say that "classes are just syntactic sugar and are redundant"? Stricly speaking we do not need classes, but they make programming immensly more convenient and lends themselves to the addressing of particular problems. As do operator overloads. And as operator overloads, classes, when taken to far, can result in very strange code. As ammoQ above told us he choked on the C++ Hello World, in all honesty, Java's hello world with a main() artificially shoehorned into the class is very strange and forced syntax. I had great difficulties explaining for my gf how it worked and I finally made her understand by rewriting the code as it would be in C++... ;)
Anyone reading this who would consider that natural string operations (without violating the language's principles), natural arithmatic operations on mathematicalish entities and natural set operations do not constitute solid reasons for the benefits of operator overloading would be difficult to involve in a rational discussion.
Admin
Sorry for double-posting, but everyone else seems to do that too, here...
That is the whole point, in fact. There is no particular "C++ style", because there is no particular problem or field specifically targeted by the language. C++ was designed to be applicable in as many situations to as many problems as possible. A stricter definition ("style") would have hamstrung that ambition. One consequence is also that C++ has been put to use addressing more diverse problems than any other language, except C; however, another inescapable consequence is that a plentifora of personal and targeted styles have resulted.
Limiting a language to one "style" probably creates greater consistency and code familiarity but it also limits its potential application space.
Admin
Classes (with inheritance, virtual methods etc.) are obviously more than syntactic sugar. It's rather difficult to do the same e.g. in C.
Aggreed. Just like templates/generics are great for all kinds of containers. But then there is always this tempation: "If operator overloading does such a great job for mathematicalish entities, why shouldn't I use them for everything else? Saves a lot of typing..."
Admin
<FONT face=Arial size=2>Just to be difficult, I use Python, which is OO. And, it's implemented in C.</FONT> [:P]
Admin
Gammy bloody font sizes.
Admin
True, but I guess it was a lot of work to implement Python. I can hardly imagine a C programmer implementing Python every time he wants to write an OO program.
Admin
As is anyone implying that people who disagree with him are necessarily irrational.
My opinion on this is that "mathematicalish entities" are the only really solid reason, and that's it. Using '+' to concatenate strings or add an element to a set may look "natural" to you, but it really isn't - what's the meaning of '-' between strings? What's a negated set? What's the result of adding a null pointer to a string? Or a set?
And for that one case which most programmers very rarely encounter, do we really need a special language mechanism?
Admin
Probably yes. Good thing no-one has suggested that, then... Point was really tautological - reason is reason: anyone who would disagree with that rational arguments merits consideration would almost per definition be unsusceptible to reason.
We "add" strings to other strings. We subtract a subset from another set. And much programming revolve around arithmatic-like problems. To once again boil this down the core: the facility is there becuase it is the best solution for certain problems - not all problems. It is up to you not to misapply it. And I deal with these situation in my daily programming. Apparently you do not, and I respect that you have little use for this facility. Please show the same repect and acknowledge that you're not prepresentative for all programmers and that quite a few of us DO need it.
After all, if no-one used it, if it was really redundant or only problematic, it wouldn't really be debated. Sort of like C++ and Java, wouldn't you agree?
Admin
Sets are a really bad example to use there, since operations on sets are well defined. Sure, the symbols "+" and "-" might not be the standard ones used by mathematicians, but using "setA + setB" for union and "setA - setB" for set difference is entirely reasonable. The negation of a set could then quite reasonably be the complement set (which would, admittedly, be tricky to represent in some circumstances.)
Adding a null pointer to a set? That would depend on how you decide to implement sets. If a null pointer represents the empty set, then the operations will work fine with it, since the empty set is just a set with nothing in it. If, OTOH, you have an object EmptySet (for example) to represent the empty set, then an attempt to treat a null pointer as a set object would result in a NullPointerException, or a segfault, or whatever form of "Go boom! now" the language you're using does when you try to dereference a null pointer.
As for strings, why does the fact that "-" doen't have an obvious meaning affect the meaning of "+"?
Admin
Because it's "natural" that if you have addition, you also have subtraction.
The first who breaks down laughing at the statement above gets a cookie. :)
Anway, my point is that arguing with certain operations being "natural" and therefore "naturally" represented by certain operators doesn't hold all that well except for a pretty small number of concepts. And that the absence of a feature that has only a small range of non-abusive uses is a very poor reason to denigrate a language.
Admin
That operator overloading only has a very small set of "valid" utility is subjective. I for one have used it in many and diverse situations, yet I have yet to have any programmer consider my solutions improper. I'd even go as far as to say that given forethought, sanity and judgement on the part of the programmer the set of valid uses for operator overload (to reflect "natural" syntax, to enhance code efficiency and readability, to represent certain problemsand data, etc) is principally infinite. Again, it is very arrogant to presume that one's understanding or usage pattern is representative of the situation "as it really is" or should be. However, humility is almost always the first victim when discussions turns to debates. Now, this might be false, but I get the feeling that you argue rather from idealism than from pragmaticism. And is so, perhaps is should be considered that another of the very code C++ principles is pragmaticism rather than idealism.
Conversely, related to your argument, one could just as well argue, from the other side, that if there is a well defined set of common problems to which a certain facility is an obvious solution and that solution does not complicate the compiler (which operator overloading does not - it is "sugar" for functions, as has been argued here) then it is neglectful, depending on the facility perhaps even criminally neglectful, to withold that solution from the programmers.
Anyway, I can't speak for the rest of the posters here, but I at least have not "denigrated" Java for lacking operator overloads. Yes, I miss the feature when I code Java, and I do consider the operator overloads for Java's strings an unaesthetic break with the languages's principles, but ti is a design principle of an established language and I live with it. Again, there is no debade whether Java or C++ is "better", they are for different things. Pragmatics.
Admin
I'd say that it is important to find a balance between idealism and pragmatism. Unmaintainable ad hoc kludges are very pragmatic, or at least they seem to be to the people who write them, even if they are not, in the long run.
And isn't it quite pragmatic to forego an easily abusable feature that you might be useful to you, but not every often, because you'll ALSO have to maintain code written by people who will abuse it horribly? The usual answer is that they can and will write horrible code anyway, but at least it won't be quite as complex and easily overlooked.
Admin
As I said, and as is shown reply upon reply, this is a fundamental difference in philosophies. In the one design philosophy the programmers are trusted, in the other programmers are trusted to be incompetent. With freedom comes responsibility. When C++ programmer have leveled enough to implement operator overloading they usually understand how to use it properly. It is no use trying to protect against Machiavelli, and it often imposes grave restrictions to protect against Murphy. I myself prefer languages where the programmer is trusted.
Point in case, look at the code posted at TDWTF: it is basically Java, C#, VB and JavaScript/SQL. C and C++ is dramatically underrepresentated. The feature basically is very seldom misapplied in real life. That is the reason I suggested you might be arguing more from ideology than pragmaticism.
I, and most experienced programmers, concede that C++ is a difficult language filled with powerful facilities that can be horribly misapplied. And newbie programmers sometimes produce monstrosities. However, most programmer, and principally all experienced ones, put the power of these features to proper use. Likewise, Java too incorporates powerful (framework) features, as well as inconsistencies, strange design decision and mild to severe restrictions. And as for C++, newbie Java programmers often misapply language features and toolsets, and produce utterly repulsive freaks of codeture. However, most experienced programmers will not, instead producing beautiful and functional code; as will all programmers with the proper skill and intellect regardless of platform.
Java, too, has toolsets that lends to horrible and incomprehensive code. These, however, are often outside the language; Javabeans (not to speak of enterprise beans!), and the myrriad of other frameworks becoming required practice, as well as the enthusiastic but often misguided berserk creation and application of patterns. In my opinion, this is a severe issue of much more acute significance than that some malicious coder with a pink slip and a grudge or a 1337 kiddie misapplying operator overloads.
Admin
Unfortunatly, it would also be quite reasonable -- but WRONG* -- to expect that
setA - setB = setA + (-setB)
* unless setB is the complement of setA
Admin
Hard-coding strings is as bad as hard-coding numbers.
Admin
For natural numbers, subtraction is _not_ defined. Of course, we tend to apply the subtraction for integers also on natural number (where it can be applied) but that is not at all part of the mathematical definition of natural number.
Gimme gimme gimme ;-)
Admin
the following also doesn't hold in general:
setA - setB + setB = setA
setA + setB - setB = setA
setA + setB = setA -> setB = 0
setA - setB = setA -> setB = 0
(setA + setB) - setC = setA + (setB - setC)
(setA - setB) + setC = setA - (setB - setC)
Admin
Here ya go: [pi]
(Always wanted to try that, and it's the closest we have)
Admin
Thinking about it, the bitwise operators "&", "|" and "~" would be much better symbols for set intersection, union and complement.
Admin
<FONT face="Courier New" size=2>ampersand was employed successfully by the legions of cobra-la.</FONT>
Admin
I guess if you really want to you can count to Integer.parseInt("010", 8), or Integer.decode("010").intValue().
Admin
In addition to the obvious, I don't think anyone has commented on the fact that it appears to be using Vector (elementAt) rather than List. Of course, there could be a good reason for that, but in my experience Vector is often used when synchronization is neither needed nor wanted, by people who just don't know any better. Given the other things wrong with this code I'd be willing to bet that's the case here.
Admin
Why? The normal way to do
j = "[" + foo + "]"
in Javascript is
j = "[" + foo + "]"
Admin
Absolutely. I'd say that in upwards of 95% of all cases where Vector is used, it's because the programmer got used to it reading code and tutorials written by people who got used to it reading code and tutorials written when List didn't exist (i.e. before 1999).
Admin
*blush*
gotta check some of my sources...
Admin
<nitpick>
The == operator in Java - when used with reference variables - compares reference values, not objects.
</nitpick>
Admin
<FONT face=Garamond size=6><FONT style="BACKGROUND-COLOR: #7fffd4">I<FONT color=#ff1493> love you all you guys are my bestest friends in the whole world!!! I love you sooo much</FONT></FONT></FONT>
Admin
<FONT style="BACKGROUND-COLOR: #000000" color=#ff0000 size=6>you are nasty you perv. Yuck!!~~~ ~~~ ~~~~ ~~~~ ~~~~~ ~~~ ~~~~ ~~~~ ~~~~ ~~~~[:'(]</FONT>
<FONT style="BACKGROUND-COLOR: #000000" color=#ff0000 size=6>Pewwwwwwww</FONT>
<FONT style="BACKGROUND-COLOR: #000000" color=#ff0000 size=6>o well i love ya</FONT>
Admin
<FONT style="BACKGROUND-COLOR: #ffffff" color=#ffffff size=6>maybe the 3 is bad and he is gay.</FONT>
<FONT style="BACKGROUND-COLOR: #ffffff" color=#ffffff size=6>(jkasdjh;jd::klghd(f fmd<,fjakkuh88756888/vV?sdjslri</FONT>
<FONT style="BACKGROUND-COLOR: #ffffff" color=#ffffff size=6>uehf;a t</FONT>
<FONT style="BACKGROUND-COLOR: #ffffff" color=#ffffff size=6>i dont understand this site at all~~~</FONT>
Admin
If losing sight of its origins means dropping a carnival funhouse set of evolutionary extensions to the class structure in favor of a well-planned, rational formulation, and a set of powerful, easy-to-use, and nice-looking GUI apis, then I'm all for it. Sadly, all 5 gives us is little more than hodge-podge language and api evolution in random directions. (I'm assuming you mean ~Java2 origins, which is "Java" to most everyone, and not Java 1 era, which really was fairly simple and elegant for the needs of embedded software. It only became a mess when they tacked a ton of extensions on it, called it cross-platform, and got the create-and-depreciate train rolling.)
Admin
Which I personally think is very interesting. So, in the Java vs. C++ spirit, of this conversation, it should be noted that C++ has never compromised its original principles, and the new standard is scrutinising itself incredibly hard to maintain that.
The original Java adaptations were good and beneficial, but given the latest iterations the language has become unwieldy, unfocused and actually quite difficult for beginners -perhaps arguably even as difficult as C++- which is amusing since it is increasingly used as the first language, replacing C++, in CS edu.
Several of my Java developer friends are expressing frustration over the current state of affairs, some going as far as to say that "Java is the legacy language of tomorrow starting today", and some are already looking for or turning to replacement favorite languages, like Ruby.
Admin
how do they get hired..
Admin
Simple:
He just mixed bit with byte. He thougth that int just has to bit#s : 00 0 01 1 10 2 11 3
and numbers wich are geater than 3 must be stored in a text variable of any sort...