Archive for the ‘Qt’ Category

Regarding Dynamic Typing

Sunday, July 10th, 2011

Currenty there seems to be a big hype about EcmaScript (JavaScript). Google probably wants to enslave the world with Chrome OS, where the system is not much more than an EcmaScript virtual machine provider (maybe it will support native applications through NaCl, like Android does not only allow Java…), Microsoft wants to reimplement their Windows interface, Qt invented QML, the EcmaScript extension we all know about, providing cool declarative features. Today I want to talk about a fundamental property of EcmaScript and why it just sucks: dynamic typing.

Clarification

First let us clarify the meaning of “dynamic typing”. “Dynamic typing” means that the type of expressions gets checked at runtime, expressions which may have any type are possible. It should not be confused with duck typing, e.g. many types using dynamic typing have a lot of built-in functions relying on specific types (“the frog will not get accepted”), e.g. most functions in PHP’s standard library (expecting string, integer, array or what ever). But for example C++ function templates in etc. provide duck typing (they will accept anything looking like an iterator), or the signatures in old g++ versions provided duck typing. Determining types may happen at compile time (type inference) even with dynamic typing, and of course optimising compilers/interpreters are doing that.

Impact on Development

Let us talk about an argument for dynamic typing: it makes life easier. Actually, that can be right, there are domains where you just do not want to care about such stuff, for example when writing a shell script with few lines of code or when quickly doing some calculations with a computer algebra system. But Typo3, MediaWiki, Windows, Plasma etc. are much more than that. Why do I doubt that dynamic typing makes life easier in those contexts? Because it is error-prone. It is always better when errors get detected at compile time. It is good to fulfill contracts when programming, and they should get verified at compile time, such that they can be easily found and will not annoy the user. A type of contract (not the only one, cf. design by contract) which has been used for long time is the type system. The programmer assures that a variable has a certain type. What happens in dynamically typed languages? You do not have to state the contract, the compiler (or code checker) will usually not be able to check it, it is just in your brain, but of course you will still rely on that contract, the type is something you rely on most of the time when programming, I know that x is an integer when using x for some arithmetics. But you will do mistakes and you get buggy software. That is the fundamental disadvantage when programming, but of course I have to compare it to the advantages of dynamic typing: you can write code quickly and efficiently not mentioning the type everywhere. But there are more proper ways to achieve that: use type inference. The type of a variable will be determined by the compiler when initialising it and you will get an error when you are trying to change the type. That is good because in most cases the type of a variable will not change. And you will get informed about undefined variables (a typo should not cause a runtime error, but in dynamically typed languages it does). For the case that you need a structure allowing different types at the same position there are algebraic data types. With algebraic data types you can state a contract with only few tokens (instead of a nested array/dictionary data structure with a layout which is just implicitly given by the manipulation of it, that does often happen in dynamically typed languages), for variable declaration you only need one token, maybe a single character. That minimalistic overhead in code length is definitely worth it once the software has reached a certain complexity. That threshold is probably not very high, annoying mistakes which could have been avoided with static type checking can already occur in small programs just computing some stuff or something like that.

Performance

Dynamic typing causes big overhead because instructions have to be choosen at runtime based on type information all the time. Of course it is much more complicated to optimise dynamically typed languages, there might be corner cases where the type is not the expected one, but the runtime has to care about it etc. I often read statements like “the performance critical parts are implemented natively” etc., but regarding the amount of applications running using such languages (JavaScript, PHP, Ruby, Python, Lua) we have to state: it is performance critical, PHP is used for more than a preprocessor, QML is used for more than just representing the UI, JavaScript is used for drawing a lot of complex stuff in the browser, Python gets used for scientific computations, and Ruby is establishing new standards regarding overhead (that is not true, Scheme has been that slow before ;), but Ruby allows modifying a lot of stuff at runtime, too). There is reasonable overhead—for abstraction, generalisation, internationalisation etc., but dynamic typing affects nearly any operation when running the program, that is unreasonable and of course it will sum up to significant overhead, although it is simply not needed (and bad for environment ;)).

Special Issues

Regarding extreme flexibility

First of all: in 95% of applications you do not need it, you do not have to modify types at runtime, adding member functions to classes or objects and all that stuff. Sometimes it may be a good way to establish abstraction etc., but in those cases there are usually alternatives: meta-programming can be done at compile time, when manipulating all the types in Ruby they usually could have been manipulated at compile time, too, but Ruby does not support sophisticated compile time meta programming (ML and Template Haskell do, in C++ and D it is kinda limited). Regarding collection of information, debugging etc. using such features: debugging facilities should not influence the performance and cleanness, I am sure by involvement of meta programming you could implement language features allowing that when debugging without neglecting the type system. And of course a lot of flexibility at runtime can be achieved without allowing any type everywhere: dynamic dispatch (including stuff like inheritance, interfaces, signatures and even multi-dispatch), variant types at few places (e.g. QVariant, although I think it is used too often, signals can be implemented in a type safe way, and there are those type safe plugin factories as alternative to QtScript and Kross), signals and slots, aspects etc.

Regarding EcmaScript

You might say that EcmaScript is becoming fast enough because of good compilers and extensions like type safe arrays (e.g. containing only floating points). But EcmaScript will stay EcmaScript, it will keep the downsides of dynamic typing, those type safe arrays are an ugly hack to make it feasible for some specific applications. It is simply lacking a proper type system and it will not get it.

Regarding QML

Using EcmaScript for QtScript was a pragmatic choice, no awesome innovation: there were many web developers knowing about JavaScript. Unfortunately that caused yet another way to integrate scripts and certainly not the most flexible one (cf. my previous blog post), for some reason they did not want to reuse KDE’s innovation (like QtCreator and KDevelop, but that is really a different topic…). QML is based on EcmaScript because QtScript had been based on it before. Dynamic typing is definitely not an inherent property of such declarative UI, most of it could have looked the same with a native implementation based on C++, but also implementations in Ruby or whatever would be easily possible. I have to admit that C++ is not perfect, it does not provide sophisticated meta programming, algebraic types or one-letter type inference (“auto” has four letters ;)), the last one may be a small problem, but overall it is simply not simple enough ;), languages like Scala, D and OCaml have certain problems, too. Hence some of the non-declarative code in QML would have been disproportionately complicated compared to the declarative code. The general approach of declarative UI is certainly good, and now we probably have to accept that it has been implemented using EcmaScript, we can accept it, as long as it is still possible to write Plasmoids using C++ or whatever etc.—obviously that is the case. Thus QML is generally a good development in my opinion, although implementing program logic in it is often not a good idea and although dynamic typing leaves a bitter aftertaste.

I hope you have got my points about dynamic typing. Any opinions?

Unification and Orthogonalisation: Part 2, Plugins and Bindings

Sunday, April 17th, 2011

Hi folks!

Part 2 of the series, there will be hopefully some more rational discussions. I want to review KDE’s plugin system.

There are currently three mainly used Plugn-technologies used by KDE projects:

  • KPluginFactory
  • Kross
  • QtScript

QtScript and Kross work similarly, both use qt-metacalls to allow scripts to access C++-objects. Thus it does only work for QObjects and only for q-properties, signals, slots and functions marked as scriptable. Additionally some data-types get automatically converted (containers, strings) and there is usually a fixed set of supported datatypes, e.g. QPainter etc. Both Kross and QtScript are easy to use, you can load script files and call functions using QVariants/QScriptValues and it reuses the QMetaObject-stuff. However, in my opinion there are a lot of downsides of those approaches:

  • You have to change the C++-code to allow interfacing with it (subclassing QScriptable, marking methods as scriptable etc.)
  • No easy way to expose functionality of non-QObjects
  • No plugins written in C++, which would be natural to have, although it would not be that hard to allow at least C-plugins in Kross
  • KDE application are usually programmed using object-orientation, even scripts are handled as objects, but inside the scripts the object-oriented architecture does not get continued, e.g. kwin-scripts are written in a procedural style, sometimes that is nice, but sometimes it would be nice if the script would follow the overall-architecture with plugin-classes or whatever

Some downsides are specific for QtScript:

  • It does not work with other languages than EcmaScript/JavaScript (no Ruby, Python, Perl…)
  • Sometimes QScriptable has to be subclassed thus making core-classes and plugin-functionality unnecassarily interdependent

Well, there is an alternative: KPluginLoader. Wasn’t that an uncool, unnecessarily complicated, old technology for C++-plugins? No, it is not. The idea is old (similar API had been included in KDE 2.0), but there are good reasons that it is still used although it has been indeed focused on C++: It is really flexible and it is really cool. Let me describe the idea: It is integrated into XDG-configuration-system, each plugin is described by a .desktop-file referencing a shared object file. A factory-object inside this shared object file is used to create instances of a plugin-class, which may subclass a class used to allow plugins inside the application (e.g. a Plasma::Applet, a KoShape or whatever). The factory is the reason for the flexibility of the architecture: There are specialised factories not simply allocating a C++-object but loading script-files, e.g. there are KRubyPluginFactory, KPythonPluginFactory and KPerlPluginFactory creating an object inside the scripting-language subclassing the plugin-class and exposing it to C++, but even more specialised classes libraries like plasma_appletscript_simple_javascript allowing a procedural interface. Those classes make arbitrary applications like Konqueror and KDevelop scriptable.

That is really cool, but there are limitations: Because the meta-object-system is not used there has to be a binding exposing the API of the application to the script. Of course it is nice that such bindings are orthogonal to the application itself and even non-QObjects can be accessed, but unfortunately it is currently complicated to add such bindings to an application. E.g. most bindings are using Smoke to access the libraries and the Smoke-Generator to generate the code for bindings, but you have to invoke it for each language, and the Python binding is using SIP. Hence it is comprehensible that many applications are using Kross or QtScript (though I do not see any good arguments for QtScript).

What could be done for unification? How could the dependencies between the plugin-functionality and the core-stuff be reduced? In my opinion it should be made easier to create a binding since that is currently the difficult task about scripting usin KPluginFactory, creating a plugin-class and invokin KPluginLoader to get plugins is not that complicated. A comprehensive set of cmake-macros would be cool, allowing to create bindings for Ruby, Python, Perl and QtScript using a single xml-file, it should detect pregenerated binding-files, invoke installed smoke-generators, convert it to SIP-information, and install it were the specific scripting-language expects the files. Creating such a XML-file would certainly not be more complicated than adding Q_SCRIPTABLE everywhere.

Maybe some of Plasma’s efforts to make scripting easy could be generalised to be usable everywhere in KDE-based projects (they did a good job ;)). Of course there should be a general purpose QtScriptPluginFactory (there is no such class, isn’t that strange?), but there are also some special features in Plasma, e.g. limitating the accessible API is a nice feature, it should be implemented in the plugin-factories, usually scripting-languages provide features to restrict importing of modules, accessing files etc., there should be a unified set of xdg-properties to specify what a script should be allowed to do. Using X-KDE-PluginKeyword to specify the script to load is also not very elegant, and passing arguments to plugins is also not perfectly implemented. Procedural scripting would be nice to have in many cases, too, e.g. there could be a KProceduralRubyPluginFactory automatically wrapping the functions in the scripting-file into a plugin-object in a generic way.

With such improvements we may get rid of QtScript and Kross, the architecture of some applications could be improved easily, and everybody could choose if he wants to use EcmaScript, QML, C++, Ruby, Python, Perl or whatever.

Disclaimer: There are always some people confusing thoughts and announcements, although the number of disclaimers does not seem to matter for them, there should be at least one: That is a review, some thoughts, no announcement for whatever. And I am familiar with the codebase.

Not everything about the Nokia-Microsoft-Deal is bad

Sunday, March 20th, 2011

At least Miguel de Icaza, founder of Gnome and Mono, thinks in a quite positive way about this deal, thank you, whilo, for the link. Well, what the hell is he talking about? He is a C# fanboy and he thinks abandoning MeeGo in favour of Windows Phone 7 is good for C#. I have always thought that there are some ideals which are more important to care about that the spread of a language, however, let us have a look at some arguments:

Although some open source advocates might see this as a set-back for Linux, Android is already the best-selling Linux OS of all times.

Oh, yes, we are just fatuous idealists, why should we care about the spread of Free Software if there is a free alternative? Wel, of course we should not care about the spread of digital restrictions management, because we can use our devices without DRM, etc. Sorry, that is ignorant, such a proprietary system like WP7, not even allowing GPL-programs, will certainly do harm.

This is fascinating turn of events for C# developers as Nokia will make WP7 more relevant in the marketplace, making C# the lingua-franca of all major mobile operating systems.

Wrong, according to his one diagrams C# had already been a lingua-france before the introduction of WP7. But there had been other linguae-francae, too, like C++ and EcmaScript, but he wants to support monopolists like Microsoft and the monopolism of a single programming language, why should there be choice? For the loss of freedom and choice he uses the euphemism “simplification”, aren’t iPhones quite simple, huh? Well, he did not mention MeeGo, which does not support Mono/.NET/C#, however, there was a Mono-port for Maemo, I am sure it would have been easily possible for MeeGo, too, why does he accept .NET – only WP7 is using .NET and Silverlight – and does not want Mono to be the “runtime-franca” for all devices? And of course he does not care about the millions of Symbian-devices, which support C++, Qt and EcmaScript/JavaScript.

We advise our users to split their user interface code from the engine, or their business logic. Developers should create a native experience for their mobile apps: one per platform.

Of course it is a good thing to split GUI and programm logic. But do you write different user-interfaces for GNU/Linux, Windows and Mac OS X? Is that useful? No, you can simply use Qt, QWidgets, Plasma or whatever and it will be an interface well usable with any keyboard and mouse input devices. But for different smartphone-systems you want to rewrite the whole user-interface? Having 3.5″ or 4″ multi-touch devices running Symbian, MeeGo, Android, WP7, iOS or WebOS, why should they get seperate interfaces written using different APIs etc.? That is nonsense, it would have been nice with C++ and Qt for Symbian, MeeGo, Android and iOS, or even an approach using C# may be better when using the same Mono-runtime with the same GUI-libraries (maybe Qt) for every device.

This is a grand time to be a mobile developer.

Sorry, no, as I explained, Nokia using Windows Phone 7 does not make the situation better, and it is a really bad time for Free Software. Why should there room for somebody like him in Free Software communities? I do not get it, he can start working for Microsoft if he wants to.

Sorry, but everything about the Nokia-Microsoft-deal is bad.

Nokia’s risks

Monday, March 14th, 2011

Hi!

In a recent report to the US Securities and Exchange Commission Nokia is talking about the risks of their new strategy. First of all, they, seem to forget about Qt and Free Software Communities, only few words about Qt:

We have also endeavored to offer a better experience to developers through the unified Qt development environment. By using Qt’s programming interface, both our own and third party developers are able to build an application once and simultaneously make it available for our Symbian and future MeeGo-based products as well as many products supported by other mobile and desktop operating systems without having to rewrite the source code.

Yes, thet was a really nice idea, but they forgot to mentioned that that is over now, at least for them, Android and Necessitas will now become the mobile platfor suitable for simple cross-platform development.

For developers, we believe that we can create new and highly attractive monetization opportunities. By leveraging Microsoft’s proven developer tools and support, based on Silverlight, with our operator billing, merchandising and global application store, we intend to offer new monetization mechanisms for developers while providing access to Nokia’s global scale. We will continue to promote Qt as the sole application development framework for our Symbian smartphone platform on which we expect to sell approximately 150 million more devices in the years to come. For our Series 40-based feature phones, we will continue to support a Java-based development environment.

Is that a joke? It should be “attrictive” for developers, if they are changing their programming languages and APIs all the time, completely? And they forgot that not every developer wants to have “monetization opportunities”, but there are vibrant Free Software/Open Source communities, and for them this great platform – part of this great “strategy” – is certainly not very nice: No GPL, but Microsoft’s own semi-free, copyleft license? One word: immoral! Silverlight? Yes, we want to port all the existing C++-code to an entirely different proprietary platform, of course. Really surprising that they have forgotten us in that short time.

Interesting:

Today, industry participants are creating competing ecosystems of mutually beneficial partnerships to combine the hardware, software, services and application environment to create high-quality differentiated winning smartphones.

Well, and they will simply stop to innovate with software.

Well, they have pointed out some risks we all knew about, too:

  • It may be a good deal for Microsoft, but they may damage Nokia directly for various reasons (the brand, privacy)
  • Microsoft does not guarantees anything
  • Windows Phone may be just bad
  • It takes long time to switch to that platform (two years)
  • Bad support for low-end phones
  • Symbian will not be attractive for anybody in the meantime
  • They will fail to make MeeGo a nice system for mobile-phones
  • They will not be able to innovate, all innovation will have gone
  • Nobody will want to work for Nokia, because it is just uninnovative
  • The developers get discouraged, of course, because a lot of them will be fired (reducing R&D costs), and their work of years will be screwed
  • Reducing costs might fail, because they will still need a lot of support for Symbian and Microsoft will recieve a lot of money
  • Target-platforms are very limited (they are not talking about the desktop, but about missing tablets)
  • Nokia-phones will not have any distinguishing features
  • Integration of their services might fail because of Microsoft’s interests

Few more things they are not talking about:
  • The “next generation user devices”-story is a farce, after that bad, bad Microsoft-deal, years later all the FLOSS-people will come back to use MeeGo, which will be awesome because of Nokia’s research? Seriously…
  • They are now totally noncredible not anly for business-partners, banks and investors, but also for all developers
  • With less research and development (“R&D”) they want to release more innovative products, that is strange
  • Nokia will die. This short and simple statement has been forgotten

It is horrible…