{"id":7452,"date":"2010-03-29T10:31:07","date_gmt":"2010-03-29T15:31:07","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=7452"},"modified":"2010-03-29T14:24:52","modified_gmt":"2010-03-29T19:24:52","slug":"lingua-programmatica","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=7452","title":{"rendered":"Lingua Programmatica"},"content":{"rendered":"<p>Typical non-programmer question: <em>Why are there so many programming languages?  Why doesn&#8217;t everyone just pick the best one and use that?<\/em> <\/p>\n<p>Fair enough. <\/p>\n<p>The definition of the term &#8220;computer language&#8221; can be really nebulous if you encounter someone who is in the mood to engage in social griefing through pedantry.  Instructions for a machine to follow? Does that include 19th century player pianos? Machine code? Flipping switches and wiring on those first-gen electric computer-type contraptions? Considering over half the &#8220;language&#8221; consists of dragging and dropping icons, does <a href=\"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=3871\">Scratch<\/a> count?<\/p>\n<p>Let&#8217;s just sweep that all aside and assume languages began with the idea of stuff like COBOL and FORTRAN and we don&#8217;t care to refine the definition further. Humor me.<\/p>\n<p>It&#8217;s pretty much standard practice to do the &#8220;Hello World&#8221; program when you&#8217;re learning a new language.  The goal is to simply print the worlds &#8220;Hello World!&#8221;, and that&#8217;s it.  It&#8217;s basically the simplest possible program that can still do something observable and meaningful for someone new to the language. <\/p>\n<p>Here is the program in assembly language:<\/p>\n<pre><code>_start:\t\t\t\t\t\r\n&nbsp;\r\n\tmov\tedx,len\r\n\tmov\tecx,msg\r\n\tmov\tebx,1\r\n\tmov\teax,4\t\r\n\tint\t0x80\r\n\r\n\tmov\teax,1\r\n\tint\t0x80\r\n&nbsp;\r\nsection\t.data\r\n&nbsp;\r\nmsg\tdb\t'Hello, world!',0xa\r\nlen\tequ\t$ - msg\r\n<\/code><\/pre>\n<p>Here is a functionally identical program, written in standard C:<\/p>\n<pre><code>\t\t\t\t\r\n#include &lt;stdio.h&gt;\r\n&nbsp; \r\nint main(void)\r\n{\r\n    printf(\"hello, world\\n\");\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<p>And in BASIC:<\/p>\n<pre><code>\t\t\t\t\r\n10 PRINT \"Hello, world!\"\r\n<\/code><\/pre>\n<p>The first is incomprehensible to anyone who doesn&#8217;t understand assembler. The second is tricky, but you might be able to intuit what it does.  The last one is simple and obvious.  So why would you ever use anything other than BASIC? <\/p>\n<p>Here is how the trade-off works: On one end you have a powerful, flexible language that makes highly efficient code. It can be used to make anything and the code will always be extremely speedy and have the lowest possible memory overhead. (Setting aside the issue of individual programmer skill.) On the other end you have a language that&#8217;s easy to use and understand.  Some languages are optimized for specific tasks.  If you happen to be doing one of those tasks, then your work as a coder will be easier. <\/p>\n<p>Let&#8217;s say you want to write a program to take a given number and perform two tasks:<\/p>\n<p>1) Print the number normally in base ten. So, ten and a half would look like: 10.5<br \/>\n2) Print the number in a base-6 number system and use an @ symbol instead of a decimal point. So, ten and a half would look like: 14@3.  I don&#8217;t know why you would want a program that does this, but I promise this isn&#8217;t really any more arbitrary or goofy than a lot of crazy stuff a boss might assign the hapless coder. <\/p>\n<p>The first task is conventional and almost all languages will have a shortcut for making that happen. The second task is unconventional and thus we&#8217;re not likely to have a lot of built-in language tools for doing it.<\/p>\n<p>In assembler, these two tasks will be of a similar level of difficulty. You&#8217;ll have to write your own number-printing code from scratch, but when you&#8217;re done the two bits of code will be about the same level of complexity (very complex) and the same level of efficiency. (Highly optimized. (Again, this is assuming you know what you&#8217;re doing.))<\/p>\n<p>In C, the first task will be trivial, and the second will take some extra effort. Printing the base ten number will be much, much faster than printing in base 6 with @ symbols. (Although both will be so fast on modern computers you&#8217;d have trouble measuring them.  Still, if you had to print out a LOT of numbers, the differences between base 10 and base 6 would become apparent.)<\/p>\n<p>In BASIC, the first task would be super-trivial.  One line of code. The second task would require pages of code.  99% of your programing time would be spent on the second task, and it would be much, much slower than the first task. <\/p>\n<p>Assembly is referred to as a &#8220;low level&#8221; language.  You&#8217;re down there interfacing with the machine on a pure, fundamental level. Every line of code is basically an instruction carried out by the processor.  BASIC is a very high level language. You&#8217;re writing things in abstracted, human-friendly terms, and a single line of code might represent hundreds or even thousands of processor instructions.  Generally, the higher the level of the language, the more tasks become either trivially easy, or impossible.<\/p>\n<p>The C \/ C++ language seems to be the &#8220;sweet spot&#8221; in this particular tradeoff.  Most software on your computer was written in that language. But despite its dominance, there are still a tremendous number of situations where other languages are better for specific tasks.  Some examples:<\/p>\n<ul>\n<li>Java is totally cross platform. Write one bit of code and it&#8217;ll run the same everywhere. Downside: It&#8217;s slower. <del datetime=\"2010-03-29T20:23:13+00:00\">Really slow.<\/del>*\n<\/li>\n<li>Visual basic is dynamite if you need lots of small programs with simple functionality but complicated interfaces. If you need a lot of complex dialog boxes with sliders and buttons and drop downs, it will be easier to set them up in Visual Basic than C++.\n<\/li>\n<\/ul>\n<pre><code>\t\t\t\t\r\n10 PRINT \"My chosen computer language is better than yours!\"\r\n20 GOTO 10\r\n<\/code><\/pre>\n<p><small>* There. Now we can be friends again.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Typical non-programmer question: Why are there so many programming languages? Why doesn&#8217;t everyone just pick the best one and use that? Fair enough. The definition of the term &#8220;computer language&#8221; can be really nebulous if you encounter someone who is in the mood to engage in social griefing through pedantry. Instructions for a machine to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-7452","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/7452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7452"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/7452\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}