views:

335

answers:

8

Many years ago when I didn't know much about object oriented design I heard one guy said something like "How can you write a text editor without polymorphism?" I didn't know much about OOP and so I couldn't judge how wise that though was or ask any specific questions at that time.

Now, after many years of software development (mostly C++), I've used polymorphism many times to solve various problems when designing software. Yet I've never created text editors. So I still can't evaluate that guy's idea.

Is using polymorphism so essential for implementing a text editor in object-oriented languages and why?

+3  A: 

Many of the design patterns like Memento, Flyweight etc that may be used to design/implement Text Editor require inheritance and polymorphism.

Chubsdad
I think that was the example application in the gang-of-four design patterns book.
Steve314
+1; @Steve yes! "Chapter 2 is a step-by-step case study on "the design of a 'What-You-See-Is-What-You-Get' (or 'WYSIWYG') document editor called Lexi."
Stephane Rolland
and those design patterns were created *so that* we can make text editors using OOP. If anything, their existence show that OOP is *not* well suited for OOP (since if it was, we woudn't need a book of design patterns to pull it off) ;)
jalf
@jalf - The GOF authors didn't invent the design patterns, and neither were those patterns a revelation from the gods. The authors basically catalogued them. Most OOP developers had invented at least a few for themselves even before the papers that preceded the book were published. Also, there is plenty of published work on patterns for other paradigms too, though the word "pattern" isn't always used. There are a huge number of published functional design patterns with examples in Lisp, ML or Haskell. Does that prove that functional languages are unsuited to software development?
Steve314
+12  A: 

Polymorphism for writing a text editor is by no means essential. In fact, polymorphism for solving any programming problem is not essential. It's just one way to do it. Sometimes it makes solving certain kinds of problems easier, and sometimes it just gets in the way.

The evidence for this is that there are perfectly usable text editors developed long before "OOP" became popular.

Greg Hewgill
Like the mechanical typewriters :)
Schedler
The fact that emacs was implemented in a Lisp dialect might be considered evidence that you SHOULDN'T use a language that supports polymorphism to create text editors :)
mjfgates
@mjfgates: Since when isn't Lisp polymorphic? Functional languages use polymorphism too. They just don't implement it through inheritance and virtual functions.
jalf
@jalf - Probably a good point, but while I know very little about CLOS, I'd be extremely surprised if it doesn't support inheritance, and I'm certain that it supports multiple dispatch (not the same as virtual functions, but doing a similar job and then some). I suppose the point is that CLOS is a library, so it's ultimately implemented using non-OO lisp mechanisms anyway.
Steve314
@jalf - I thought that was his point. ;)
dash-tom-bang
GNU Emacs is implemented in c and elisp.
dmckee
+9  A: 

I would say "no", because it's entirely possible to write perfectly good text editors in non-object-oriented languages, so it can't be that essential.

Polymorphism is a great technique for the problems it addresses, but it's by no means the golden hammer for everything that ails a software developer.

duffymo
C has polymorphism in the form of function pointers
doron
+1. In 30 years, I've learned that most of the people who say "you can't implement x without y" are the ones who've latched onto something and are dying to apply it to every problem they encounter, appropriate or not.
Blrfl
@deus - I've seen people write text editors in FORTRAN - no pointers, no polymorphism. It's not essential.
duffymo
@Blrfl - Absolutely. It's a form of technology bullying: "You just don't get it..."
duffymo
+5  A: 

This is a term that was thrown around a lot when OO programming was the rage. This guy was probably trying to intimidate you with large words, I doubt if he fully understood what he was saying although it is a simple concept when explained.

Any here lies the crux of the argument - how many times would you have to write, maintain or extend a text editor - none - therefore imho an OO paradigm is of little use in a for what is a relatively simple piece of code that needs to be highly efficient.

James Westgate
+1 for "trying to intimidate you" - sounds like the kind of nonsense that people love to spout to sound smart.
duffymo
Are you suggesting that every text editor was just written once, overnight perhaps, and then never required any new features or maintenance? Clue - I'm using v5.7 of Notepad++. As has already been pointed out here, a lot depends on how sophisticated you want your text editor to be.
Steve314
@Steve314: Sophistication can be achieved with other paradigms than OO.
jalf
@jalf - I never said otherwise. I countered your apparent claim that text editors are trivial pieces of code that get written once then never maintained or extended. For more sophisticated applications, OO provides a useful abstraction toolkit - but I never claimed OO is the one true way.
Steve314
@jalf - oops - I mean James' apparent claim, of course.
Steve314
+1  A: 

Is using polymorphism so essential for implementing a text editor in object-oriented languages and why?

Depends on what kind of text editor you're talking about.

You can write notepad without OOP. But you most likely will need OOP for something like MS Word or OpenOffice.

Design Patterns: Elements of Reusable Object-Oriented Software uses text editor for examples (i.e. "case study") of Design Pattern application. You may want to check out the book.

SigTerm
so what exactly would be the problem if I tried to write something like MS Word in a non-OOP language?
jalf
+1 mainly because I don't see why this got downvoted and there was no comment to explain.
Steve314
@jalf - I'm guessing that "need" isn't intended to be taken that literally.
Steve314
+1, in the book Design Pattern: Chapter 2 is a step-by-step case study on "the design of a 'What-You-See-Is-What-You-Get' (or 'WYSIWYG') document editor called Lexi."
Stephane Rolland
@Steve314: so once again, what would be the problem in writing a text editor without using OOP? And if you say "Nothing", tell me what point SigTerm is trying to make.@Stephane: So because someone once showed that you *can* write a text editor using OOP, we should upvote everyone who says it *must* be done with OOP?
jalf
@jalf: *"so what exactly would be the problem if I tried to write something like MS Word in a non-OOP language?"* It may take longer, and you'll probably end up emulating OOP, or functionality will be non-extensible. *"it must be done with OOP"* You're misquoting me. I said that you **"most likely will need OOP"**, but I didn't say "you **must** use OOP". This is not the same thing, you know. *"we should upvote everyone"* If you don't like my answer, provide your own. You're free to write a small thesis here, if you want. *you*(not "we") "should" do whatever you want. I don't care anyway...
SigTerm
@SigTerm: you are assuming that it is OOP or nothing. I certainly agree that you'll be in trouble if you try to write everything in assembler or some ind of unstructured programming language But why will you "most likely need" OOP specifically? Couldn't you get it done just as easily with, say, functional programming? I'm only objecting to the idea that OOP is the *only* paradigm that lets you handle complex programming projects. You don't need OOP any more than you *need* steel to build a house. It's just one of several building materials that might do the trick.
jalf
(And the upvote thing was a directed at @Stephane who upvoted *because someone once showed that a text editor *can* be written using OOP)
jalf
@jalf: *"say, functional programming?"* The question is tagged "C++", not "language agnostic". While it is (AFAIK) possible to use functional programming in C++, it is not common. *"you are assuming that it is OOP or nothing."* I'm not assuming that. *"OOP is the only paradigm that lets you handle complex programming"* You're misquoting me again. I did not said that. Look, I probably can write answers in lawyer-talk-like way that would prevent every possible misunderstanding/misinterpretation, etc, but that will be boring, unreadable, will be 50 times longer, and will take way too much time.
SigTerm
@jalf... no, "not because someone once shows...", you're really funny. Only because the GoF - Design Patterns book is the most important book of my readings in many years so far, and I consider it Essential for C++ programming, for good programming I mean.
Stephane Rolland
@jalf Thanks to OOP, C++ can implements the functional programing paradigm, through Function Object called Functors and soon C++0x lambdas. Your holy war is useless and nonsensical.
Stephane Rolland
@jalf - returning to your reply to my previous comment - the problem is with writing *any* complex application without *any* appropriate tools for large-scale development. The better the tools you have for managing complexity, the less you make the task unnecessarily difficult. OO is just one such toolkit, of course, but that doesn't mean that it's irrelevant here. It's certainly not taboo. As far as I am aware, SigTerm was under no commitment to list all possible ways to manage complexity, and given the context of the question OO was particularly relevant.
Steve314
@jalf - BTW, if yours was the downvote, sorry about the "no comment" comment. Apparently I don't hit the refresh button often enough.
Steve314
+2  A: 

I once wrote a text editor in Basic. It wasn't a sophisticated text editor by any means, it's big highlight being a text-mode windowing thing used for some menus and dialogs, but it still did it's job at the time - ie it proved I could write a text editor in Basic. I even used it sometimes. I won't be showing the source in public - it's just too embarrassing!

When your text editor is mostly just inserting/deleting characters in a big array of strings and displaying them, little or no abstraction is needed other than the usual provided-as-standard abstractions of arrays and strings.

On the other hand, the amount of text that a text editor on a PC is expected to cope with has increased a lot over the last 20 years, sometimes to the extent that even a modern PC with multiple Gigabytes may not be able to keep the whole file in RAM. On top of that there are character set and encoding issues. A good text editor is expected to remember a (potentially large) number of bookmarks into multiple files, and to maintain them so they refer to the same point despite edits. And then there's syntax highlighting, the ability to record/playback macros, and more.

In short, modern text editors are much more complex than the things used in DOS and on other micros twenty years ago. That complexity is no doubt much easier to manage with a good toolkit for handling abstractions.

Steve314
+2  A: 

The other points about polymorphism as being just a tool are spot on.

However if "the guy" did have some experience with writing text editors he may well have been talking about using polymorphism in the implementation of a document composition hierarchy.

Basically this is just a tree of objects that represent the structure of your document including details such as formatting (bold, italic etc) coloring and so on.

(Most web browsers implement something similar in the form of the browser Document Object Model (DOM), although there is certainly no requirement that they use polymorphism.)

Each of these objects inherits from a common base class (often abstract) that defines a method such as Compose().

Then when it is time to display or to update the structure of the document, the code simply traverses the tree calling the concrete Compose() on each object. Each object is then repsonsible for composing and rendering itself at the appropriate location in the document.

This is a classic use of polymorphism because it allows new document "components" to be added (or changed) without any (or minimal) change to the main application code.

Once again though, there are many ways to build a text manipulation program, polymorphism is definitely not required to build one.

Ash
(Not so) fond memories of [Interleaf](http://en.wikipedia.org/wiki/Interleaf)
dash-tom-bang
+1  A: 

While a simple text editor (below edit.com from MS-DOS) may be realized easier in a static class only (because the functionality is very limited), as soon as you get to menus and dialogs, you will find yourself in dire need of object oriented language features.

Personally, I frown upon procedural code anyway - I prefer a mixture of OOP (program structure, separation of functionality, etc...) and functional programming (implementation).

This may sound like a religious argument of some sort, but I find my personal style quite recommendable. Usually I need far less lines of code (which are much easier to understand) than most of the developers I work with and my code feels much more "agile" and "flexible".

Try it. :-)

Oh - and polymorphy is not hard to understand. Simply imagine that you (as a person) can be handled as:

a) Man or woman b) European, asian, american, african, oceanian (I hope this is right), etc... c) By your name d) By your occupation

But still you are a person - and a living being, and a part of the universe... and YOU.

So for someone who asks you for statistical reasons a few questions, you may be handled as a, say, woman from oceania (I don't know where you come from, but lets just assume) who is, hm, 42 years old and lived in, hm, Switzerland for 23 years (hahaha).

For your employer, you may be competent in programming and talking to your collegues.

However, HOW you fill those roles is dependent on your implementation. This is you.

StormianRootSolver
In the common form of OOP where methods are members of their class, methods are basically just functions, but with one parameter (this/self/whatever) treated as special. It works well for single dispatch polymorphism, but for multiple dispatch there's no particular reason to nominate one parameter as being a special one. One very logical approach to multiple dispatch is that you still have classes and inheritance, but functions are non-members, so code looks very much procedural. It's OOP, but not as we know it. The visitor pattern is harder work than it needs to be without multiple dispatch.
Steve314
The only thing - I've not really seen this in a general purpose language. I've used it in an AST-handling DSL (treecc) and I've written my own DSL with a more sophisticated inheritence model (but not released as yet), but that's about it. I believe that CLOS may follow a similar model, but don't know for sure.
Steve314
Frowning on procedural code (and forcing everything to be an object) is one of the biggest barriers to writing sensible code that I encounter every day. What's wrong with procedural code when you've got a sequence of steps to follow? (It seems to me that a lot of people write procedural code inside of classes, anyway, and think that their sequence of steps is somehow now magically OO.)
dash-tom-bang