Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I never did generic programming before, being more of a python guy, and generally using OOP. As I moved into generic programming, I faced some issues. I read the FAQ, but I am not satisfied. What I want to ask are the following questions (I am mostly interested in the first, but answering the others will be extremely welcome):

Are generic programming and OOP mutually exclusive? Meaning, for example, are you supposed to have methods and functions accepting the template, instead of baseclass or abstract pure class?


Some other questions I've got, only to provide context to my lack of understanding are: How do traditional design patterns react to generic programming approach and concepts? How to prevent (or control) the genericity of each class/template to "bubble up" in the dependencies dictated by the program logic, in particular when two types are related and go always together (e.g. a RealNumberProducer class and double vs ComplexNumberProducer and std::complex)?

share|improve this question
2  
1) No, 2) Just fine. 3) I don't understand what you're asking there. –  Robert Harvey Mar 21 '13 at 15:05
1  
@Robert: care to detail a bit more? Thanks –  Stefano Borini Mar 21 '13 at 15:07
3  
Type erasure? Isn't that Java's poor, bastard stepchild? –  Robert Harvey Mar 21 '13 at 15:36
1  
@Robert: I have no idea. I'm trying to learn. –  Stefano Borini Mar 21 '13 at 15:37
2  
@StefanoBorini if you "aim at the first one", consider editing your question to make it clear for the readers and to avoid frustrating answerers who will have hard time deciding what you are asking about (btw I also feel that "three questions are too much") –  gnat Mar 21 '13 at 15:52
show 9 more comments

4 Answers

Nope. In fact, many of the best systems use both in combination. Containers are worthless if they are not generic- case in point, Java or C#'s containers when those languages were launched.

Indeed, generic programming is virtually identical to OOP, except that it occurs at compilation/interpretation time rather than execution time, which has a large number of advantages, including increased performance, safety, and flexibility.

are generic programming and OOP mutually exclusive? Meaning, for example, are you supposed to have methods and functions accepting the template, instead of baseclass or abstract pure class?

They are not mutually exclusive, but you should use templates whenever possible to achieve a generic method. Do not ever use inheritance unless you cannot use a template. Inheritance is one of the worst tools that is in the arsenal.

How do traditional design patterns react to generic programming approach and concepts?

They get absolutely blown out of the water. Some patterns are simply completely worthless to begin with (e.g. Singleton), many others are useless in the face of templates or all possible instances of them are implemented directly by one template, such as Listener.

How to prevent (or control) the genericity of each class/template to "bubble up" in the dependencies dictated by the program logic, in particular when two types are related and go always together (e.g. a RealNumberProducer class and double vs ComplexNumberProducer and std::complex)?

Don't. Encourage it. Also, I'm just guessing by the names here, but that really just sounds like std::function<double(args)>, rather than an actual class.

share|improve this answer
    
Don't. Encourage it. but then I end up with a class that is templated over an obscure type that is used by a internal data container used by an instance of another object used by my class. –  Stefano Borini Mar 21 '13 at 15:55
    
@Stefano: Why would you template it if it's only used by one class? –  DeadMG Mar 21 '13 at 15:56
    
because the class might be templated also on other stuff that imply a choice of that obscure parameter so that it is compatible with that stuff –  Stefano Borini Mar 21 '13 at 15:57
2  
@StefanoBorini Code speaks louder than words. –  R. Martinho Fernandes Mar 21 '13 at 16:11
1  
@Stefano: Now I have no idea what problem you're trying to describe. Post a code sample? –  DeadMG Mar 21 '13 at 16:28
show 5 more comments
  1. No, they aren't mutually exclusive. Eiffel and C++ have both had generics and OOP for a long time, and newer languages also support both features.

  2. Design patterns are mostly written in terms of object-oriented programming, and relate to decomposition of a problem into classes. Those classes could be generic or not in terms of the data types they accept; generics and OOP are orthogonal in that sense.

  3. You just choose whether a collection of code (OO or otherwise) is going to be generic or is going to use specific types; there's not really any "control" to be gained or lost. Taking your case, you could maybe fold those two pairs of types into a NumberProducer<T> that returns numbers of type <T>, though the question doesn't go into enough detail to work out whether that's feasible or appropriate.

share|improve this answer
    
I mean, exclusive in usage (for the developer). not in implementation (by the language). I don't see a real reason to have base classes and inheritance if you can use generic programming, or viceversa. I think that it's the same underlying topic I raised about python a long time ago, if I understand generic programming correctly. –  Stefano Borini Mar 21 '13 at 15:52
2  
-1, you're wrong about DPs, as some templates can implement every instance of a DP. In fact, DPs in general are intended to function in a language with basically no useful features whatsoever, like Java, and many of them don't make sense or are available as language features in a language which actually has features, like C++. –  DeadMG Mar 21 '13 at 15:56
1  
Since when has C++ ever had generics? Templates are not generics, despite the similar syntax. –  Thomas Eding Mar 21 '13 at 16:17
1  
The paradigm is still known as "generic programming". –  DeadMG Mar 21 '13 at 16:29
    
When you consider the design patterns in the GoF book, they're all implemented using OOP design with no generic features (even when examples are given in C++). –  user4051 Mar 21 '13 at 16:42
add comment

In theory, OOP and Generic are two different paradigms and the mental state for coding is different from each other. In practical terms, you can combine OOP with template meta-programming. However, I wouldn't suggest to combine them unless you are very comfortable with OOP and generics and if you are in a learning process it might confuse you. Also, there some caveats in C++ when combining inheritance with templates. Take a look Scott Meyer's book (I believe item 48) and he mentions good points on it.

Good luck in your learning journey Stefano

Armando.

share|improve this answer
add comment

No they are not mutually exclusive, but I guess that point has come across by now.

In my mind OOP is a about semantics. It's a way to separate concerns and to help maintainability and use (reuse if you will) of the code.

Furthermore, Generics is about expressiveness. When I can implement an algorithm and express it in such a way that it can, and will, accept many types - then generics is the right tool.

Some languages combines the two by allowing the programmer to specify a contract/interface for the type as a way to guarantee the semantics of the type. Does it have an overload for multiplication? Can it be compared to other instances of the same type? etc etc Most languages (at least that I use) offer both, side by side.

If you're hell bent on using generics without using OOP then I suggest you start looking into some dynamically typed functional language.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.