top | item 8732374

(no title)

penprog | 11 years ago

java interfaces are one of the main reasons people like java (and as other people have said, it's reimplementation in other more modern languages shows how popular and useful they are). They promote code reuse and allow protected variation. Your professors bad explanations aren't a reason to dislike interfaces.

Also how have you not, in a java class, written code that uses polymorphism? That would be the easiest way to understand how useful interfaces are.

discuss

order

jekrb|11 years ago

But I reuse code all the time with js, following DOT and DRY principles, just fine without an interface. Especially with tools like browserify, where I can basically manage my code as partials independent from each other. Also, I have had to write java code that uses polymorphism. I still find polymorphism easier in javascript. That being said, I think classes and polymorphism is kinda of all just nonsense.

My preferred method of "inheritance" really is just extending an object. Which js is great at. And there's multiple ways to do this, with multiple kinds of prototypes. For something quick an easy I can make a prototype and just pass that through object.create() and now I have a new object that has all the properties of what I would loosely consider to be a "parent". It's more cloning than it is inheritance, and I can completely override properties however I want, while the original properties stay unchanged.

IMO polymorphism and interfaces in Java seem more like hurdles and added complexity compared to object extension and cloning in JavaScript. _.extend paired with browserify also makes for extremely modular code that I haven't seen any Java code compare too.

woah|11 years ago

I agree. An interface in JS is a unit test.

I think that a lot of "classical" programmers simply don't understand how important modules are to JS development.

This is why you get these posts about "how can you manage 1,000,000 LOC in JS!!?", when they don't understand that you never have 1,000,000 in JS, you have a bunch of small, unit tested modules.