<?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>.simplicity &#187; plug-in</title>
	<atom:link href="http://www.dotsimplicity.net/tag/plug-in/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotsimplicity.net</link>
	<description>Simple, reliable, simplicity. A software discussion blog</description>
	<lastBuildDate>Sun, 04 Jul 2010 09:44:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Plug-ins: Scripts and Shared Libraries</title>
		<link>http://www.dotsimplicity.net/2009/05/plug-ins-scripts-and-shared-libraries/</link>
		<comments>http://www.dotsimplicity.net/2009/05/plug-ins-scripts-and-shared-libraries/#comments</comments>
		<pubDate>Tue, 26 May 2009 22:14:42 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Discussion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shared library]]></category>

		<guid isPermaLink="false">http://www.dotsimplicity.net/?p=285</guid>
		<description><![CDATA[In a heated discussion earlier this night, Michael and I battled our way through the chat about what we preferred for plug-ins, scripts or shared libraries. I fought for shared libraries since he&#8217;d taken the position of script defender already, and we came to a few points concerning the differences between scripts and shared libraries. [...]]]></description>
			<content:encoded><![CDATA[<p>In a heated discussion earlier this night, Michael and I battled our way through the chat about what we preferred for plug-ins, scripts or shared libraries. I fought for shared libraries since he&#8217;d taken the position of script defender already, and we came to a few points concerning the differences between scripts and shared libraries. More after the break. </p>
<p><span id="more-285"></span></p>
<h2>When the need arises</h2>
<p>A side note: since &#8220;shared libraries&#8221; was quite long to type each time, I abbreviated it to &#8220;SL&#8221;.</p>
<p>A plug-in is needed when you wish to alter the implementation of a specific part of an already compiled program. A script, as well as a SL, is simply a module which you can fit inside an already compiled program to work with that program, so they are excellent for use as a plug-in. Changing from one version of the plug-in to another would be like changing the tire of a bike: you don&#8217;t need to take the whole bike apart to replace a part of it. The same goes for programs: you don&#8217;t want to compile the whole program to alter a small part. Most IDE&#8217;s take care of this pretty nicely, take my C++ IDE Code::Blocks for instance. The IDE automatically generates a makefile and can recompile in such a way that whenever I change a implementation file, but not the header, the only steps it needs to do again are compiling that small source file and re-linking it to main. Here&#8217;s an example of the output Code::Blocks shows me when first compiling the main program with a header and an implementation for that header, and then changing the header.</p>
<pre>Compiling: main.cpp
Compiling: foo.cpp
Linking console executable: bin\Release\TMP.exe</pre>
<p>And after changing foo.cpp:</p>
<pre>Compiling: foo.cpp
Linking console executable: bin\Release\TMP.exe</pre>
<p>As you can see the most basic option is to simply get the source, compile it once (from scratch) and after that you can alter whatever you want at basically the same cost as scripts/SLs, even slightly less since the program does not have any glue-code for loading (and interpreting) SLs (and scripts). So why doesn&#8217;t everyone do this?</p>
<p>Well, most people don&#8217;t have a fast computer, compiling a big program can take a lot of time, but that&#8217;s a one-time investment so shouldn&#8217;t be a biggie.</p>
<p>What could be a biggie is not being able to compile the thing! Closed-source, yeah that won&#8217;t work. Some weird compiler costing a bunch and the program only compiles on that one? That neither.</p>
<p>To circumvent these problems, you can build your code that calls something from outside the program, such as a script or a SL!</p>
<h2>Differences</h2>
<p>So what&#8217;s the big difference between a script and a SL? Most differences come with a downside or a benefit (or both, as it depends on what you&#8217;re striving for).</p>
<h3>Interpreted vs. Compiled</h3>
<p>Scripts are interpreted at runtime, this gives them a bit of overhead at first, but this overhead should be negligible on the next passes due to the script being compiled once and thereafter being retrieved from the cache (most of the time). Further discussion about the speed and memory usage of interpreted code versus compiled code will be kept to a minimum.</p>
<h3>Deployment time</h3>
<p>SLs would take a bit longer to deploy. Change a script in any editor, save it and you&#8217;re done. To recompile a SL, I open up my project file, change the code, compile, and then I&#8217;m done. Compiling can take a few seconds and is largely dependent on the size and complexity of the code (for C(++) at least). </p>
<h3>Code Access</h3>
<p>A big advantage of SLs (in the same language, or closely related such as C and C++) is the ease of merging the module with the main program. Give the code the right headers, and you can do anything you could do in the main program. This is pretty hard to do in most scripting languages, often consisting of macros and other code that only exists to glue the script and the program together.</p>
<h3>Languages</h3>
<p>Scripts are obviously in a different language, but SLs don&#8217;t need to be. However, if SLs are in a different language, you need to learn how to compile it in such a way that the compiled code can be called from your main program&#8217;s compiled code, make sure it matches the calling convention, variable types, etc. When you want to change or add a scripting language (so that your program can use it) you need to add that functionality in your main program… It doesn&#8217;t really differ a lot in difficulty I think. However, most scripting languages are more oriented on ease-of-writing than on speed, as opposed to compiled languages. Therefore, it depends on what you want with this specific plug-in.</p>
<h3>Portability</h3>
<p>With more and more people &#8220;fighting the good fight&#8221; everyday, more people are going Linux. Therefore in general people give a bit more if their program runs on more platforms. When writing a script, most of the time you&#8217;re done for each platform: the APIs you used belong to the script, not to the platform, as is the case with compiled languages. Making compiled languages portable is a bit harder, since you are then dependent on another library such as Boost, or on some other kind of construct in your source. Preprocessor directive #ifdef works well for small algorithms in my opinion.</p>
<h3>List-it</h3>
<p>Lets whip up a quick list of benefits and downsides of the options</p>
<ul>
<li>Scripts</li>
<ul>
<li>Benefits</li>
<ul>
<li>Low deployment time</li>
<li>Potentially faster due to JIT compiling can take advantage of variables known at runtime.</li>
<li>More portable in general.</li>
</ul>
<li>Downsides</li>
<ul>
<li>Needs a library that ships along with your app (separate or statically linked, doesn&#8217;t matter), given that the library (in the right version) isn&#8217;t already available on the system.</li>
<li>Needs some &#8220;glue&#8221;-code for exposing the program&#8217;s data to the script, so you can access the program&#8217;s variable from the script.</li>
<li>Needs to be recompiled each time the program is launched, given that the interpreter doesn&#8217;t store a cache of course.</li>
</ul>
</ul>
<li>Shared Libraries</li>
<ul>
<li>Benefits</li>
<ul>
<li>Can really act like a part of the program, or at least easier then mixing compiled code with interpreted code.</li>
<li>Potentially faster due to compiler can make expensive optimizations. It&#8217;s okay for a compiler to take a bit longer to compile a SL than for a script interpreter.</li>
<li>SLs can be closed source whereas scripts obviously cannot be as easily (need to be obfuscated/repacked in some way). Some people may not find this a benefit, but I do, the choice to go open or closed shouldn&#8217;t depend on whether the code is easy to hide or not.</li>
</ul>
<li>Downsides</li>
<ul>
<li>Longer deployment time, but shouldn&#8217;t be really different.</li>
<li>User can&#8217;t really check what the library is doing, it could be &#8220;bad&#8221; code, such as a virus.</li>
</ul>
</ul>
</ul>
<p>So there you have it! A neat list, if I say it myself, with benefits and downsides of the options. I tried to make it as complete as possible but feel free to leave a comment if you think you can add something. For large programs, I think I&#8217;ll go for SLs at first, then for scripts to end up with a hybrid solution. For example: if you want to use a lot of internal classes of your C++ program, it&#8217;s way easier to go for a SL. On the other hand, if you want to be able to quickly see what different code has for effect, go for a script. If you want both you&#8217;re fsked and still have to make a choice. A good and well known example of a hybrid program is Blender: compiled in C, C++ and Python, it uses quite a lot SLs and also has support for Python scripts.</p>
<p>I hope you enjoyed reading the article, I sure did writing it. Thanks go to Maurice Bos of BosByte and Michael of <a href="http://www.dotsimplicity.net">this great site</a> for correcting and in general improving this article. Stay tuned for more articles, there&#8217;s a small abstraction tutorial coming up!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotsimplicity.net/2009/05/plug-ins-scripts-and-shared-libraries/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
