"You must learn from the mistakes of others. You can't possibly live long enough to make them all yourself"
Sam Levenson
Posted 2 years, 4 months ago
  1:  // What's the best ? Functionnal or Object Oriented
  2:  // Of course it depends, but how do you choose what idiom is the best ?
  3:  
  4:  
  5:  // Example 1: Functionnal way
  6:  sealed class Base
  7:  case class Derived1 exends Base
  8:  case class Derived2 exends Base
  9:  
 10:  def blabla( obj : Base) = {
 11:    obj match {
 12:      case d1 : Derived1 => {[ACTION1]}
 13:      case d2 : Derived2 => {[ACTION2]}
 14:    }
 15:  }
 16:  
 17:  
 18:  // Example 2: Object oriented way
 19:  class Base {
 20:    def doSomething()
 21:  }
 22:  class Derived1 extends Base{
 23:    def doSomething() = {[ACTION1]}
 24:  }
 25:  class Derived2 extends Base{
 26:    def doSomething() = {[ACTION2]}
 27:  }
 28:  
 29:  def blabla( obj : Base) = {
 30:    obj.doSomething()
 31:  }
Bookmark and Share
From oopexpert
Posted 10 months, 3 weeks ago
OO is more structured, putting data and corresponding functions together. Furthermore you have one instance of structured data per object. OO introduces object states.
From Anonymous
Posted 1 year, 8 months ago
I don't agree Mike, OOP is a classic design in imperative language, but with a functional language it is not the same
From Mike A
Posted 1 year, 10 months ago
Can't believe so many people still don't get OOP! Although I suppose it's only been around like 4-5 decades. Perhaps one day the language designers might actually take some notice.

So, to flatly contradict Anonymous:

There is a better choice, it doesn't depend on the problem (it's general), and that choice is to use polymorphism.

Replacing switch blocks with polymorphism is, to many experienced programmers *the* classic refactoring. The point of it is that it allows modifying behaviour without rewriting source code by adding new classes.

In the functional case above you'd have to modify the switch source code and add a new case. Then recompile. Then re-test. Then re-debug. Then re-release.

In OOP, as above, you just add a new class, without modifying any existing, compiled, tested, debugged, working and possibly publicly-released code, (sometimes for which the source might not even be available, e.g. in the case of third-party closed-source frameworks).
From Anonymous
Posted 2 years, 4 months ago
Scala is a mix between OO and functional programming, so as you notice, the idiom has to choosen depending on the case. There is no better choice
From Robert
Posted 2 years, 4 months ago
The OO is surely the idiom the most known and masterized by programmers, so following the KISS principle, I would recommand it.
From Steve
Posted 2 years, 4 months ago
I like the functionnal one, the specific code is put where it has to be read. But as always it depends on the problem
Add a comment
Name