<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RasterGrid Blog &#187; Delphi</title>
	<atom:link href="http://rastergrid.com/blog/tag/delphi/feed/" rel="self" type="application/rss+xml" />
	<link>http://rastergrid.com/blog</link>
	<description>A technical blog from Daniel Rákos (aka aqnuep)</description>
	<lastBuildDate>Tue, 24 Aug 2010 19:34:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>One more degree of freedom for C++</title>
		<link>http://rastergrid.com/blog/2010/02/one-more-degree-of-freedom-for-c/</link>
		<comments>http://rastergrid.com/blog/2010/02/one-more-degree-of-freedom-for-c/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 14:38:42 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[event handling]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[signal]]></category>
		<category><![CDATA[signals and slots]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=176</guid>
		<description><![CDATA[

Those who worked enough with C or other procedure oriented languages know how much flexibility callbacks provide. The simplest example is the qsort function of the C standard library. It is also not unintentional that many libraries, windowing system APIs and operating system APIs also highly rely on callbacks to pass a particular task over [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F02%252Fone-more-degree-of-freedom-for-c%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fdrfdzt%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22One%20more%20degree%20of%20freedom%20for%20C%2B%2B%22%20%7D);"></div>
<p>Those who worked enough with C or other procedure oriented languages know how much flexibility callbacks provide. The simplest example is the qsort function of the C standard library. It is also not unintentional that many libraries, windowing system APIs and operating system APIs also highly rely on callbacks to pass a particular task over to another program module and it is one of the fundamental tools needed to implement an event-driven application. At the same time, object oriented languages does not directly support the concept of callbacks as they don&#8217;t really fit into the paradigms used by these languages. Fortunately, even if not as a language feature, all object oriented languages support a similar facility like callbacks in the form of delegates.</p>
<p><span id="more-176"></span>Delegation as a design pattern is used to describe the situation when one object passes on the implementation of a particular task to another object. This clearly reflects the purpose of callbacks used in procedure oriented languages. Many languages does natively support some form of delegation, some of the well known ones are C# and Delphi.</p>
<h2>Callbacks</h2>
<p>As mentioned before, the facility present in procedure oriented languages that enables the delegation of functionalities to other modules is done with callbacks. These callbacks are specified by passing function pointers to some registration functions provided by the library. Here is a very simple C example:</p>
<pre class="brush: c">/* server header */
void registerFooCallback(int (*fooCB)(int, float));
int doFoo(int a, float b);

/* client code */
int myFooCallback(int a, float b) {
    /* ... do something ... */
}

int main() {
    registerFooCallback(myFooCallback);
    cout &lt;&lt; doFoo(5, 3.2f);
    return 0;
}</pre>
<p>Here we can see how easily callbacks provide injection of user code for handling events happened in the server.</p>
<h2>Delegation as a design pattern</h2>
<p>The simplest way to create object oriented callbacks is by applying the design pattern of delegation. If we would like to construct the C++ equivalent of the example above using the mentioned pattern, we end up with something like the following:</p>
<pre class="brush: cpp">/* server header */
class IFooCallback {
public:
    virtual int operator() (int a, float b) = 0;
};

class Foo {
private:
    IFooCallback* _fooCB;
public:
    void registerCallback(IFooCallback* fooCB);
    int doFoo(int a, float b);
};

/* client code */
class MyFooCallback: public IFooCallback {
    int operator() (int a, float b) {
        /* ... do something ... */
    }
};

int main() {
    Foo foo;
    MyFooCallback fooCB;
    foo.registerCallback(fooCB);
    cout &lt;&lt; foo.doFoo(5, 3.2f);
    return 0;
}</pre>
<p>As you can see, it is quite straightforward to provide an object oriented alternative to callbacks. However, there is a very significant drawback when using the technique above, namely the type intrusion inherently coming from this definition of a callback. The client code needs to explicitly inherit it&#8217;s own code from a type defined in the server. This results in tight coupling and is likely to carry other disadvantages inside regarding to maintainability and migration issues.</p>
<h2>Delegate methods</h2>
<p>In our previous attempt to provide an easy to use C++ alternative for callbacks with OOP in mind we tried to replace function pointers with a pure virtual base class that acts like an interface definition for our callback. However, it somewhat violates the original goals of delegates which by definition should be some form of run-time inheritance (this varies from definition to definition, still, this is the one that I&#8217;m referring to in this article). We soon figure out that the most convenient way would be to be able to assign member functions of any class as a callback. Obviously, the parameters and return type should still match as previously to provide type safety, but we would like to remove any additional dependencies between the client and the server.</p>
<p>While C++ does have the term of pointers to member functions there is no easy and standard way to implement callbacks using them. Or is there? First of all, there is no particular problem with class static member functions as they are much like C functions, however, limiting delegates to static methods heavily affects the freedom of the developer. The problem with object member functions and especially with virtual member functions is that they have the implicit parameter <strong>this</strong> that enables them to access the object they correspond to.</p>
<p>The popular Boost library provides mechanisms that enables the use of object member functions as separate entities by using the <strong>bind</strong> functor adaptor which became part of the language standard as part of <a title="ISO/IEC TR 19768:2007" href="http://www.iso.org/iso/catalogue_detail.htm?csnumber=43289" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.iso.org/iso/catalogue_detail.htm?csnumber=43289&amp;referer=');">Technical Report 1</a>. This extension makes it possible to use member functions as delegates in a way that does not involve any type intrusion side effects.</p>
<p>Unfortunately, these facilities involve a noticeable performance hit when the callback is invoked compared to simple method invocations. Also, using functor adaptors for implementing delegates is not the most straightforward and makes the code quite ugly compared to an ideal situation when delegates are part of the language itself. Of course, this is only my opinion, others who used these libraries more often may have a different vision about the topic.</p>
<p>Anyway, as for me performance is always a concern, I started to look around for alternatives. It surprised me that I&#8217;ve found even two of them very soon:</p>
<ul>
<li><a title="Fastest Possible C++ Delegates" href="http://www.codeproject.com/KB/cpp/FastDelegate.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeproject.com/KB/cpp/FastDelegate.aspx?referer=');">Fastest Possible C++ Delegates</a> by Don Clugston &#8211; This is a library that provides delegates that are as fast as simple virtual method invocations. The implementation strongly relies on the behavior of different compilers, yet is very portable, at least as far as I can tell.</li>
<li><a title="The Impossibly Fast C++ Delegates" href="http://www.codeproject.com/KB/cpp/ImpossiblyFastCppDelegate.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeproject.com/KB/cpp/ImpossiblyFastCppDelegate.aspx?referer=');">The Impossibly Fast C++ Delegates</a> by Sergey Ryazanov &#8211; This library was introduced as an alternative to the previous one that strictly relies only on standard features of the languages. Surprisingly, this later is less supported by different compiler implementations and it is also somewhat slower than the previous one.</li>
</ul>
<p>Personally, I go with the first one as for me performance and portability is more important than conformance with the standard. And, of course, it is not that hard to change the back-end for the delegate support at some time if I change my mind. Finally, lets see how our foo callback looks like when using the fast delegates of Don Clugston:</p>
<pre class="brush: cpp">/* server header */
class Foo {
private:
    FastDelegate2&lt;int, float, int&gt; _fooCB;
public:
    void registerCallback(FastDelegate2&lt;int, float, int&gt; fooCB);
    int doFoo(int a, float b);
};

/* client code */
class MyClass {
    virtual int handleFoo(int a, float b) {
        /* ... do something ... */
    }
};

int main() {
    Foo foo;
    MyClass myObj;
    foo.registerCallback( MakeDelegate(&amp;myObj, &amp;MyClass::handleFoo) );
    cout &lt;&lt; foo.doFoo(5, 3.2f);
    return 0;
}</pre>
<h2>Multicast delegates</h2>
<p>The delegates presented previously can only be bound to a single method, as usually delegates behave this way, although a single method can be bound by many delegates. The signals and slots model extends this to a many-to-many relationship. Thus a signal is actually just a delegate that can bind to multiple methods at once. Such a primitive is sometimes also referred to as a multicast delegate.</p>
<p>Multicast delegates come handy especially in case of user interface programming and other situations where the event based programming model is used. The basic foundation behind this programming model is the idea of &#8220;subscribe and notify&#8221;. That means there are <em>publishers</em> who will do some logic and sometimes publish <em>events</em>. When such an <em>event</em> is published, it is actually sent out to the <em>subscribers</em> who have subscribed to receive the specific event. At implementation level this is nothing more than having a multicast delegate in the <em>publisher</em> object and providing an interface that will be used by the <em>subscriber</em> objects to register one of their methods that has to be called in case a particular <em>event</em> occurs.</p>
<p>There are plenty of signals and slots libraries out there including but not limited to the Boost Signals library. However, again, if performance is a concern one must look around carefully to find the appropriate library suitable for a particular purpose. One such library that extends the fast delegates of Clugston with a signals and slots framework is that of <a title="Simpler UI Code With Signals and Slots" href="http://www.gallantgames.com/2009/12/13/simpler-ui-code-with-signals-and-slots" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.gallantgames.com/2009/12/13/simpler-ui-code-with-signals-and-slots?referer=');">Patrick Hogan</a>&#8217;s.</p>
<h2>Asynchronous delegates</h2>
<p>If we do one more step forward, we arrive to asynchronous delegates that can provide us a flexible yet efficient messaging system for multi-threaded applications. The only additional thing we have to implement a message queue on the callee side and optionally some form of synchronization if we would like to also make it possible for the asynchronous delegates to return data to the caller.</p>
<p>As this topic deserves a thorough discussion on its own, I would recap on the subject in a future article and try to provide a sample implementation using OpenMP as usual.</p>
<h2>Conclusion</h2>
<p>We&#8217;ve just touched the surface of what possible use case scenarios of delegates one can met during software development, still, we&#8217;ve seen how many advantages such a programming primitive can give to C++ developers no matter if they are implementing a very simple library of sorting algorithms like the qsort C standard library function or a robust, fully event-driven multi-threaded application. We&#8217;ve also seen that there exist several efficient implementations of such a framework for those performance fanatics like me.</p>
<p>It is a perfect example how easily one can extend C++ with another facility that is usually available only in the most modern managed languages. By the way, I would be interested in your opinion what do you like the most in other languages like Java and C#, and you are disappointed that C++ does not directly provide the same thing. Maybe there exists a C++ alternative for those facilities as well, just we have to look around to find them&#8230;</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/02/one-more-degree-of-freedom-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Never seen flexibility for Delphi</title>
		<link>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/</link>
		<comments>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 18:53:30 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[byte code]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Pascal]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=95</guid>
		<description><![CDATA[

As far as I can tell people still prefer using python for scripting in their Delphi projects. This is probably because python scripting in Delphi has a long history and it is a proven method with easy integration. What if I would tell you that there is a free solution which enables you to write [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F01%252Fnever-seen-flexibility-for-delphi%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F5saVqb%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Never%20seen%20flexibility%20for%20Delphi%22%20%7D);"></div>
<p>As far as I can tell people still prefer using python for scripting in their Delphi projects. This is probably because python scripting in Delphi has a long history and it is a proven method with easy integration. What if I would tell you that there is a free solution which enables you to write also your scripts in pascal? A few years ago I discovered an alternative that makes this possible. This highly influenced my vision about the importance of scripting. Since that I accompanied all my hobby projects with full scripting support.</p>
<p><span id="more-95"></span></p>
<p><img title="More..." src="http://rastergrid.com/blog/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" />The first question of the reader might be that why I would need a script language in my program? It is, however, very easy to answer this question. The presence of some sort of scripting support in a software product has many benefits both from developer and customer point of view:</p>
<ul>
<li>Less wasted time on software building by customizing application without the need for recompilation.</li>
<li>Unusually flexible reflection capabilities by creating script code on-the-fly with string manipulation.</li>
<li>Very flexible debugging possibilities by modifying executed code during the debug session.</li>
<li>Easy separation of often changing high level logic and rarely changing internal framework.</li>
<li>Powerful tool for the user to customize application for own needs.</li>
</ul>
<p>These are just a few examples what additions a scripting engine can give. Personally my favorite one is the second mentioned example in the list above. As a result that I&#8217;ve worked as a PHP developer for over a year I really got used to the ability to compose code on-the-fly by just putting some strings together. It is a built-in feature of PHP and most other interpreted languages, however it is fairly uncommon that such functionality exists in native programming languages. I knew that to emulate the presence of this facility I have to use some scripting language but I never thought it will be so easy to find one which suits my needs.</p>
<p>First, I was looking around to find out what other Delphi developers use in their projects. That&#8217;s how I first met with python. The language itself has a very powerful OOP tool-set but the syntax was too extraneous for me. After checking tons of other scripting languages out there which had mostly no easy integration possibilities with Delphi, finally I found RemObjects Pascal Script.</p>
<h2>Pascal Script for Delphi</h2>
<p>First, I just said &#8220;Wow! This is exactly what I was looking for&#8221;. It not just enables you to write your scripts using the language what you also use to develop the application itself but it also provides incredibly easy integration with Delphi. Just to mention a few features of Pascal Script:</p>
<ul>
<li>Calling Pascal Script code from Delphi code or vice versa.</li>
<li>Using external DLLs from within the script without requiring any type of function header.</li>
<li>Sharing any kind of data between the script and the Delphi code in both direction.</li>
</ul>
<p>For a full list of features please refer to the <a title="RemObjects Pascal Script" href="http://www.remobjects.com/ps.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.remobjects.com/ps.aspx?referer=');">official site</a> of RemObjects Pascal Script.</p>
<p>One thing that most Delphi developers got used to is that most of the third-party components and tools available for Delphi are with a commercial licensing. Well, this is not the case with Pascal Script. It is not just freely available but also comes with full source code. This further increases the potentials of it as you can add new features to it if you wish. However, it is already powerful enough for most use cases.</p>
<h3>How it works?</h3>
<p>In a nutshell, using Pascal Script is piece of cake. You just have to put together your source code string, compile it and then execute. Yes, Pascal Script needs a compilation phase which produces a byte code as an intermediate format. By telling this, you&#8217;ve probably already figured out that Pascal Script is not just very flexible but also provides adequate performance by not having to interpret directly the source code. Let&#8217;s start with a very simple script:</p>
<pre class="brush: delphi">{ some very simple pascal script }
begin
  PrintString('Hello World!');
end.</pre>
<p>This will call the procedure PrintString with the string parameter &#8220;Hello World!&#8221; . But where this procedure comes from? Well, actually you have to bind it using the run-time library that comes with Pascal Script. To execute this script having an actual Delphi procedure bound to the procedure name used in the script you&#8217;ll need some code similar to the following:</p>
<pre class="brush: delphi">program HelloWorldScript; { host application that executes the script }

uses
  uPSCompiler, uPSRuntime;

procedure PrintString(message: String);
begin
  writeln('Message from script: ', message);
end;

function LoadScript: String;
begin
  { load the script from some file and return it }
end;

var
  Compiler: TPSPascalCompiler;
  Exec: TPSExec;
  ByteCode: tbtString;
begin
  Compiler := TPSPascalCompiler.Create;  // create an instance of the compiler
  // register user procedure to compiler
  Compiler.AddDelphiFunction('procedure PrintString(message: String)');
  if Compiler.Compile(LoadScript) then  // compile the script to byte code
  begin
    Compiler.GetOutput(ByteCode);
    Exec := TPSExec.Create;  // create an instance of the executer
    // register user procedure to executer
    Exec.RegisterDelphiFunction(@PrintString, 'PRINTSTRING', cdRegister);
    if Exec.LoadData(ByteCode) then  // load byte code to executer
      Exec.RunScript;  // run the script
  end;
  Compiler.Free;
  Exec.Free;
end.</pre>
<p>If a newbie looks at this code, probably thinks that it&#8217;s overcomplicated. Well, actually this is true. Pascal Script needs some basic setup stuff but if you take the time to learn it, you will figure out that it does not involve too much code to use it. One thing that may still need some clarification is the registration of the user procedure provided to the script&#8230;</p>
<h3>Connecting the script to the Delphi code</h3>
<p>Pascal Script needs that all the procedures, functions, variables, types and classes that we would like to provide to the executing script needs to be registered both to the compiler and the executer. The compiler needs this information for syntax validation and to produce the byte code, that&#8217;s why it&#8217;s enough to provide the signature of the particular language element to it. The registration to the executer is which actually binds the concrete object to the script.</p>
<p>All these manual registrations looks like a pain in the a** at first sight, however, they are unavoidable. Fortunately, Pascal Script provides a very handy tool for us to automate this process, namely the UnitImporter. This tool can create an import unit based on an actual production code unit that will provide one parameterless procedure that registers all the classes, methods, etc. to the script compiler and run-time.</p>
<p>Beside this, Pascal Script provides ready baked units for the registration of the standard units of Delphi and a component set that integrates into the IDE, although I haven&#8217;t used them so far. Anyway, even if the framework seems to be a bit too complex and needs too much auxiliary code, in practice it is very easy to use it and most of the support code for scripting can be generated with the UnitImporter. If you don&#8217;t believe me, try using it for a while and you will see.</p>
<h3>Portability and usage limitations</h3>
<p>Maybe Pascal Script is not the Holy Grail of Delphi programmers but it is a very powerful scripting engine. The first question in those who are not developing on/for Windows platform can be that &#8220;Is it portable?&#8221;. This is another advantage of Pascal Script as it is as portable as Delphi can be. This means that it will work fine also with the Free Pascal compiler and it comes with the same component set for the Lazarus IDE as well.</p>
<p>So if not the portability and the ease of use then has it any disadvantages by the way? Unfortunately, yes. We&#8217;ve already seen that it has both-way interworking capability with Delphi, it has tools for automating the integration process and it can be used also for cross-platform projects, but it has small problem: it supports only a subset of the Delphi language so you cannot define classes in the scripts. You can still use classes and objects inside the script, but only those which were defined in the host Delphi application.</p>
<p>Anyway, this does not necessarily mean that Pascal Script is not flexible enough. I used it in my project mainly for the following purposes:</p>
<ul>
<li>Using the services provided to the script (classes, objects, etc.) to manipulate application behavior at run-time.</li>
<li>Handling Delphi events with procedures/functions implemented in scripts.</li>
<li>Baking a console that makes debugging tasks much easier by correcting errors while the program is running.</li>
</ul>
<p>Personally I don&#8217;t understand how Embarcadero Technologies (the current supplier of Delphi) haven&#8217;t integrated it into the core Delphi distribution as they did with many other popular frameworks like DUnit.</p>
<h2>Conclusion</h2>
<p>Pascal Script is probably not a script language that can have a future on it&#8217;s own but it is still a great addition to the feature set of Delphi as it provides a way to push the flexibility of the language above it&#8217;s limits with minimal effort. It is fast, robust and stable, and it offers most of the facilities that usual developers expect from a scripting language. Maybe I&#8217;m quite biased as I really enjoyed working with Pascal Script, but I think you Delphi programmers out there should definitely take a look at it and give it a try.</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/01/never-seen-flexibility-for-delphi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage code yourself</title>
		<link>http://rastergrid.com/blog/2010/01/manage-code-yourself/</link>
		<comments>http://rastergrid.com/blog/2010/01/manage-code-yourself/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 16:41:25 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[byte code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=10</guid>
		<description><![CDATA[

Those who know me know it well that I am not a big fan of languages which produce managed code. In this article I would like to cover the reasons behind my skepticism. Also I would like to dispel the myths around such languages and try to prove them with facts (we will see how [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F01%252Fmanage-code-yourself%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F4FH6No%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Manage%20code%20yourself%22%20%7D);"></div>
<p>Those who know me know it well that I am not a big fan of languages which produce managed code. In this article I would like to cover the reasons behind my skepticism. Also I would like to dispel the myths around such languages and try to prove them with facts (we will see how well I manage to achieve this). If you disagree with me, you&#8217;ll most probably hate me because of making this post but please, respect my personal point of view.</p>
<p><span id="more-10"></span>As I&#8217;ll mostly talk in this post in general about managed code I would like to emphasize that I have worthwhile experience only with Java and have barely used C# and other managed languages so my primary arguments maybe apply directly only to Java but still would like to present my arguments in general as they still apply in some way to all such languages.</p>
<p>Without the intension to insult anybody, I would like to start with a citation from one of my friends, who often says that &#8220;Java is just a pseudo-language&#8221;. Mostly I agree with him and it&#8217;s not just because I have my not so delightful experiences with the language but also because it introduced a brand new way of coding style that is a complete nightmare for an old-school coder like me. As I don&#8217;t want to start another religious war, I&#8217;ll stop my preposterous arguments here as I would like to prove my concerns with facts that nobody can disclaim.</p>
<blockquote><p>&#8220;&#8230; nowadays Java is almost as fast as native code&#8230;&#8221;</p></blockquote>
<p>This used to be a common myth in the last few years. For those who are still arguing by shouting this loud on forums and blogs, I would like to draw their attention to the fact that Java is <strong>still</strong> an interpreted language no matter if the latest JVM versions do some on-the-fly compilation of the code and as such it is inherently slower than any language that generates native code.</p>
<p>As my hobby is computer graphics I have to talk a bit about the case of PC games. Have anybody seen a game fully written in Java or any other interpreter based language that produces state-of-the-art graphics which competes with the best professional games and it also provides the same performance? I think not, and this is not a coincidence.</p>
<p>GPUs are getting more horsepower in a much faster pace than CPUs. That is what resulted in the batch crisis lately. It&#8217;s already happening for several years that the CPU is simply not powerful enough to feed the GPU with enough data in complex real-time rendering situations. There have been already tons of different software and hardware technologies which already targeted this issue a long time ago but as a perfect example that <strong>is</strong> the reason which resulted in the appearance of the completely redesigned DirectX 10 API and which emerged the need for a cleaner architecture for OpenGL as well. That&#8217;s why there are no advanced games written in interpreted languages as they waste those very valuable CPU cycles which would have great benefits otherwise.</p>
<blockquote><p>&#8220;&#8230; Java has a much greater expression power&#8230;&#8221;</p></blockquote>
<p>I tend to agree with this one. As these languages are interpreted by a virtual machine they provide great room for such features that would be impossible or simply very inefficient in a native language. That&#8217;s why one of my favorite programming language is PHP as an example. It is even more flexible then Java, for example, but never forget that PHP never meant to try to replace native languages as instead it is used in a totally different domain.</p>
<p>There are many language features that are not available in native languages but they weren&#8217;t left out unintentionally. Their lack is primarily justified by the fact that most of these features are rather inefficient and they simply not fit in the philosophy of a performance oriented language like C++. However, the most important ones like reflection, delegates and most OOP related facilities are present for example in Delphi, which is one if not the best OOP language that doesn&#8217;t make too much trade of between flexibility and run-time performance. Also, even if most of these features are not present in C++, Objective-C and other popular native languages, they can be made available with a little programming knowledge in the particular language (maybe not just little).</p>
<blockquote><p>&#8220;&#8230; you don&#8217;t have to care about resource deallocation as there is the garbage collector&#8230;&#8221;</p></blockquote>
<p>This is one of the most annoying &#8220;features&#8221; of managed code based languages. I understand that the prevention of memory leaks can have a great impact on development effort, however the unpredictable behavior of garbage collectors is simply unacceptable in certain situations. The issue of lost references has been already addressed in almost all native languages with the use of reference counted objects so I simply don&#8217;t get it why is this still an important feature. For efficient resource management you need much finer control over the time and way how resources are released.</p>
<blockquote><p>&#8220;&#8230; you just have to care about the basics and Java will make you the rest&#8230;&#8221;</p></blockquote>
<p>While I strongly support the idea of COTS, the way how Java and friends interprets this concept is simply unacceptable for me. Sometimes you need to know what is going on under-the-hood. This is basically the same issue like that of the garbage collector. Beside that, according to my personal taste, I don&#8217;t like if the compiler tells me how to do a particular thing. I rather do it in my way and even if I tend to agree that the compile time static analysis what the Java compiler does can be very valuable in some situations, simply denying code generation even in the case of a very minor semantic problem is simply makes me angry.</p>
<blockquote><p>&#8220;&#8230; Java eliminated almost all unsafe features inherited from C++&#8230;&#8221;</p></blockquote>
<p>This is one of the things that hurts me most. While Java really managed to achieve this, it sacrificed flexibility, ease of use and the fun of programming to make this possible. One such removed feature is operator overloading. I love them! As an example a linear algebraic library, which is often used in graphics programming, or the string library is not just makes the coding more natural, it eases the reading of code which is written with these in mind and that greatly effects the maintenance cost of a particular software as the code is read ten times more than modified. Java in this domain seems too verbose for me and it is much harder to read the long method invocation chains, that is not just used to be used by the developers, but is the recommended way how to code in Java.</p>
<p>This post already ran wildly long so I will close the topic for now. As a conclusion, I have to point it out that this article presented the subject from a rather subjective perspective at the end. Sorry for that, I will recap on the topic in a later post where I will try to be more detached and I&#8217;ll present actual use case examples but for now, this is all that came from my heart so please, don&#8217;t be very harsh with me if I was very biased. I hope you enjoyed the article anyway.</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/01/manage-code-yourself/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
