Programming languages I like

Since I began programming in 7th grade, I’ve used dozens of languages to do many different things. Some of these languages I liked, and some I did not.

The languages I like seem to have some basic things in common. The most obvious one is that they’re simple. Simple can mean one of many things, though, in a programming language. Maybe it would help if I used examples (in approximate order of increasing level of abstraction):

  • C
  • Scheme
  • Python
  • Ruby

These are all languages that I consider “simple." There are many aspects to the simplicity of a programming language, but I think the primary one is this: It is easy, once you know a few simple rules about how the language works, to write non-trivial projects. This holds for the above four languages. There aren’t too many special cases for a programmer to remember.

In C, the only thing you need to hold in your mind is the basic abstraction of how the computer is actually doing computation. Once you have that, and a few syntax rules, you can write C effectively.

In Scheme, you need only one syntactic structure: The S-expression. You also need to understand the difference between functions and macros, but beyond that there are literally no special cases whatsoever. Function application is easy to wrap your head around, too, if you have a decent guide (Say it with me: “Thanks, Abelson and Sussman!").

Python and Ruby have objects at the center of their designs. In Ruby, everything you do is some combination of objects, methods, and blocks. In Python there are more special things to remember (list comprehensions, for loops, and iterators, to enumerate one category), but you mostly don’t *need* to remember them. And you can look them up when you do need them.

Compare these to a couple other languages I’ve been forced to use recently: C++ and PHP.

C++ is the antithesis of “simple." There are a million things to remember when writing C++ code. What exactly does “const static virtual string& my_method(void);" mean about that method? And why does the compiler freak out about the string reference being a const when I try to use it later? I have never written more than 30 lines of C++ without making some stupid mistake, and I’ve probably written more C++ than I have any other language. I get the impression that you would need to write C++ for years and years to ever even approach “proficiency."

PHP is the other antithesis of “simple." It’s the least consistent language imaginable. How anyone can write PHP without a reference manual in their lap is totally beyond me. Should functions be camelCased or under_scored? The language “designers" couldn’t figure it out, either! What gets cast automatically and what doesn’t? I guess we’ll find out! Objects? Yeah, we have those!

I guess the conclusion of all of this is that one should write interfaces that are consistent, that stay out of the way, and that require very little figurative disk-space in the user’s mind.

Long-Forgotten Resource

Well, I did end up finding a good resource for anyone interested in programming but not sure where to find extra, concise learning resources. If interested, try Safari Books Online. It’s an online database provided by Virginia Tech. In fact, the school pays thousands of dollars, part of each student’s tuition, to have access to these resources. So if we’re paying for these databases it can’t hurt to actually use them!

All you need to type in the search box is what you’re really interested in. Just typing in “Python Programming” brings up results and titles ranging from “Intro to…” to “Expert Python Programming”; it’s perfect for any level of programmer. There’s tons of professionally done books on other languages from Java to Tcl and systems like the PIC microcontroller. This database has already sorted through the unworthy or cluttered resources that appear at the top of Google searches. You’re much better off finding serious help in this database than through a Google search.

TIP: When you get your required textbook list for next semester, look up the titles in this database. The book might be available for free and you could save yourself tons of money!

Structure, Language and Art

In a recent post tylera5 commented that the last time he wrote poetry was in high school, and wasn’t expecting to have to write a poem for a programming course. I got the idea for a poetry assignment from a friend of mine who teaches a biological science course. She found that the challenge of condensing a technical topic into a 17 syllable Haiku really forces one to think critically about the subject and filter through all the information to shake out the key concept. And poems about tech topics are just fun to read!

I think the benefit is even increased for a programming course. As tylera5 mentioned, both poems had a structure, and he had to think a bit about how to put his thoughts into the structure dictated by the poetry form, whether it be the 5/7/5 syllable structure of a Haiku, or the AABBA rhyming scheme of a limerick.

Poetry is the expression of ideas and thoughts through structured language (and the structure can play a larger or lesser roll depending on the poet, and type of poetry). Programming also is the expression of ideas and thoughts through structured language. The domain of ideas is often more restricted (though not necessarily, this article and book could be the subject of a full post in its own right) and adherence to structure is more strict, but there is an art to both forms of expression.

Are there artistic and expressive tools in other STEM topics as well?

Is There a Better Way to Run a Programming Class?

Last year in ECE2574, a C++ Data Structures class, there were 64 students crammed into a small classroom on the first day. Just two weeks later, there were no more than 15 students coming to class on a regular basis. The worst I saw was just 8 students. I thought the class was just too hard for some people so they dropped it. However, on the day of the final exam, over 50 people showed up!! I finally came to the conclusion that students, including me, don’t seem to get much out of classroom teaching of programming so they just don’t show up.

So is there a better way to keep students in programming classes engaged so that they not only come to class but participate and ask/answer questions?

Based on the programming classes I’ve been in, nothing has worked. Most professors accept that too but keep on going because they’re at least reaching a few students who are getting the most out of their [parent's] $24,000/year payments.

My suggestion for programming classes: have a LIVE feed or terminal window that everyone can log onto/access and post questions or answers to coding solutions. To avoid fear of asking stupid answers, just first and last initials could be used to hide the identity of a shy student. Also, include color codes(red for answers, blue for questions, etc) just for fun and make it a little more user-friendly and fun to use!

Having a live feed that everyone can see and post too will keep the classes fast paced and won’t drag on and make kids feel like it’s useless to come to class. Instead, this new live feed will provide speedy interaction between students, other students, and their professor. As a result, classes can cover more topics and make sure no one is completely left behind.

So… anyone want to create that? I’m a bit busy to even google to see if an app exists already.

Python Programming – reposted

I originally posted this on Sept 19th but it was on the wrong blog. I am re-posting to the correct blog.

Having programmed in C++, Java, Matlab, and some low level languages I thought programming in python would be a piece of cake. This isn’t turning out to be the case. I have about 21 more hours to complete homework 3 and I am far from having it down. Even when going through the “Learn python the hard way” exercises I am not prepared for the homework assignment. I feel like the assignments cover things that aren’t mentioned anywhere in the LPTHW book. When asking others for help they send me the link to LPTHW and tell me to google what I need because python is so well documented.

I don’t really like that kind of learning for a programming language. I know many people had to learn it this way for C++ in the intro class and they struggled with it as well. I’d rather have a separate class for python or allocate more time for python in class. When learning a language you should learn all of the ins and outs instead of a general overview. This opinion may be biased based on the fast approaching due date for HW3 so don’t judge this rant too harshly.

Response to “Advanced Python exercises”

Recently Matt brought up some good points in his post Advanced Python exercises.  First, the more easily addressed one:

To break up a Python program into multiple functions, just store related functions in a separate `.py` file, then in the main source use import

For example, if you have a file named `hello.py` that contains

def greeting(string):
    return "Hello, {}".format(string)

Then in your main source file (located in the same directory), you can import `hello.py` and all the functions will be available from the `hello` namespace:

import hello

print hello.greeting("world")

Easy as .py! (sorry)

Also in the post Matt talked about Python’s use of “try” and “except” as a means of flow control, as an example:

try:
    mynumber = float(line)
except ValueError as e:
    sys.stderr.write("We have a problem: {}".format(e))
else:
    print "We have the number {}".format(mynumber)

He said “if complex return types are needed such that you’re throwing exceptions to communicate logic information rather than true fatal errors, your function needs to be redesigned.” which I agree with. But I disagree that Python itself relies on this technique, or encourages it, though of course individual developers may miss-use it.

Like the programs they are a part of functions should be written to do one thing and one thing well. In the preceding example the function float converts its argument to a floating point value. The name of the function makes it very clear what it does, and it does its job well. If it can’t do it’s job, then it raises a ValueError exception.  All other built in and library functions I’ve seen work the same way.  Think about the alternative without exceptions, for example, C:

double n = atof(line);
printf ( "We have the number %f\n" , n);
//Except if n == 0 it could be because there was no valid numeric expression in line

According to the documentation for atof:

On success, the function returns the converted floating point number as a double value.
If no valid conversion could be performed, the function returns zero (0.0).
There is no standard specification on what happens when the
converted value would be out of the range of representable values by a double. 
See strtod for a more robust cross-platform alternative when this is a possibility.

This is less than ideal. If we wanted to check if an input even contained a valid numeric string (which we usually would) we’d have to work harder. The strtod alternative provides a means to do that, but we’d still have to do an explicit check after calling the function.  Other C-style functions use a return code to indicate success or failure.  It is also extremely easy to forget to check return codes, in which case the problem may only manifest itself in a crash later on, or worse, not at all, but instead just produce bad output.  These types of problems are very hard to track down and debug.  Using exceptions the program crashes precisely where the problem occurred unless the programmer handles that particular exception.

So to summarize: Each of your functions should have a well-defined job.  They should do only one job and do it well.  If they can’t do their job because of improper input, then they should raise an appropriate exception.  I think following that idiom results in cleaner, easier to debug code.  You could certainly still return complex data types where appropriate, but trying to incorporate success/failure information in a return type will often lead to difficult to debug errors when the programmer forgets to check the return status!

Mary Poppins

I just finished up Ruby – Day 3 from Seven Languages in Seven Weeks.  I can’t say I fully grok everything that I’ve done, but Ruby seemed fairly straight forward and in many ways similar to Python (though I have noticed Rubyites tend to be able to on a moments notice generate a long list of why it’s better than Python).

I had originally wanted to learn Ruby because I’ve been trying to re-launch my website and blog using nanoc, which is written in Ruby.  While strictly speaking it isn’t necessary to understand Ruby to use nanoc, any type of customization will require writing and manipulating Ruby code, and knowing me, I’ll want to do some customization.  I feel I understand enough of the syntax and a bit of the power-features of the language to get by understanding the bits of nanoc that I might be customizing, but I definitely have a long way to go with a lot more experimenting before I could say I’m comfortable with the language, but I suppose that’s to be expected!

Maybe it’s due to my very preliminary understanding of it, but I feel kind of let down by all the hype.  Perhaps it’s because it’s an OOP language, and so in many respects similar to Python, or even C++ in terms of how the code is structured, but I didn’t really get terribly excited about it as I was working with it.  I’m really looking forward to the other languages in the book and breaking away from OOP thinking.  From the descriptions, it sounds like each of them encourage a different type of thinking when writing code and designing a program.  Even though it’s last on the list, I I’m going to start the Haskell section next, mainly because I’ve already dabbled in it a bit, and I’d like to write up an assignment using a functional programming language, so I should probably learn enough of one to be able to actually write up a solution!