To what extent is the choice of programming language important in terms
of making it easier to build desktop applications? This was the question
asked by Bart Massey in his
talk on day two of the 2012 X.Org Developers' Conference.
Bart observed that we've now reached the point where it's harder to
write a desktop application than it is to write a mobile or web
application. Furthermore, the quality of desktop applications is often
worse than for mobile and web applications. In Bart's opinion, the
situation should be the other way round, and he is puzzling over why it is
not.
Some factors that make programming desktop applications harder are
beyond the X developers' control, he noted. For example, desktop
applications may be much bigger than mobile and web applications. They may
also have requirements that are not present, or are simply not addressed on
mobile and web platforms. For example, desktop applications may need to
manage quite sophisticated user interactions, and provide full
internationalization support (which is omitted from many mobile and web
applications). It's hard for the X developers to control these factors, but
Bart noted one area where they can have some influence in making the
development of desktop applications easier: the choice of programming
language.
When it comes to programming languages, how are mobile and web
applications different? Bart pointed out that—unlike desktop
applications—it is notable that they are almost exclusively
not written in C or C++. Objective C is used in the mobile space,
but Bart noted that that language is special (because it provides some
features not found in C/C++ that are found in the other languages used for
mobile applications). In the mobile and web space, nearly everyone is
using languages that have automated memory management; that eliminates
problems with memory leaks and mishandled pointers. These programming
languages commonly support late binding and dynamic loading. Languages
that provide these sorts of features are attractive because
they allow developers to be much more productive. That's especially
important because the mobile and web spaces are fast moving and programmers
need to be able to put applications together quickly. By contrast,
developing for the desktop—and Bart made it clear that here he was
speaking about desktops in general, not just X—requires a much
greater initial effort before a programmer is productive.
On the X desktop, the common programming languages have been C and C++,
and Bart noted a number of ways in which these languages have been a source
of pain for application developers. There are many reasons to use multiple
threads in GUI programs, most notably because a windowed interface in a GUI
environment is naturally concurrent. However, threading support in C and
C++ (typically via POSIX threads) is not easy to use for the average
application developer. The frameworks used in C/C++ often involve
callbacks and other complicated flows of control, which are likewise
challenging for programmers. Manual memory management is tricky for
programmers as well: monitoring memory usage of desktop applications on any
platform (not just X), it's easy to find cases of applications that leak
memory. And while its true that things can go wrong with automated memory
management, in practice those problems are not as bad.
In Bart's view, the choice of programming language (and the
accompanying frameworks that that choice implies) is one of the things
getting in the way when it comes to developing X applications. The X tool
kits have a widget mentality, and while he feels that that is probably a
good mentality, the toolkits make widget creation a lot of work: too much
boiler-plate code and "weird interaction" is required for building
widgets. "The problem is that the language doesn't give you
convenient ways to express what you want to express."
We use C and C++ on the X desktop because they've always been
there, Bart noted. But, what are the alternatives? Bart estimated that there
are some 400 programming languages in wide use, but noted that there are
obvious attractions to using a mainstream language, in terms of the pool of
developers and available support. Java is an obvious choice. C# is another
possibility, but he thought that there were no advantages that made it
clearly compelling over Java. If one widens the net, other alternatives
might include SML,
OCaml, or Haskell,
but he felt sure that if he asked the room what language should be the
future of desktop development, he'd get fifteen answers.
Bart then considered the toolkits used with X a little more deeply. He
noted that low-level libraries such as XCB are written in C/C++. There
are various reasons for that choice, among them efficiency and the ease
providing foreign-function
interfaces (bindings) for other languages. The choice of C/C++ at this
level of the stack is okay, he thinks. But, as we go up the stack, the next
thing we come to is the toolkits, and the choices there are Qt and
GTK. Both are old, entrenched, and written in C/C++. Because of that, the
applications created above them tend to be written in the same language.
It's time to think hard about whether these are the right tool kits for
applications built in 2012. The problems of building desktop applications
are amplified by using these large, complex libraries, and they don't
provide the automated support that is provided in mobile and web
frameworks.
Bart noted some summary points from his work during the last ten
years. If someone asks him what language to use to write a graphical
desktop application, he is typically going to respond: Java. This is
ironic, because he, an X developer, is recommending a language that will
not produce a native X application. But, he noted that the overhead of
writing a Java GUI application is simply lower (than C/C++), even when
developing for the Linux/X desktop. Another reasonable alternative that he
sees is Python with a toolkit (for example, wxWidgets). As a teacher
(at Portland State University), Bart recommends those alternatives to his
students, rather than the C/C++-based frameworks available for X.
Is programming language even the right thing to be thinking about, when
considering problem of building desktop applications? Bart said that the
audience might be able to convince him otherwise. But, he wanted to know,
if programming language is not a part of the problem, then just how weird
does a programming language need to be before it is a problem? He
then mentioned a few factors that may or may not matter with respect to the
choice of programming languages and toolkits. One of the more interesting
of these was portability of applications across desktop, mobile, and
web. It would be nice to have that, though it is "really hard." But, he
asked, what can we do in terms of choice of programming language to
facilitate that possibility?
On the other hand perhaps we are stuck with GTK and Qt, Bart said. He
noted that it was hard enough to replace Xlib with XCB. The task has taken
ten years, and Xlib is likely still to be with us for another
decade. Perhaps replacing the toolkit frameworks is too difficult, and when
it comes to developing desktop applications, we're consequently stuck with
C/C++ forever. He concluded, however, that "if we're going to save
the desktop—and don't fool yourself, it is in trouble—then I
think programming language is part of the problem."
A lively discussion followed the formal part of Bart's presentation.
In response to some comments from Supreet Pal Singh about the satisfactions
of doing X application development in C/C++, Bart elaborated further on his
point. He noted that that there is no ramp to being an X
developer—it's a cliff. Developers have to master many technologies
before they can do anything. He acknowledged that he too has a different
feeling when developing X applications in C++, "but it's not a happy
one: I feel smart for being able to do this in C++, but not smart for
having done it."
Another audience member noted that Qt Creator can be
used for rapid development of X applications. Bart observed that
code-generation tools solve some problems, but replace them with other
problems as one reaches the limits of what the code generator can do. At
that point, the developer then has the problem of trying to understand what
is going on under the hood of the generator and possibly tweaking its
output, which can complicate code maintenance. He also noted that a GUI
application created with a code generator tends to behave less well for the
user. For example, he suggested, try using a GUI tool to build an
application designed for a small window and then scale that window up and
watch what happens. The result is very different from an application where
the layouts and layout policy have been hand-crafted so that it works well
for all window sizes.
Peter Hutterer pointed out that it may be because of the relative youth
of the web and mobile application spaces that they are not suffering some
of the same problems as desktop X applications. X has been around for well
over 20 years; in 10 years' time, web and mobile applications may have some
of the same issues. He mentioned Android fragmentation as a possible
example of the kinds of problems to come. Bart agreed that the passage of
time may bring legacy problems to mobile and web application development,
but noted that many of his points were independent of legacy problems: the
need to manage pointers and memory, wacky untyped (void *)
interfaces, and complex linking interactions. He constantly sees his
students struggling with these problems.
Matthias Hopf pointed out one language that was missing from Bart's
discussion, but noted that he was hesitant to propose it as a
candidate. That language is of course JavaScript, which started out in the
web space, but is making its way to the desktop in the form of node.js. Bart responded
that he wasn't against JavaScript as a development language. Some aspects
of the language make him suspicious, "but the very fact that people
are successfully developing web and mobile applications in JavaScript and
HTML5 says that it's worth considering [for the desktop]. It would be
interesting to consider what a set of JavaScript bindings for X would
look like."
Other members in the audience noted that the combination of QML, JavaScript, and C++ is an
effective framework for rapid development of X applications, though Keith
Packard interjected that he didn't want to have to write an application in
three languages. Although Bart acknowledged that some of the
information about QML was new to him, by the end of the session, he still
didn't seem to be convinced away from his thesis that the current X
programming languages and the frameworks were part of the barrier to
developing X applications.
Decades of experience have shown C and C++ are powerful languages for
system programming, but few would deny that they provide application
developers with too many opportunities to shoot themselves in the foot.
Bart was facing a rather atypical audience: a room full of C/C++
experts. Nevertheless, there was some sympathy for his thesis, and one
suspects that if a more typical cross section of application developers
were asked, there would be rather more agreement with his position.
The X.Org wiki has a pointer
to the video of this session.
(
Log in to post comments)