Nice Quote

Pages: 12
Actually are you commenting on the lines of code needed to produce a simple "Hello, World" on screen ? If that is so, then all along it has been that way. Interpreted language like Perl, Python, bash shell etc provide the concise and compact syntax. We should compare orange to orange as Java and C++ are compiled languages. (Note we still need to compile Java programs just like C++).

As for functions outside classes, there is some history. When C++ was created, it must be in such a seam-less integration with legacy C code so that is why global functions are supported in C++. Java on the other hand is created to be a pure OO-language from ground up and that is why you can't have global functions.

In a way one can write procedural+OO, procedural, OO in C++. This is powerful but then complexity sets in. Imagine your first language is Java and then you come across to C++, you will be stumped why global functions exist isn't it ? C++ allow the developer too much power and if power is not rein-ed in proper, havoc and chaos will occur.

PS Btw I not anti-C++ I use both Java and C++ depending on business requirement needs.



C++ allow the developer too much power and if power is not rein-ed in proper, havoc and chaos will occur.

This is exactly what I don't like about Java. It is not a language's place to tell me what to do. I like a language which I command, and it does what I say, for better or worse. Since George Orwell's book '1984', futurists have feared languages designed to limit one's thoughts. Java is such a language. It forces you to use OO methods even when it is patently ridiculous to do so.
Bottom line: I command, and the computer obeys, is the natural order.
Reg Braithwaite wrote:


"It is a logical impossibility to make a language more powerful by omitting features, no matter how bad they may be."

Is this obvious? So how do we explain that one reason Java is considered “better than C++” is because it omits manual memory management? And one reason many people consider Java “better than Ruby” is because you cannot open base classes like String in Java? So no, it is not obvious. Why not?

The key is the word better. It’s not the same as the phrase more powerful. The removal or deliberate omission of these features is an expression about the idea that programs which do not use these features are better than programs which do. Any feature (or removal of a feature) which makes the programs written in the language better makes the language better. Thus, it is possible to make a language “better” by removing features that are considered harmful, if by doing so it makes programs in the language better programs.

In the opinion of the designers of Java, programs that do not use malloc and free are safer than those that do. And the opinion of the designers of Java is that programs that do not modify base classes like String are safer than those that do. The Java language design emphasizes a certain kind of safety, and to a Java language designer, safer programs are better programs.

“More powerful” is a design goal just like “safer.” But yet, what does it mean? We understand what a safer language is. It’s a language where programs written in the language are safer. But what is a “more powerful” language? That programs written in the language are more powerful? What does that mean? Fewer symbols (the “golf” metric)?




It forces you to use OO methods even when it is patently ridiculous to do so.


You can code everything with statics if you wish. If there is a task, where using OO is ridiculous, you probably can't do anything about it in C++ either, because except a slightly more concise syntax for structural programming, it doesn't offer any stronger abstractions over Java. So, I it seems you are nitpicking here. I totally agree with sohguanh on this.
Last edited on
"You can code everything with statics if you wish"
Yes, but they still must be inside some class. What I would like from any good language is:
- global functions, i.e. sin(x) not Math.sin(x)
- functions that take actual functions, not stupid functors, as arguments, like qsort(list,num,siz,compare);.
- finally, any interpreted language should have lambdas, like set_callback(void fun(){event_flag=1;})
Hell, even C with gnu extensions supports a kind of lambda:
1
2
3
4
5
#define lambda(type, args, body)\
  ({type name args body &name;})
/*use like this
set_callback(lambda(void,(void),{flag=1;}));
*/

global functions, i.e. sin(x) not Math.sin(x)


What prevents you from doing a call to sin(x) in Java instead of Math.sin(x)? Just add a proper import, just as you #include in C or C++. BTW: If you code global funcitons in C++, you should enclose them in a namespace. Global functions in global namespace are evil. So you end up writing exactly the same amount of code in C++ and Java.


functions that take actual functions, not stupid functors, as arguments
finally, any interpreted language should have lambdas


Semantically, there is no difference between a function and a stateless functor object. There are lambdas in Java - anonymous classes (which are more general than lambdas). The syntax is extremely ugly, but works. On the other hand the C/C++ syntax for passing function pointers or creating functors is just as ugly and verbose, but in different ways. If you want true functional language, with short and nice syntax for creating lambdas, just take a language that was designed to support functional programming. C++ and Java were not, so criticizing them for lack of elegant functional support is a little unfair.

Last edited on
Global functions are evil.

Why?
the C/C++ syntax for passing function pointers or creating functors is just as ugly and verbose

No, it's the same as for passing pointers to anything else: set_callback(&function);
Maybe you mean the syntax for defining a function pointer:
void (*callback)(void);
This is entirely consistent with the meaning of * in all declarations, and it is required because x() has a higher precedence than *x. Not ugly at all, and certainly not verbose.
Last edited on

Global functions are evil.
Why?

Global namespace pollution. Linking problems.


void (*callback)(void);


Yeah, just remove one () and you get a function prototype declaration. The problem with this is that it is too similar to other types of declarations and additionally it cannot be easily seen, what is here the type, and what is here the variable. The type of the newly introduced symbol is given at the both sides of the symbol. It is not even ugly. It is crazy. But ofc, I can live with this.

A syntax like that: (void -> void) callback, or even void(void) callback;would be much more readable IMHO. However, you were talking about lambdas. The #define, or Boost::Lambda or even new C++0x lambda syntax is nowhere so elegant and simple as the <insert any functional language here> approach. Also, show me what you do in your "structural" approach in C (no functors or boost::bind allowed), when you need to pass a partially applied function somewhere... :P
Last edited on
The type of the newly introduced symbol is given at the both sides of the symbol.

Yes, like an array: int hiscores[10]; You must realize that we are simply using various operators to define the type, in a way analogous to the way they are used on the symbol later. These "type operators" have the same syntax and precedence as the analogous operators in code.

But you're still not addressing the true issue: I can do something in C, a 40-year-old language, that you cannot, cannot do in Java. Specifically, I can take any function I like, and pass it into another function, provided that it takes and returns the right types.
1
2
3
4
5
6
double sumofresults(double x[],int n,double (*f)(double)){
	int i;
	double sum=0;
	for(i=0;i<n;i++)sum += f(x[i]);
        return sum;
}

And now I can put any function from math.h into it, and it will work. This is to my knowledge impossible in Java.
Last edited on
So you should improve your knowledge of Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface F {
  double apply(double arg);
}

double sumofresults(double[] x, int n, F f){
	int i;
	double sum=0;
	for(i=0;i<n;i++)sum += f.apply(x[i]);
        return sum;
}

// and a call:
sumofresults(x, n, new F() { double apply(double arg) { return Math.sqrt(arg); }});



The only difference is syntax, which is a little more verbose in this case than in C. But they do the same. Note that the { double apply(double arg) {}}fragment is autocompleted by all IDEs, so you don't have to type this.

Anyway, I think we are driving off topic too much. This is about quotes, not about C function pointers vs the rest of the world.
Last edited on
C++ allow the developer too much power


No offense, but this is just...a really dumb statement. What if I *need* that much power?

Anyway...
Someone intelligent wrote:
Anyway, I think we are driving off topic too much. This is about quotes, not about C function pointers vs the rest of the world.
Alan Turing wrote:
A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.
Topic archived. No new replies allowed.
Pages: 12