The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Code Reuse: At the Program Level
by Christopher Diggins
January 7, 2005
Summary
When people think of code-reuse they usually think of function libraries, object hierarchies or cut-and-paste. A very powerful and too frequently overlooked method of code reuse is reuse of programs.

Advertisement

I have always been in awe of the productivity of unix wizards. Given a simple bash shell, they can write in one line a comple program which would take me possibly hundreds of lines of code to write in C++.

The secret to the productivity is code reuse. By piping from one program to another, it is a deceptively simple way to write a new program. Perhaps it is due to this simplicity that it has been overlooked by languages like Java, C++, C#, Delphi, etc.

I know that the Perl, Ruby and Perl programmers reading this right now are probably smirking. They know something that programmers like myself, who grew up on Dos and Windows, have trouble realizing. Why reinvent the wheel, when you can just invoke it from the shell? Of course, Windows and Dos never had a wheel!

Now here is an interesting problem, there is no simple and portable way to write a program in C++, and then reuse it within another program without resorting to the OS. Of course in Windows we can write something like:

  ShellExecute("some_program.exe > some_file.txt"); 

This is fine if we are always going to be in Windows, except that it isn't integrated with the language. It is very hard to run a program and redirect its output into a stringstream without generating a file. What I think is really lacking in C++ is the ability to write directly:

  SomeProgram() > SomeStream(); 

The ability to write such code, would make C++ behave much more like a higher level language (or agile language, if you accept such a thing can exist).

Since C++ is so darn powerful, I have written a library which facilitates reuse of programs by allowing them to be written as objects. I have also providing them with an operator so they can be redirected to and from a stream, or piped to other programs. For instance:

 
  #include "programs.hpp"
  #include "hello_world.hpp"
  #include "upper_case.hpp"
  #include <fstream>
  #include <sstream>

  fstream f("c:\\tmp.txt");
  stringstream s;
  HelloWorldProgram() > f > UpperCaseProgram() > s;

A program like UpperCaseProgram is written as follows:

  // upper_case.hpp

  #include <iostream.hpp>
  #include <cctype.hpp>
  #include "programs.hpp"

  class UpperCaseProgram : public Program {
  protected:
    virtual void Main() {
      char c;
      while (cin.get(c)) cout.put(toupper(c));
    }
  };

So what I am proposing is both a library and a technique to make program reuse in C++ much easier by writing them as objects. The source code, and more a detailed explanation of the technique is available at CodeProject.com

Talk Back!

Have an opinion? Readers have already posted 11 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use