Generic Programming – How to Spread Awareness Among Team Members

cgenericsjavatemplates

I am staying in an environment, where people believe:

  1. Java generics are the feature exclusively used for library writing
    and not for the real coding.
  2. C++ is an OO programming language; template is an optional and
    avoidable feature

Though, these people rely highly on the libraries written using generic programming (e.g. STL, Java containers). If I write a code using templates or generics, the code reviewer is most likely to reject it and will comment to write it in a "proper / understandable / elegant" way.

Such mentality is applicable from normal programmer to senior most manager. There is no way out, because 90% of the time, these people have their lobbying.

What is the best way (not being cut throat) of explaining them, the practical approach of writing code which constitutes OO and generic programming both at the same time ?

Best Answer

There are still people in the world who don't use Java generics in "ordinary coding." I can believe it with C++ templates, but generics? They aren't even hard to learn/use. Seriously the best features of Java and C++ are respectively generics and templates.

The best way to convince people of things is to make a compelling argument, be non-threatening, and be right.

So long as you are not doing something like using templates as your programming language, parametric polymorphism (generics/templates) is almost certainly good.

1. Avoids code duplication.

This is obvious, but polymorphic code is general code. That is why it is called generics.

2. Supports better static checking.

Without parametric polymorphism you end up writing things like public Object clone() or public boolean equals(object b) which are not just abominations, they have types that provide no information about what they do, and invariably end up throwing exceptions all over the place. The alternative to parametric polymorphism is casts all over the place

3. Non parametric polymorphism OOP code is basically unable to handle "binary methods" in a correct way.

You use these often.

4. It is best practice

In Java, use of generics is considered best practice (see Effective Java by Josh Bloch). Major C++ thinkers like Sutter and Alexandrescu also encourage the use of templates to solve a variety of problems.

5. It fits the OO paradigm.

People often don't notice this, but the combination of sub-typing and generics produces a system MUCH more powerful expressive, and object oriented than any system with just one of them.

Consider Scala's mixins. These are a nice feature that lets you pull your objects together from component parts. Generics and templates can simulate some of these benefits. For example, say one of your objects uses a database. Good design would have you abstract out the database access into a separate class. If done right this not only lets you mock your data-store (key to testability), it also means that you can add alternative implementations like that new no-sql database. Here though, you might have a problem, mattering which implementation you use you will get different capabilities of your business object.

Generics to the rescue!

   public class Business<S extends Datastore>{
      private S store; ...
   }

Now you can start statically differentiating your Business objects based on ability to use database specific features. You still need some runtime checks and casting, but you can start building MUCH better code.

and

6. Normal code doesn't exist.

There are only three things in the programming universe:

  1. libraries,
  2. configurations, and
  3. bad code.

If you don't think about your code like it is a library you are in serious trouble when the requirements for your project change. Architecture is (arguably) the art of designing good APIs.

I find this attitude stunning. After you get used to programming with parametrized types, not using them just makes everything a pain. And, Java and C++ have a bunch of rough spots which they help remedy.

Related Topic