“Why should you start learning a new programming language” was the questions I asked me in the 2011?
Nowadays we are all to busy to try new things, to expand our horizon. After reading the Pragmatic
solving problems In the following chapter I will explain you, how I learned programming languages.
In the beginning there was Turbo Pascal
The first language I learned was Turbo Pascal. Everything (not only the programs) looked like a
great “geek nerving god tool”. I quite don’t understand what I was doing with Turbo Pascal as my
computer science teacher told me to make some simple calculation the perimeter for triangular and
other geometric object. It was great to declare your variables and then put them into a function and
print line the outcome. Here are some images:
Turbo Pascal was my first procedural language and later during my studies I learned in a lecture
about compiler construction that this language was developed 1970 by Niklaus Wirth (whom I admire
for building such easy and effective techniques like the recursive descendant, but thats another
tale). If you want to have this retro feeling just check out Ubuntu resources (or other things if
you want to stay up).
The following images show the “IDE”. It has few options with fancy looking buttons and no
options-overload.
In secondary school we had written some programs in Delphi (original it had the name Object Pascal).
The new things was that even the girls had little fun in writing programs in it (some nice “Hello
Kitty” in with pink color, but they actually love it). We learned how to write functions and to
create some appealing graphics. After that I lost my interests in programming because the age of
warhammer or video games.
Then Java came …
When I started my studies of computer science I knew I had still no programming experience and
during my first year I had to learn Java. It was quite good and quite bad if you ask my. The good
things about it was that I had to learn an exhausting new methodologies: OOP, Interfaces, Recursion,
Inheritance, Encapsulation, Polymorphism and many other concept. I was lost, I just wanted to learn
how to write simple programs and no one could tell me, why I have to write this public static void
main (String args[]) to start my program. My fellow students told my that I was an absolute newbie
(they were actually right).
Chad Fowler wrote in his book The Passionate Programmer: “Always be the worst guy in every band
you’re in.” I actually was it and in now time I made great progresses. But some aspects of Java hit
my but: It was so long to express simple things, so much overhead with static, private and so on. To
what I mean have a look on the following code:
Ok, you may say: “It’s quite readably” but wait until you have written, about several kilo of locs
and want to maintain or refactor it. I thought that I could understand my “well” written mail client
but well afterwards you know it better.
For another term I looked on C++. It was good to see how pointer (or references) are working, how
you must allocate your memory (Java has it’s gc) and generics (or templates) for your data
structures. I nearly learned something new, C++ is like Java, it’s OOP and you can program
procedural if you like to. Have a look on some code:
/** 2.1* Author: Matthias Guenther* function: repeated shortening of breaches with a ggT* use the Euklid-Algo to compute the ggT*/#include <iostream>usingnamespacestd;intcompute_ggT(inta,intb){if(b==0){returna;}else{returncompute_ggT(b,a%b);}}voidshortening_breach(){inta,b,shortening_factor;stringq;do{cout<<"Please input the parameters to solve the breach: a/b\n";cout<<"[Input] Parameter a: ";cin>>a;cout<<"[Input] Parameter b: ";cin>>b;// look at the ggTif(b==0){cout<<"This is not allowed";}else{shortening_factor=compute_ggT(a,b);a=a/shortening_factor;b=b/shortening_factor;cout<<"The breach is:"<<a<<"/"<<b<<"\n Continue?""\n [Input \"q\" to leave the programm or \"c\" to ""continue]";cin>>q;cout<<"\n ";}}while(q!="q");}intmain(){shortening_breach();return0;}
Enlightenment with Ruby
Normally I’m not the guy how runs after every hype but I haven’t found so far my beloved programming
language. Why not risk a look. And then there was this ‘Whosh’ (think DC Comics). You could program
in different styles, could design your own languages (called DSL) and this nifty framework Ruby on
Rails (RoR) with Test-Driven Development (TDD), Behavior Driven Development (BDD), Continuous
Integration (CI) and many more things. These things sound a like a mystery for me, but just get
started and see how “easy” you can use it with RoR. It hits my head.
It was a very hard steady learning curve but it was more pleasantly then just go to the candy shop
and get what you want for nothing. You have to invest much time. But look for yourself on ruby - it
looks like a natural language. Just look at a script I wrote to tag MP3:
require"mp3info"# setting the basedirectorybase_directory="check/"# run through all directories and get the mp3Dir["#{base_directory}**/*.mp3"].eachdo|mp3|# get the directoryarr_directory=mp3.split("/")[0..mp3.split("/").length-2]# empty string which will be merged to a string and which is needed for file renaming as a pathdirectory_of_album=""arr_directory.eachdo|part|directory_of_album<<part+"/"enddirectory_of_album=directory_of_album.gsub("//","/")mp3_track=""# open the mp3 to read the id3 infosMp3Info.open(mp3)do|track|# delete genretrack.tag2.TCON="Game"# check, if the track title existsiftrack.tag.title!=nil# mp3 with an underscore are translated in / so we must catch thistrack_string=track.tag.title.gsub("/","")track.tag.title=track_stringelseputs"Please set the track-tag for #{mp3.split("/").last} ... NOT converted"end# save mp3 tagmp3_track=trackend# rename filesifmp3_track.tag.title!=nilmp3_newname="#{Dir.pwd}/#{directory_of_album}#{mp3_track.tag.tracknum} - #{mp3_track.tag.title}.mp3"output=mp3_newname.split("/")File.rename(mp3,mp3_newname)puts"#{output[-1]} ... converted [#{mp3_track.tag.album}]"endend
Automating tasks
I resisted very long to learn the bash, but there were so many tasks over the years which I didn’t
do because t gives me the heebie-jeebies to do the same thing over and over again. After one weekend
studying the open book “The Linux Command Line” by William E. Shotts I caught fire. For a long time
I messed my desktop up with tons of images with ugly names, so I wrote a script to change this.
I have a bunch of images with ugly name, so I wrote a script to change this. Each command has it’s
purpose you have to learn its syntax. Here is some sample code (pretty neat):
#!/bin/bashz=`ls | wc -l`z=0
for i in *.jpg; do z=$[$z+1]; mv "$i"${i##*.jpg}$z\_$1.jpg; echo$i ; done# replace watermark_ (prefix)
It’s like Lego, you have these and that, plug it together to create something
new. For my diplom thesis I create a Rakefile (the modern version of make
written completely in ruby):
desc"Create a directory for the compiled classes"task:create_class_directorydosystem'mkdir -p classes'# overwrite the directory if it existenddesc"compile the plugin and put the files in the directory"task:compile_plugindosystem'fsc -d classes Plugin.scala'enddesc"copy the plugin descriptor"task:copy_plugin_descriptordosystem'cp scalac-plugin.xml classes'enddesc"create the jar file"task:create_jar_filedosystem'cd classes; jar cf ../divbyzero.jar .'enddesc"create environment"task:create_environment=>[:create_class_directory,:compile_plugin,:copy_plugin_descriptor,:create_jar_file]doputs'done with creating the jar file ...'enddesc"compile the traits"task:compile_traitsdosystem'fsc -d classes TraitProgrammingHelper.scala'system'fsc -d classes TraitUpdaterHelper.scala'system'fsc -d classes TraitArithmeticRulesHelper.scala'system'fsc -d classes TraitBasicRulesHelper.scala'enddesc"create environment with traits"task:create_environment_complete=>[:compile_traits,:create_environment]desc"run example file"task:test=>[:create_environment_complete]dosystem'scalac -Xplugin:divbyzero.jar Example.scala'end# ...
Head scratching with Scala
I learned this language for my diploma-thesis, otherwise I still wouldn’t put a thumb on FP. So far
I must say that it was a good decision to learn an functional language. Scala showed me how to use
chained function, anonymous functions, currying, type bounds and many more constructs. In the
beginning my head howls like a steam-tank, there was so much new terrain for me that I need several
breaks to get used to it. After many hours of happy cramming I got surprisingly insights in doing
OOP better. That was is, what the Pragmatic Programmers were preaching. To get new insight in a
technology you have to regard it’s counterpart. Don’t hesitate to do something you would never do,
you gain experience and more confidence in your profession.
Here is some of Scala code:
classReference[T]{privatevarcontents:T=_defset(value:T){contents=value}defget:T=contents}objectIntegerReference{defmain(args:Array[String]){valcell=newReference[Int]cell.set(13)println("Reference contains the half of "+(cell.get*2))}}
Conclusion
This was my first essay. I tried to cover the things which whom I’m actually dealing. I presented my
wide route from my programming experience so that you can see, how the things evolved for me. I
always get my hands dirty and try to roll up my sleeves to get a touch about the newest technology.
I think through adventuring you become each day a little bit better then yesterday.
Thanks to Hanna Schuett for reading an earlier draft of this post.