Learning to program (ii) – an interactive Julia program

In the last post we created a simple program to calculate the approximate age of a particular species of tree, the Silver Maple, given the circumference of the tree.

d = 94.25 / pi
g = 3.0
age = d * g
println(age)

How is that program improved upon? Firstly by adding some input for the user to allow for any tree circumference to be entered by the programs user. This is achieved by first prompting the user, (because otherwise they would not know what value to input), and then reading in the value typed by the user. Note that as the program is now using a changing value for the circumference, the value 94.25 should be replaced with c, the value input by the user.

println("Enter the circumference of the tree: ")
c = parse(chomp(readline()))
d = c / pi 
g = 3.0 
age = d * g 
println(age)

We should add some things that make the input and output easier to decipher. The units are helpful, in this case inches, and the time units for the output:

println("Enter the circumference of the tree (inches): ") 
c = parse(chomp(readline())) 
d = c / pi 
g = 3.0 
age = d * g 
println("The tree is ", age, " years old")

When the program is run, it looks like this:

Enter the circumference of the tree (inches):
98
The tree is 93.58310653803446 years old

Of course the answer has way too many decimal places to be useful. So the println statement can be modified to round the answer to 2 decimal places:

println("Enter the circumference of the tree (inches): ") 
c = parse(chomp(readline())) 
d = c / pi 
g = 3.0 
age = d * g 
println("The tree is ", round(age,2), " years old")

Now the answer is much more user friendly:

Enter the circumference of the tree (inches):
98
The tree is 93.58 years old

Now of course this program only works with Silver Maples, so it’s not that useful. In the next post, we’ll see how to make the program a little more broader in scope.

 

 

 

Learning to program (i) – a simple program in Julia

Let’s say we wanted a program to calculate the approximate age of a tree. This can be done if you have the trees diameter, and know what its growth factor is. Of course to calculate the diameter, you would have to chop a cross-section through a tree, which may not be convenient (if you want the tree to keep standing). But the diameter can be calculated from the tree circumference, which is easy to obtain.

diameter = circumference / π

The growth factor is specific to each tree species. For example, the growth factor for a Silver Maple is 3.0, for American elms it is 4.0. So if  the circumference of a Silver Maple is 94.25 inches, then the diameter will be 30″. Then multiplying this by the growth factor gives an age of approximately 90 years. In Julia calculating this is as simple as:

d = 94.25 / pi
g = 3.0
age = d * g

Here is what it looks like in an interactive session of Julia:

It’s almost like using a calculator. The value “d” is used to store the value of the calculated diameter, and the value “g” is used to store the growth factor for Silver Maples. The age of the tree is stored in the value “age“.

At the moment, these are program snippets, and not a “cohesive” program. To make that we would have to put it in a file, and give the file a name (in this case treeage.jl). We also have to add a means of printing out the result – which means adding a println() statement.

d = 94.25 / pi
g = 3.0
age = d * g
println(age)

Now we can run the program by typing:

julia treeage.jl

There we have it, a simple Julia program. There are still things missing from it, but it is a starting point.

 

 

So you want to get a PhD?

What type of PhD?
How many years years do you want to spend in school? In North America the model is normally undergrad (4-5) + masters (2) + doctorate (3-?). So many years. The British system is slightly different with 3 years for a typical undergrad, followed by a year for a honours degree, and then some schools allow direct entry into a PhD. The other difference is that British-style doctorates are often research-based, versus the coursework etc. required of many North American degrees (i.e. it will probably take less time). So 10-12 years in school? If you’re lucky you’ll get a scholarship to do the PhD., otherwise, you’ll have to find funding. There was a time when you didn’t need a PhD to teach at a university, but this seems to be rare now, more’s the pity. There are many talented people with masters, bachelors, or even no degrees who would make exceptional teachers.

Teaching? – NOT!
Here’s the thing, most PhD programs do *not* teach you how to teach. They are concerned primarily with research. This doesn’t make a lot of sense considering 50% of an academic job is usually concerned with teaching. That’s why you end up with some people who have degrees up the wazoo, but can’t teach to save themselves. Other’s who are exceptional teachers, may not excel at research (but might do well at pedagogical research, which for some reason is not always considered to be “real” research). Usually it’s because they have no time to do research, or live somewhere where they do not fund teaching research (like here in Canada). So learning to teach basically involves “teaching yourself”. Also, not all classes are 15 students. Many first and second year classes are huge these days, especially in science/engineering.

A career in research
If you’re into research, you’ll spend time writing grants to get funding to pursue your research. Most universities do *not* provide you with funding of any sort, except for maybe a start-up grant when you start in a position. In some countries, there are very few funding bodies, and as such it is often difficult to obtain funding. These funding decisions are made by committees of “peers” from your discipline… and if they don’t like your proposal, they won’t fund it – no matter how positively reviewed it is (externally), or how useful the research may be. Sometimes I think they almost expect you to be a research star by the 2nd year of your career, which is completely counter-intuitive. Research takes time. You’ll also have to publish a lot. How meaningful is this? I don’t really know. Many papers certainly aren’t readable by people outside the field, and some are not even readable by people within it. People should learn to write readable papers – but then again they don’t teach you this either (and it seems to be getting progressively worse – papers written in the 1960’s -1980’s are often magically readable). Have a passion for teaching and want to become a teaching focused faculty? In many places that’s not well looked upon, for whatever reason.

Disorgademia
Do you like working in a place that may not be well run? Well then academia is for you. Academics are not trained to run large organizations, and as such many make a complete hash of it. This isn’t to say that commercial companies are any better, but people there usually have a slightly better sense of dealing with other people (generally). Some universities try to act like companies, but they are not.  Universities are nearly always non-profit organizations, and from the publics perspective teaching is their core activity. Despite their mantra, universities are also very bureaucratic, and change can be vexingly slow (like snails pace slow). It is a government institution, so that’s not really surprising.

Career
In reality, there is little course for advancing your career. From Assistant Professor, one is usually promoted (at tenure) to Associate. Beyond that, one can advocate for promotion to full professor. What does it buy you? Prestige? Enlightenment? Ego? I have yet to figure it out. The only other alternative is to move into the “admin” side of things, which I would advise to stay well clear of. In commercial companies, there is often quite a few layers of promotion – and rewards, e.g. bonuses (there is no such thing in universities, especially unionized ones). Note that compensation in Canadian universities is extremely good when compared to other places like the US (and even the UK), where you often have to find ways of paying yourself in the summer. It does provide for a very stable career (again in Canada, other places it may not be so).

The pluses?
There are of course benefits, one being that you have the freedom to research what you want. This has good and bad points. You have to manage your research yourself, and find the $$ to fund graduate students, trips to conferences, equipment, journal publishing etc. But research takes time, and if you have large classes, you may find little time for much writing. Hours are usually flexible, but as a general rule you will always work more hours than you should. With the advent of email, it’s often hard to escape work.

 

Learning to program – One algorithm, many languages

Once you know one language, it’s easy to convert a program to run in other languages. Here is the same “interest calculator” program, just written in Fortran. Note the subtle differences between Fortran and Julia? Firstly Fortran uses what could be termed a program “wrapper“, to enclose the whole program. Next it requires the values input and calculated to be “defined” as types. This just means that I, R, P, and t are all real values. The rest of the program is roughly the same, even though some of the syntax has changed. What is Julia was accomplished by using parse(chomp(readline())), is now performed in Fortran by read(*,*).

program interest

   real :: I, r, P, t

   write (*,*) 'Enter principal amount: '
   read (*,*) P
   write (*,*) 'Enter interest rate (0-100): '
   read (*,*) r
   r = r / 100.0
   write (*,*) 'Enter period of time: '
   read (*,*) t

   I = P * r * t
   write (*,*) 'Interest = ', I

end program interest

You could also try it in a more cryptic C. I would not recommend C as a language  for beginners, primarily because it uses constructs which might be unfamiliar to novice programmers. The “wrapper“, becomes somewhat more complex, and read/write are replaced with the more cryptic scanf/printf. combination.

#include <stdio.h>

int main(void)
{
   float I, r, P, t;

   printf("Enter principal amount: ");
   scanf("%f", &P);
   printf("Enter interest rate (0-100): ");
   scanf("%f", &r);
   r = r / 100.0;
   printf("Enter period of time: ");
   scanf("%f", &t);

   I = P * r * t;
   printf("Interest = %.2f\n", I);
}

Now you can see why Julia is a great language for novice programmers.

Software craftsmanship

What we do in computer science is not really about the computer, and it’s not really a science. Go figure. Computers are generally designed by computer engineers, and whilst computer science does delve into the world of hardware, it is not the core goal. Science on the other hand is described in the dictionary as: “the intellectual and practical activity encompassing the systematic study of the structure and behavior of the physical and natural world through observation and experiment”.

Computers have nothing to do with the physical or natural world per se. Science is driven by observation. Computers are driven by technology, and in as much as computer engineering helps build them, the discipline is not a engineering discipline. So, not a science, not an engineering. It may have the same dilemma as mathematics – is it an art of a science, or something in between? An earlier post of mine dabbled in the notion that it is a craft, and the longer I am in this field the more I believe that. It is belief that has been around since the early 1990’s when Jack W. Reeves’ wrote an essay titled “What Is Software Design?“. In it he postulates:

“Software development is still more a craft than an engineering discipline. This is primarily because of a lack of rigor in the critical processes of validating and improving a design.”

You can design a bridge and be fairly certain it will work on paper. The same can not be said of software. In this article, “Why Software Development is a Craft“, the author Doug Bradbury makes the point that “A furniture maker cuts a joint with his hands, then fits the two pieces together. If the alignment isn’t perfect, he sands and trims and fills until the joint is true. It’s a cycle of shaping and observing.”. Is coding any different? Code is a physical entity, like a piece of wood, which must be properly crafted to obtain the right outcome.

It should be time that we actually started treating “computer science”, as the true craft it is, and throw away ideas of it being a science, or an engineering.

Learning to program – throw yourself in at the deep end

If you have a yearning to learn how to program, then the best way to go about it may be the “deep end” of the pool approach. What I mean by this is to just try and code a program. Pick a problem you know something about (i.e. you know what the result will be), and design a program. Of course you’ll need a language to code in – and I would strongly suggest Julia.

The first thing to understand about programs is that they take data and turn it into information. For example suppose we wanted to write a program to calculate the amount of interest earned on a sum of money. We would need a number of pieces of data:

  • the original sum of money, or principal (P)
  • the interest rate (r)
  • the compounding period of time (t)

Then we need a formula to calculate the interest earned, I.. The simplest formula is:

I = P r t

So, if P=$1000.0, r=3.7%, and t=4 years, then the calculation can be performed in the following manner:

I = 1000.0 × 0.037 × 4
I = 148.0

These pieces of data are all numbers that have a fractional part. For example the interest rate, r, is 3.7%, or 0.037. The choice of data has an impact on the type of data used in a program, effecting the amount of memory used, and how precise calculations will be. A Julia program to perform this calculation might look like this:

P = 1000.0
r = 0.037
t = 4.0
I = P * r * t
println("Interest = ", I)

The program is very simple, and achieves exactly what it needs to. Of course it can only calculate one value for the interest, because P, r, and t are static. Next, maybe add a means of allowing the user to input values, making the program more generic.

println("Enter principal amount: ")
P = parse(chomp(readline()))

println("Enter interest rate (0-100): ")
r = parse(chomp(readline()))
r = r / 100.0

println("Enter period of time: ")
t = parse(chomp(readline()))

I = P * r * t 
println("Interest = ", I)

This program is longer, but allows each of the values to be input by the user. At this stage it doesn’t matter that you don’t completely understand the parse(chomp(readline())) code. Just know that is reads in a number. Now the program works, it can be used as the basis for building other similar programs.

The truth is that for many people there may be no systematic way of learning to program. I learned through trial-and-error, and honestly it’s not a terrible way to learn.

Debunking myths about programming languages

Super programming languages are best.
There is no such thing as a “super” programming language. There is no “secret weapon” that will make writing a piece of software easier, and faster. If the algorithm isn’t any good, it doesn’t matter what the language is, it won’t save it. Garbage in, garbage out.

All languages are created equal.
Ah, no. Certain languages are good at some things, others are good at other things. For instance C is a fantastic systems language, Cobol is great for business stuff, Fortran is good at scientific work, and parallel processing, Ada is good for real-time systems. There is *no* one language to rule them all.

Fortran and Cobol are old, and quite useless.
Really? Fortran is 60 years old, C is 45. There isn’t much difference. You may not hear much about Fortran or Cobol, but that’s because they are quiet achievers. Not brash and loud-moutheed like Java. It doesn’t matter how *old* a language is, as long as it can do what you want it to do.

Object-oriented languages are better.
Not. OO appeared in the mainstream in the mid 1980s in the guise of C++. OO is a *methodology* for designing software, not a religious experience. Many languages (even Cobol) have gone down the OO route. Some people love OO so much one wonders if it is a cult of some sort.

GOTOs are harmful.
Yes, yes they are. Don’t think that you should use them in your programs. The unrestricted use of goto’s stems from the early years of languages such as Fortran. They were “it”, before other structures appeared. If your program is using a lot of them, then there is something wrong. Experienced programmers know where they can use goto’s. If you don’t understand where problems could occur, don’t use goto.

Compiled code always works perfectly.
??? The compiler will try and turn your program into something that can be interpreted by a machine. If the program compiles, it just means that the proper syntax was used, and the compiler was happy. Doesn’t mean there isn’t some hidden logic error in there somewhere. The larger the program and the  more complex the algorithm, the more likelihood that something might go wrong, sometime.

Python is better than C.
Like I said, no one language is better than another. C does well with systems programming, dealing with memory, low level stuff. Python is a good glue language, and is good for rapid development. C is a compiled language, Python is an interpreted language. C’s programs are fast, Python is slower. C is not an easy language for novice programmers, Python is. C requires very few dependencies, Python requires many depending on what you want to do. They are different, and that’s okay. And besides Python is written in C.

 

 

 

Debunking myths about programming (ii)

Programming is hard.

Actually programming is not that difficult. However you have to be willing to practice. You actually may find that it is quite easy to “code”. It is really no more difficult than learning to cook. People say that’s hard too, but hey are usually the ones who don’t put any time or effort into it. Sometimes people think it’s hard because their program fails, and it takes them four times as long as their friends to get it working. Who cares how long it takes. Failure is a part of the process of programming, and people who experience failure often have a better understanding that programs can fail, and how to make them more robust.

Programming is boring.

Anything can be boring. But programming is not all about sitting around “writing” code. The first thing that has to happen is a problem has to be solved – this may actually be the most challenging part of writing a program. If you were coding the same piece of code all the time, yes it would likely become boring… but find an office job that isn’t somewhat boring.

Once you learn to code, you can write an app, and make $1,000,000.

Yeah,… no. Writing apps for the iPhone was likely to make you lots of $ in the years after its release. Maybe not so much anymore. To make a fortune on apps, you first have to come up with a idea that (i) solves a problem, (ii) people want, and (iii) has little or no competition. Then you have to design it, test it, and make sure it’s user friendly. Not a trivial task. Yes, learning to program will get you part of the way, but it is no panacea for becoming a millionaire fast.

Girl’s can’t code.

Yeah… why not? That’s just like saying girls can’t do math (and they can). For some reason women tend to shy away from computer science, but years back there were more women in CS. In the mid-80s women accounted for 37% of all CS undergraduate students. Now they make up roughly 18%. Why, nobody really knows. It could be the “nerdy programmer”  moniker associated with people in CS? Regardless, times have changed, CS is no longer geeky. Some of the most knowledgable and forward thinking students in my classes are women, so girls CAN code.

Debunking myths about programming (i)

Debunking some common myths about programming…

You have to be proficient in math to learn to program.

Well, you do need to know some math. But isn’t  there a relationship between math and computer science? Well, yes and no. Most of the first programming languages were developed by mathematicians, but that is mostly because computers were used largely for numerical calculations, so it makes sense that mathematics had a large role to play. Programmers, by-and-large will spend most of their time writing code, not mathematical formulas, so you don’t have to have a wealth of knowledge in calculus. In fact if anything is important, knowledge about linear algebra and matrices makes the most sense. Undoubtedly there are application areas, such as physics, and game programming where you will need more advanced mathematical skills – but few people are lone programmers these days, so there will always be someone on a team with this skill-set.

You have to have go to university to learn to program.

Many people have been successful in the computing world without a degree. It’s not hard to teach yourself how to program. Even students in university have to have some form of self-learning ability, because the pace of the industry is swift, and you often have to learn things that are cutting edge by yourself. If you are intrigued, read this article.

You must learn only the most “popular” programming language.

Define popular? There are a bunch of websites which publish lists of programming languages. Some of them list languages by the “number of jobs”. The top three from indeed.com are SQL, Java and Javascript. Others like PYPL (PopularitY of Programming Language Index) rank by how often language tutorials are searched on Google. They rank the top three as Java, Python, and PHP. Anyways, you get the picture. Lot’s of different rankings. the language with the most code written every year? Likely Cobol, and it makes it to none of the rankings because it is uncool. But it underpins most of our financial systems, and isn’t going away anytime soon.

Look, learning to program is more about the concepts of programming than any one particular language. When you have mastered the basic concepts, you will be able to apply them to any language you want… all it requires is an understanding of the languages syntax.

You can learn to program in 48 hours.

Look, honestly learning to program in 48 hours is about as successful as learning Spanish in 48 hours. It may work for 0.001% of people… but for the rest of us it doesn’t. Many languages can take years to master, so that’s why for novice programmers it’s important to start learning to program with a language that’s easy to use.

It’s like learning anything else.

Ahhh, but it’s not. Many other subjects require you to do a lot of rote learning – cramming your brain with facts, and formulas. Programming requires you to solve problems and express them in term that the computer can understand. It’s not about memorizing every last piece of syntax in a language, it’s about how to use the structures in a language to solve a problem. Programming is a craft, and like it crafts it must be practiced.

 

Life Skills 101

I think a great course, required to be taken by all students in every university should be Life Skills 101. Taught by faculty who have life skills, and let’s be honest not all of them have any skills beyond their narrow focussed research areas. This course would teach common sense things that were once taught in high schools, like shop class. We strangely enough feel comfortable cramming young peoples minds with theory about this, that and the other, but fail to impart much useful information. What about basic financial literacy? Obviously nobody is doing a good job there because the average Canadian has a debt load of $22K, *not* counting the mortgage. Debt, spending, saving, mortgages, not spending money you don’t have – all valid topics up for discussion. Next, eating. Surprisingly few people have any notion of actually cooking for themselves – not helped by a society where we are surrounded by ready-made food in one form or another. Then you get people saying things like, “oh, it’s cheaper to eat take-away”. Yeah, like it’s not, and you’re not eating well. We have never been surrounded by so much information related to food, cookbooks galore, and blogs/networks dedicated to food. Finally there are the everyday skills that must be learned, just because one day they may be of value. I tire of people saying things like, “oh, manual labour – I don’t like that”. Ok, but then don’t be surprised when a plumber charges you $200 to change a washer. It doesn’t require a lot to learn to change a lightbulb, chop wood for a fire, start a camp fire, paint a room, saw a piece of wood using a hand saw, basic car maintenance etc. There was a time when basic “home” skills were taught in shop class, but sadly shop classes have all but disappeared in many high schools.

Will we ever see this sort of a course in a mainstream university? Probably not, although Harvard did run some non-credit “Life Skills” courses for a while about 10 years ago (don’t know if they still do).