Goodness in programming languages, part 3 " not doing the same thing more than once
September 05, 2012 [C, C++, Java, JavaScript, Lisp, Metaprogramming, Programming Languages, Python, Scheme, Tech, Template Metaprogramming]Posts in this series: Syntax, Deployment, Metaprogramming, Ownership
I'm going to use a word here - don't stop reading: Metaprogramming. Does the language provide what you need to avoid repeating yourself?
Repeating boilerplate code, algorithms and most importantly ideas, slows you down, makes maintenance difficult, and allows all kinds of mistakes to creep in. If a language provides the ability to abstract, name and re-use all the different types of structure it contains, you can avoid harmful repetition.
Here are some things I like:
- Python, JavaScript and Scheme's ability to treat functions like any other object. A massive step towards sharing code is being allowed to pass around something that can be called without worrying about what it is.
- Scheme's ability to define an algorithm independently of types. In Scheme, there is never a need to write another version of the same function because it deals with different types of thing.
- Python's ability to read and modify classes just like any other object. Want a class just like your current one, except it logs every method call? Write a function that copies and modifies the class definition.
- Scheme's ability write code about code. In Scheme, code is just some nested lists. It's trivial to build and modify code without stepping out of the language.
- C++'s ability to write code that runs at compile time. If you can stand the syntax and (lack of) debugging, C++ template metaprogramming allows you to build C++ code at compile time without stepping out of the compiler environment.
- Scheme and C's macro systems. Both Scheme and C (and C++) allow you to write macros that build commonly-repeated code. Scheme's syntax for this is much easier to work with.
Until you've experienced the freedom of totally generic code in a language like Scheme it's hard to explain why the "Generics" features of some languages are so lacking. Of course, static typed languages work under different constraints. Would it be possible to write a language with very strong generic programming features, but which still allows static typing and compiling to native, non-generic code? I think so.