Java is a statically typed language which couldn't care less for type safety



  • Take a look at this doc page. Standard library observer pattern implementation - yay!

    ...or nay? I mean - observer is basically a callback that gets called when something happens. In parameters, it gets data about what has changed. The kind of data is pretty arbitrary - there might be none, or there might be dozen separate values, depending on what you need. Right?

    Well, not in Java. Here, observer registers at observable object and the only data it can get is the object it is listening to. Literally. Every time the observable calls observer's callback, it passes its this pointer as parameter - as if the observer didn't know what hit him (if it indeed doesn't, it probably doesn't care anyway). So the only way to pass a value to callback other than this, you have to make member variable, set it before calling observers and let them read it away.

    Oh wait, there's overload. Instead of just the useless this pointer, you can also call the observer with useless this pointer and some other object. Like, anything. Because the method accepts objects of type Object - which is, any object, at all. One does not simply have an observer that cares about only one kind of data. You must downcast from anything-type to type-you-care-about every time, hoping that the observable you registered to isn't a dick and hasn't called you with ParameterModeHelper instead of NodeList. If you want to be safe, you must use RTTI utilities and do instanceof every single time. Because there's no other way to guarantee type safety in statically-typed language.

    First rant here. Rate.



  • Java is a statically typed language which couldn't care less for type safety

    Thank You!!!

    I also love how Java is usually held up as an exemplar of static languages and compared against, say, Ruby (a decent example of the benefits of a dynamically-typed language).



  • The kingdom for a proper multimethod!

    @Gaska said:

    You must downcast from anything-type to type-you-care-about every time, hoping that the observable you registered to isn't a dick and hasn't called you with ParameterModeHelper instead of NodeList. If you want to be safe, you must use RTTI utilities and do instanceof every single time. Because there's no other way to guarantee type safety in statically-typed language.

    Java: Replicating the Mistakes of Others™ since its inception.

    Or, more seriously, this interface predates Java generics support. Not that generics in Java are all they're cracked up to be...

    @Bort said:

    Java is a statically typed language which couldn't care less for type safety

    QFT. Modern-style C++ actually does a better job of type safety than Java does, somehow. This should terrify you. Also, for those who want a real statically typed language, ask our resident Haskell @Captain.



  • Leave C++ alone. If not for legacy stuff, it would be very good language.


  • Discourse touched me in a no-no place

    @Gaska said:

    Leave C++ alone. If not for legacy stuff, it would be very good language.

    And if not for the fact that compiling and linking anything written in C++ can be an ordeal, I might possibly agree with you.



  • Well, Java also has generics that really just there to automate casting between some type and Object, so this seems like nothing surprising.

    We have things a bit better over in the .NET world, @blakeyrat and me.



  • @Gaska said:

    Leave C++ alone. If not for legacy stuff, it would be very good language.

    If not for legacy stuff, it would be called "C#". C++ is basically C# without hundreds of features and with hundreds of kludges to make three guys who keep compiling their C programs with g++ 20 years after it's been relevant happy.

    The only use case for C++ is to teach people C without having to explain printf syntax on the Hello World lesson. And it's only downhill from there.

    @Magus said:

    We have things a bit better over in the .NET world

    Definitely. Though AFAIK Java has been improving with v8.



  • @Magus said:

    We have things a bit better over in the .NET world, @blakeyrat and me.

    Well yeah, we've already determined that C# .NET is the best language to date.

    speaking of which, is anyone else wondering what @blakeyrat's marital status is?



  • @Gaska said:

    Leave C++ alone. If not for legacy stuff, it would be very good language.

    Modern C++ (i.e. standard library style) is a very underrated language from a technical standpoint -- you can write performant, reliable code in it provided you have a reasonably modern compiler and build harness at hand, and aren't stuck with sucky legacy APIs/libraries.

    However, managed code is easier on code monkeys, so that's what the business folks embraced.

    @dkf said:

    And if not for the fact that compiling and linking anything written in C++ can be an ordeal, I might possibly agree with you.

    This is either a function of:

    1. Using a compiler that should be dead and buried by now (Can VC++6 die already?!?!?!), or
    2. Using 80s era build tools (such as Imake) that should be dead and buried by now.

    Modern, conformant compilers (at least to C++03), when paired with modern build harnesses, don't blow up randomly in the way you think they do.

    @Maciejasjmj said:

    If not for legacy stuff, it would be called "C#". C++ is basically C# without hundreds of features and with hundreds of kludges to make three guys who keep compiling their C programs with g++ 20 years after it's been relevant happy.

    No, C# is "Java, with some of the bad bits fixed before Java was able to figure out it had sinned". Also, I want to see what sort of gnarled nightmares would come out of trying to interface to your nearest friendly railroad codeline from within Java...or maybe, on second thought, I don't, because it'd likely never work correctly!

    I will admit that C# has been getting better and better faster than Java has, but it still has significant downsides that folks have forgotten about due to the incessant preaching of a growing choir of object-worshippers who do not realize the damage they have wrought on programming.

    Filed under: get over your object fetish already, mmk?



  • @Magus said:

    Well, Java also has generics that really just there to automate casting between some type and Object, so this seems like nothing surprising.

    Yeah, TIL; sadly, it'll mean that the JVM will never have a properly statically typed language.



  • I love C#, but even I don't think that.

    The .NET language I most want to learn is Nemerle, but I'll probably learn Lisp before then. Sadly, the CLR does not offer an object system on par with that of one of the oldest languages, to the point where you cannot port CLOS to .NET.

    Ah well, Nemerle is probably close enough.

    Just having tail-recursion optimization is huge, since most C# devs are terrified of recursion for the stack's sake.



  • @Magus said:

    Sadly, the CLR does not offer an object system on par with that of one of the oldest languages, to the point where you cannot port CLOS to .NET.

    Yeah: the OO fetishists conveniently forget about the existence of such things as CLOS. :P



  • @tarunik said:

    Yeah: the OO fetishists conveniently forget about the existence of such things as CLOS.

    I was about to rant about it, but I guess I'll just wait for @blakeyrat to call you out.


  • Discourse touched me in a no-no place

    @tarunik said:

    Modern, conformant compilers (at least to C++03), when paired with modern build harnesses, don't blow up randomly in the way you think they do.

    This was Clang 3.5 and 3.6 that were detonating in some way on some header file completely not under my control (something to do with an enable_if template, which isn't something explicitly used in any of the code I was trying to build). 3.4 worked fine, no warnings at all (except for a dumb one because of someone who put C++ code in a .c file, and I've not looked up how best to change that yet…)

    It's all damn shame; I wanted to build against a later version of LLVM to pick up some extra optimizations. When I grumble about C++, I'm not just yanking peoples' chains: I grumble because it's causing me pain and I've no idea how to fix it.



  • Come now, I already invoked @blakeyrat! I know you're impatient, but don't you think once is enough?

    (Yes. I know.)


  • Discourse touched me in a no-no place

    @dkf said:

    And if not for the fact that compiling and linking anything written in C++ can be an ordeal,

    [code]c:\windows> copy con dkf_is_wrong.cpp
    #include <iostream>
    int main(void)
    { std::cout << "whoo hoo!" << std::endl; return 1; }
    ^Z
    c:\windows> cl -EHsc dkf_is_wrong.cpp[/code]

    QED.


  • Discourse touched me in a no-no place

    @dkf said:

    except for a dumb one because of someone who put C++ code in a .c file, and I've not looked up how best to change that yet…

    I assume "change the extension" or "move the code into a different file" aren't options.


  • Discourse touched me in a no-no place

    @Magus said:

    Come now, I already invoked @blakeyrat! I know you're impatient, but don't you think once is enough?

    Ah, he was probably up so late last night, what with that carefree funemployment lifestyle that he isn't awake yet.



  • The funny thing is that I don't think this class is used for anything. I have newer seen it used for anything in Java, and awt/swing have their own observers so they can pass type-safe information.

    But it is just an useless class you can forget. Java got a lot of those -(



  • @FrostCat said:

    dkf_is_wrong.cpp

    hehe



  • Today, my CS552 midterm had a question that required me to explain how "Java Object Oriented Programming" made programs run faster and better and stuff.

    I wrote an answer on the top half of the page, then in tiny letters near the bottom:

    Because no other turing-complete language ever made has ever been able to do that.


  • Discourse touched me in a no-no place

    @FrostCat said:

    I assume "change the extension" or "move the code into a different file" aren't options.

    Changing the extension is what I'll probably do. Just not got a round tuit yet.


  • Discourse touched me in a no-no place

    Since I'm writing a JIT engine for another programming language that needs to run on multiple platforms, MSVC is a complete non-option. You might be satisfied with being a jerk on just Windows, but some of us have grander horizons than that.


  • Discourse touched me in a no-no place

    6. I could write the same code in GCC if I remembered the correct options, if any are required.
    3.



  • @tarunik said:

    Modern, conformant compilers (at least to C++03), when paired with modern build harnesses, don't blow up randomly in the way you think they do.

    You forgot "3. Use lots of template stuff" which if you're not careful can lead to massive compile times (I've seen a single compilation unit take more than two minutes to assemble, though... that was a "special" case) and other problems that fall out from related issues.



  • @tarunik said:

    Modern C++ (i.e. standard library style) is a very underrated language from a technical standpoint -- you can write performant, reliable code in it provided you have a reasonably modern compiler and build harness at hand, and aren't stuck with sucky legacy APIs/libraries.

    That's not the point. The point is development time. Sure you can build performant, reliable code in it-- but you can build the same performant, reliable code in an managed environment in half the time. And it's easier to solve bugs when they occur, to boot.

    The point isn't, "C++ can do the same shit as C#", the point is, "right; but the reverse also applies, and C# code can be written in half the time."

    @tarunik said:

    However, managed code is easier on code monkeys, so that's what the business folks embraced.

    Business folks are interested in economics. Managed languages make better economic sense.

    @tarunik said:

    No, C# is "Java, with some of the bad bits fixed before Java was able to figure out it had sinned".

    "it had sinned"? Are you a crazy-person?

    @tarunik said:

    I will admit that C# has been getting better and better faster than Java has, but it still has significant downsides that folks have forgotten

    Like...?

    @Magus said:

    Sadly, the CLR does not offer an object system on par with that of one of the oldest languages, to the point where you cannot port CLOS to .NET.

    Why would you want to?

    @Maciejasjmj said:

    I was about to rant about it, but I guess I'll just wait for @blakeyrat to call you out.

    What am I supposed to be saying here?



  • @blakeyrat said:

    What am I supposed to be saying here?

    @blakeyrat said:

    Are you a crazy-person?

    Something along those lines.


  • Discourse touched me in a no-no place

    @blakeyrat said:

    "it had sinned"? Are you a crazy-person?

    Aww, Drax, you never fail.



  • @tarunik said:

    OO fetishists

    You seem to have a bit of a axe to grind.

    It fascinates me the way mentioning a programming language will invariably descend into arguments about C++.


  • Winner of the 2016 Presidential Election

    @trithne said:

    You seem to have a bit of a axe to grind.

    It fascinates me the way mentioning a programming language will invariably descend into arguments about C++.

    Either that or how it's better than PHP?



  • @Arantor said:

    it's better than PHP

    If you're going to put the bar that low...


  • Winner of the 2016 Presidential Election

    @trithne said:

    If you're going to put the bar that low...

    Well it depends, are we talking truly-toxic-era PHP (like 4.x) or vaguely-toxic-era (5.0, 5.1) or doesn't-even-smell-too-terribad era (5.3+)?


  • Discourse touched me in a no-no place

    @FrostCat said:

    I could write the same code in GCC if I remembered the correct options, if any are required.

    It wouldn't change the fact that C++ is the language in which code (by someone else) is most likely to fail to compile for entirely mysterious reasons that are beyond the ability of mortal men to comprehend. Which is what I don't like. Smugly proclaiming that I'm doing it wrong and using an insulting version of a HelloWorld as evidence for this just pisses me off on several levels at once; I want to write real code, not pansy-ass CS101 examples.

    (FTR, I'm doing language JIT systems — not of C++ — and that means I need a compiler that will live as a library. So far as I can tell, neither MSVC nor gcc work that way, so my interest in using them is minimal. I can't switch to a system like the JVM or CLR because those would have a very large impact on the downstream users memory management styles, etc. I do need to port things to C sometime if I can, at least outside the compiler library itself, so that I can avoid this weird bound version BS that I'm experiencing, but that's really nothing like as high a priority until I've got a lot more bits and pieces working.)



  • How does a post about Java ends up being about C# and C++? GO AWAY!

    Now, about Observer. Yes, it's been around for a long time and hasn't been modified/deprecated. As said by @Martin_Tilsted Swing, SWT, JavaFX, Spring and Guava have their own way of implementing this pattern.

    OTOH, Java is OO and the whole events, observer & actor patterns doesn't fit (IMO) well with this paradigm. I mean, inner-classes are horrible, and only with Lambdas (functional) has this been made any less of a pain.

    So, you're late to the party. Any Java developer with some experience will know to avoid this (and other) old stuff.



  • For test programs, you can just invoke 'make' on the base name.

    Make is usable as long as you only need the implicit rules. As soon as you have multiple source files you need a makefile and things get horrible quickly.



  • @Eldelshell said:

    Java is OO and the whole events, observer & actor patterns doesn't fit (IMO) well with this paradigm

    This. And about the observer receiving the observable object in the update callback, that's what the guys (Gang of Four) who wrote the book defined as the Observer pattern, mainly because the relation could be n:n



  • Main problems with C++:

    • segfaults if you're not careful
    • requires tremendous amount of boilerplate
    • linking things that came from different compilers is impossible
    • massive compilation times
    • bad error messages
    • most vexing parse rule and similar problems
    • horrendous metaprogramming facilities (preprocessor and templates)
    • duplication of code due to header files

    All the problems above are due to bad design decisions that kinda sorta made sense thirty years ago, and because of having to support legacy code (in pure C). If we got rid of these things, we would get a language that:

    • compiles natively, getting most performance without need for JIT to kick in
    • has no fucking garbage collector
    • doesn't need explicit resource freeing
    • doesn't require petabytes of RAM just for basic runtime library
    • is flexible enough to do anything
    • has seamless C integration (which is the language of choice for POSIX and WinAPI)

    The closest thing we have right now is Rust - but it's still in its infancy, and developers seem to not care about real life usage (after two years, still doesn't work on Windows despite being just a frontend for LLVM).



  • @Martin_Tilsted said:

    The funny thing is that I don't think this class is used for anything. I have newer seen it used for anything in Java, and awt/swing have their own observers so they can pass type-safe information.

    Too bad my professor at college doesn't know that.



  • @Maciejasjmj said:

    If not for legacy stuff, it would be called "C#". C++ is basically C# without hundreds of features

    C# is puny toy compared to C++. The expressive power power of C++ templates is huge compared to C# ones. Hell, C# does not even have const references. (All the people argue that const does not help optimization. Well, that never was what it was intended for. It provides the ability to say "hey, read the data from this element in the collection, but don't dare to modify it" and that's extremely useful ability all the Java people work around by cloning stuff all over the place (or you could create an interface with only the read-only access; that's what const in effect does, just makes it a lot easier to write and does not cause use of virtual dispatch))

    However it is good enough for the usual business stuff and being simpler is actually advantage for the average code monkey, so it is a good match. Unlike Java that is so absurdly cut down to the point it is pain for the more skilful.

    @Gaska said:

    Main problems with C++:- segfaults if you're not careful

    If you use modern C++, the standard library takes care of this for you. I haven't had segfault or memory leak in ages.

    @Gaska said:

    - requires tremendous amount of boilerplate

    Definitely isn't good, but C++11 does make it better (unfortunately I can't use it).

    @Gaska said:

    - linking things that came from different compilers is impossible

    On Unix all compilers on given system are usually compatible just fine. It's a mainly problem of Windows where the ABI is underspecified and where the Microsoft compiler is incompatible with itself using different options.

    @Gaska said:

    - horrendous metaprogramming facilities (preprocessor and templates)

    Compared to none at all in most other programming languages including Java and C#. The only languages that have stronger meta-programming facilities I know of are Lisp (even Scheme does really not since it replaced macros with the "hygienic" stuff), D and Rust and probably Haskell.

    @Gaska said:

    The closest thing we have right now is Rust…

    Agreed, I'm waiting for that to get somewhere too. They simplified it quite a bit lately so it's even starting to look usable. The crapton of pointer types in the early versions would make a really steep learning curve. The previous attempt, D, was not bad either, but it failed to take off, in big part due to some serious split in the community (the incompatible Phobos and Tango standard libraries).



  • @Gaska said:

    Too bad my professor at college doesn't know that.

    Don't get me started with this. Is the same crap with the Oracle FTW thread. What we're taught in school/college has almost no application in the outside world or it's too old (are you using Java8 or 1.4.2 still?). So take it as what it is, a learning experience.

    OTOH, is not in your professor's hands to know about this or even to teach it if it's not appropriate for your current course. If you're to be teach about the observer pattern, the one in Java is enough to get the idea.



  • @Bulb said:

    If you use modern C++,

    If you use modern C++, you don't have to deal with legacy. And as I said, most problems with C++ is with legacy. Oh, and nearly no Unicode support. That's major fuckup (C++11 actually has libraries to deal with it, but no compiler implemented them yet).



  • @Bulb said:

    C# is puny toy compared to C++. The expressive power power of C++ templates is huge compared to C# ones.

    Yep, an amazing feature no one in their right fucking mind should ever use in code. Sure, templates being Turing complete is kinda cool, but if I ever see someone pulling that shit in code intended to be maintainable, I'm going to beat them over their head with a good practices book.

    @Bulb said:

    It provides the ability to say "hey, read the data from this element in the collection, but don't dare to modify it"

    And then it does so anyway with a simple cast, because somebody got pissed off at some compiler error.

    @Bulb said:

    being simpler is actually advantage for the average code monkey

    Oh go do yourself with a cactus, you fucking elitist. Simple is good. Being able to write code simply results in more code being created and more features being implemented. You want power, go write in assembly - but I guarantee you that a 10-minute scratch program in C# is gonna end up being your lifetime project.



  • I only learned about Java's type erasure recently, and my brain exploded. WHY WOULD YOU DO THAT?



  • @Maciejasjmj said:

    Yep, an amazing feature no one in their right fucking mind should ever use in code. Sure, templates being Turing complete is kinda cool, but if I ever see someone pulling that shit in code intended to be maintainable, I'm going to beat them over their head with a good practices book.

    Ditto. Except that some things can't be done without templates. C++ should have a real metaprogramming language embedded that allows explicitly examining abstract syntax tree (things like "if this type has a method of that name") instead of relying on SFINAE.



  • To avoid major changes to the JVM and maintain backward compatibility.



  • The result is a terrible compromise. At the very least, they should have never added lambdas, without fixing that. The result is a "me too" feature that doesn't work.



  • @Gaska said:

    Every time the observable calls observer's callback, it passes its this pointer as parameter - as if the observer didn't know what hit him (if it indeed doesn't, it probably doesn't care anyway).

    Or perhaps it's to allow a single object to act as an observer for multiple observables, since you might want to handle events from multiple objects in a similar manner.

    @Gaska said:

    Oh wait, there's overload. Instead of just the useless this pointer, you can also call the observer with useless this pointer and some other object. Like, anything. Because the method accepts objects of type Object - which is, any object, at all. One does not simply have an observer that cares about only one kind of data. You must downcast from anything-type to type-you-care-about every time, hoping that the observable you registered to isn't a dick and hasn't called you with ParameterModeHelper instead of NodeList. If you want to be safe, you must use RTTI utilities and do instanceof every single time. Because there's no other way to guarantee type safety in statically-typed language.

    So how would you do this? Keep in mind, you're working with Java 1.0.



  • @Spectre said:

    Or perhaps it's to allow a single object to act as an observer for multiple observables, since you might want to handle events from multiple objects in a similar manner.

    OK. Fair point. Except it's 0.01% of use cases and I could handle this by passing this pointer in the anything-you-want parameter which is also provided.

    @Spectre said:

    Keep in mind, you're working with Java 1.0

    I'm working with Java 8. JDK8u20, to be exact. It has Observable class and Observer interface in standard library. And it doesn't make use of generics that were added quite a few versions back.

    @Spectre said:

    So how would you do this?

    If I was one of the original developers of Java 1.0, I would fucking make real generics.



  • @blakeyrat said:

    "it had sinned"? Are you a crazy-person?

    No, and those Java fanatics who cannot admit that Java lost massive amounts of expressiveness and conciseness by going noun-fetishist are the crazy people.

    @trithne said:

    You seem to have a bit of a axe to grind.

    Yes, I do have an axe to grind against those folks who believe OOP is The Only Right and Holy Way To Program. They're holding our entire industry back, in much the same manner as those who do not pay attention to their own industry's history. Is it good or bad that I agree with @blakeyrat on that particular point?

    @Bulb said:

    @Gaska said:
    horrendous metaprogramming facilities (preprocessor and templates)

    Compared to none at all in most other programming languages including Java and C#. The only languages that have stronger meta-programming facilities I know of are Lisp (even Scheme does really not since it replaced macros with the "hygienic" stuff), D and Rust and probably Haskell.


    Clojure, being a Lisp, falls into this list as well, thankfully. But yes: if you are writing a statically compiled language and you do not provide a metaprogramming facility, you are Doing it Wrong™ as API design is really language design. Yet another thing that the OO fetishists don't grasp...

    @Bulb said:

    Agreed, I'm waiting for that to get somewhere too. They simplified it quite a bit lately so it's even starting to look usable. The crapton of pointer types in the early versions would make a really steep learning curve. The previous attempt, D, was not bad either, but it failed to take off, in big part due to some serious split in the community (the incompatible Phobos and Tango standard libraries).

    Tis a shame that D was hobbled by that rift. I ought to look at Rust sometime...

    @Gaska said:

    Ditto. Except that some things can't be done without templates. C++ should have a real metaprogramming language embedded that allows explicitly examining abstract syntax tree (things like "if this type has a method of that name") instead of relying on SFINAE.

    This, because unless you are in a place in the world that does not break out the torches and pitchforks as soon as they see (car x), you have basically no access to metaprogramming whatsoever.

    @Maciejasjmj said:

    Oh go do yourself with a cactus, you fucking elitist. Simple is good. Being able to write code simply results in more code being created and more features being implemented.

    Some of us would write the scratch program in a language that is simple and expressive, like Clojure ;)

    @Remy said:

    I only learned about Java's type erasure recently, and my brain exploded. WHY WOULD YOU DO THAT?

    This is one of the things that makes the JVM completely unfit for purpose as a runtime for a language with a proper static type system. Clojure skirts this issue, thankfully, by way of being a Lisp, and thus dynamically typed; Scala, on the other hand, smacks head-on into type erasure.

    @Remy said:

    The result is a terrible compromise. At the very least, they should have never added lambdas, without fixing that. The result is a "me too" feature that doesn't work.

    :laughing: Java batting 0 on a feature that has precedent among the JVM languages (Scala and Clojure both have working lambdas, TYVM). Well done!

    @Gaska said:

    If I was one of the original developers of Java 1.0, I would fucking make real generics.

    Yeah. Crippling a language in expressiveness simply shifts the complexity from the language into your programs, something that the folks who uphold Java and C# as paragons of simplicity do not understand.



  • @Bulb said:

    The only languages that have stronger meta-programming facilities I know of are Lisp (even Scheme does really not since it replaced macros with the "hygienic" stuff), D and Rust and probably Haskell.

    O'Caml too, through camlp4. (Disclaimer: I have not actually used this so can't compare.)


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.