One thing I dislike about Java:
- Methods often take a reference to an Interface of some kind (Runnable, etc) as parameters
- Programmers often use anonymous classes to provide those parameters
Those two things combined can lead to awful awful code. Granted, that's a bit the fault of the program design and programmers.
For instance, generics are safer than, for example, in C++.
While C++ can be a huge pain at times, those "unsafe" generics (and other features) can also be so useful.
For example a function/method taking a pointer (or better smart pointer) to a Runnable object (well a C++ equivalent):
Code:
void function(Runnable *runnable)
{
// ... Do stuff ...
runnable->run();
}
Using a combination of templates argument deduction and overload resolution something like this is possible:
Code:
void callback(int n);
int main()
{
function(Closure::from(callback, 5));
return 0;
}
Then
function can invoke
callback without directly knowing it's type signature.
Closure is a namespace with several overloaded
from functions which can create closures from various types of function/method pointers and bound argument combinations.
In Java the same would require to wrap the
callback method into a class first (which the C++ version also does, but it's automatic). Problem is that each method needs to be wrapped in such a way which can get annoying. And anonymous classes become messy as mentioned above.
Lambdas solve the above issues for both languages, but only available with Java 8 or C++11.