August 26, 2019

What's wrong with creating a library?


An often told best practice method for creating more efficient software programs is to write a library over a normal application. Instead of typing in a new Python program from scratch the better alternative is to encapsulate the code so that it can be imported easily with an “import library”. On the first look this allows to reuse existing code, but there is a big disadvantage. Because somebody who likes to utilize the existing code has to use the same programming language which is python.
In most cases, this results into a discussion which programming language is the right one. Is Python well suited to write a library, especially if the code should run very fast? Many people would argue pro Python while other are against Python, and both a right. But if Python is a bad idea, what is the more appropriate language in creating libraries? On the first look, C++ or Java is a much better choice, but the problem remains the same, that after creating a library in C++, only other C++ programmer get access to the functionality.
This is not a typical C++ problem but is visible in all major languages. No matter if the code was written in C++, Java, C#, Python or Go, it will become difficult to use the code from a different programming language. And this issue can't be fixed with new wrappers like SWIG or new sort of programming languages. What all programming language have in common is, that they are not working very well with different languages.
A typical technique from the past to overcome the issue is called standardization. The idea is to make a library compatible to the ABI standard on a binary level, so that it can be included from within other languages. The problem is, that even within the C/C++ community this principle doesn't work. For example, in Python it's possible to include C libraries with the ctypes interface, but the approach fails if the aim is to include C++ libraries. Does that mean, that C is superior over C++, because it's the lowest common standard? No because, C doesn't support classes, and classes are needed to create more complex application. The problem is, that in general it's a bad idea to connect two different programming languages. It's very easy to include C code into a c application, and it's easy to include Java code in Java, but it's hard to bridge from Python to C#, from Java to C++ and from C to LISP.
In most cases, the communication between different programming languages works with a wrapper which is higher instance ontop of both applications. It transforms the problem to a higher hierarchy, in which both applications are speaking the same language. There are some techniques available for doing so: wrapper, pipes and RESTful.
RESTful is the most promising technique to connect different applications, because it's not located within an existing programming language like the SWIG wrapper, nor it's working with a certain operating system like UNIX pipes. REST is an internet based message protocol which can be handled by all programming languages. The idea is to convert an application into a webserver which sends and receives data from the environment. The concept is very new and advanced and it was invented at first for internet services. Programming languages like Java and PHP are very RESTful friendly, but the concept is known in Python, C++ and C# as well.