Posts Tagged ‘programming’

Quotable quotes 2

This post got me thinking: the structures that help people learn coding will also help experienced programmers be more productive.

Working in the head doesn’t scale. The head is a hardware platform that hasn’t been updated in millions of years. To enable the programmer to achieve increasingly complex feats of creativity, the environment must get the programmer out of her head, by providing an external imagination where the programmer can always be reacting to a work-in-progress.

Source: “Learnable Programming,” Bret Victor.

Office Politics 101 for Recovering Idealists. The article title says it all. I found this eye opening.

In the meantime, there’s a pretty easy rule of thumb to follow. When in doubt, keep it to yourself. Listen more than you speak and certainly more than you vent, collect as much information as possible, and ponder how it can be used to make yourself seem more of an asset to your boss than your competitors.

Source: “Office Politics 101 for Recovering Idealists”, Erik Dietrich.

I was encouraged to know that a pattern of thinking I sometimes slip into has a name. It also challenges me to overcome this distorted thinking so that I am no longer controlled by fear.

Catastrophizing essentially involves imagining and dwelling on the worst possible outcome of something. It’s basically overreacting and letting your thoughts run away to dire and highly unlikely scenarios. It’s the kind of thing that happens when you’re lying awake at three in the morning worried sick about the future and what’s going to happen to you.

Source: “Building Your Resiliency: Part VI – Quit Catastrophizing,” Brett & Kate McKay.

Patrick McKenzie with one take on a software career track. This article influenced me a lot:

Engineers are hired to create business value, not to program things:  Businesses … converge on doing things which increase revenue or reduce costs.  Status in well-run businesses generally is awarded to people who successfully take credit for doing one of these things.  (That can, but does not necessarily, entail actually doing them.)

Source: “Don’t Call Yourself A Programmer, And Other Career Advice,” Patrick McKenzie.

Quotable quotes

It would be arrogant of me to think that I have solved the problem of large-scale software development:

It is widely acknowledged that coordination of large scale software development is an extremely difficult and persistent problem.

Source: “Splitting the Organization and Integrating the Code: Conway’s Law Revisited,” Herbsleb and Grinter, 1999 (PDF).

One of the most common antipatterns of commercial software development:

In this evil, but extremely common, mirror universe, developers branch to create features. This branch stays isolated for a long time. Meanwhile, other developers are creating other branches. When it comes close to release time, all the branches get merged into trunk.

At this point, with a couple of weeks to go, the entire testing team that has been basically twiddling their thumbs finding the odd bug on trunk suddenly has a whole release worth of integration and system-level bugs to discover, as well as all the feature-level bugs which have not yet been found because nobody bothered to have the testers check the branches properly before they got integrated.

Source: Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation, Humble and Farley, 2010.

This view is too optimistic for me, given what I know about human nature and our sinful condition:

Technological optimists believe that technology makes life better. According to this view, we live longer, have more freedom, enjoy more leisure. Technology enriches us with artifacts, knowledge, and potential. Coupled with capitalism, technology has become this extraordinary tool for human development. At this point, it is central to mankind’s mission. While technology does bring some unintended consequences, innovation itself will see us through.

Source: “The Moral Character of Cryptographic Work,” Rogaway, 2015 (PDF).

This article was how I first came across the work of Erik Dietrich. Since then I’ve had a lot of fun reading his articles on software development and office politics.

In the sense of skills acquisition, one generally realizes arrested development and remains at a static skill level due to one of two reasons: maxing out on aptitude or some kind willingness to cease meaningful improvement. … [L]et’s discard the first possibility (since most professional programmers wouldn’t max out at or before bare minimum competence) and consider an interesting, specific instance of the second: voluntarily ceasing to improve because of a belief that expert status has been reached and thus further improvement is not possible. This opting into indefinite mediocrity is the entry into an oblique phase in skills acquisition that I will call “Expert Beginner.”

Source: “How Developers Stop Learning: Rise of the Expert Beginner,” Erik Dietrich.

Being objective about Objective C

(Sorry, couldn’t resist the pun.) A few months ago, I started a new job that required me to learn Objective C and Cocoa/Cocoa Touch development on the Apple platform. In this post, I’ll relate a few concepts that I learned in other languages that crossed over to the Apple platform.

MVC architecture

In web app development, the view is (arguably) the DOM. The DOM is loaded at a certain point in time: when the browser is finished parsing the initial payload, a load event is generated, and as a developer you can specify a handler for this event. This allows you to do things when that view appears, like populate it with data.

In iOS, the load event is similar to viewWillAppear. A NSViewController subclass can implement this method to perform custom behavior when the view appears, such as updating the UI based on the contents of some property.

Connecting the view to the controller

In Cocoa/Cocoa Touch, outlets and actions are used to connect objects in the view with code in the controller. Outlets are for linking objects in the view to a property in the controller. This allows the controller to manipulate that object, for example, setting the title of a button object. Actions are triggered when an event happens to an object. For example, an action responds to a click on a button. Action methods are passed the sender of that action.

I would compare outlets to DOM objects, and I would compare actions to DOM event handlers. Xcode makes creating outlets and actions a drag and drop operation, but in Javascript, you use document.getElementById or jQuery. DOM events are attached to an object, and specify a callback function, which can also take an argument of the sender (the this keyword).

Event loops

An event loop handles all incoming events and allows the application to respond appropriately. More generally, an event loop takes a queue of messages and processes them one at a time. An example of a message would be “call this callback function”. Processing that message would be actually executing the callback. For example, in Javascript, you specify a callback for the click event. When the click happens, the message to call that callback is placed on the message queue. The loop processes messages one at a time, so if you have another Javascript function that is currently processing a table (maybe shading the rows), the click callback message will not be processed immediately.

The Cocoa main event loop, GLib main event loop, and Javascript event loop are conceptually the same.

Node.js uses a single-threaded Javascript event loop to great advantage. Using Node, it is fairly easy to implement a scalable, event-driven web application.

Cocoa extends the main event loop, allowing you to create other queues. Queues are event loops that may or may not run on another thread. However, messages dispatched on the same serial queue will always be executed one at a time. This allows you to protect variables that aren’t thread safe by only accessing them from a particular queue.

The Glib event loop is used by a GUI library such as Gtk+ to make a Linux application. I’ve used these libraries while fixing a few Gnucash bugs and while writing a Twitter client.

Conclusion

My experience with Gtk+ and Javascript allowed me to quickly pick up core Cocoa concepts and design patterns. Now if I could only remember to do all UI stuff on the main queue!

Random number generation in Python and C++

When I was writing my evolutionary computing program in Python, I often wondered what would have been different if I had used C++ instead. Execution time would be faster, but development time would be slower. In this post, I’ll compare the performance of the two languages in a program I wrote that simulates 100 million coin flips.

I wrote the first version of the program using Python. Python is a good programming language to use for statistical simulation because it has a built-in random library which implements the Mersenne Twister. I executed this version of the program in both Python 2.7.2 and PyPy 1.7. PyPy is an alternative Python interpreter with improved performance. The Python code can be executed using the different interpreters without modification.

I wrote the second version of the program using C++. C++ has a simple random number generator, rand(), which should not be used for any serious statistical simulation. I used two libraries that implemented the Mersenne Twister. My first C++ version uses the GNU Scientific Library (GSL). I was helped by the GSL documentation and by the GSL example in this post. My second C++ version uses the new random number capabilities in C++11. I was helped by this example of uniform_real_distribution. The C++ code can be executed using the two different libraries by changing a few declarations and function calls.

Here are the results of running the code on my desktop computer:

Version Time
Python 23.9 s
PyPy 6.3 s
C++ using GSL 1.8 s
C++11 21.7 s

So assuming I’m not using C++11 incorrectly, it seems that it has really poor performance. There is a decent performance boost when using PyPy over native Python interpreter. But GSL does the best of all.

The files are available in this github repository.

Computer animation project

One of the fun results from my computer graphics class last semester was our computer animation project. The assignment was to model the firing of a cannon, including the cannon recoil and the projectile’s parabolic trajectory. The end goal was to use this model to drive a 3D animation.

I worked with 2 other group members, and in all I spent 54 hours in group meetings and coding individually. The project resulted in about 4000 lines of code in 34 Java files.[1]

Physics modeling

For each frame of our animation, we make slight changes to the position of each object on screen. These small changes are based on simple physics calculations.

For example, for a wheel to roll to the left, it needs to both rotate counter-clockwise and move to the left. The angle of rotation \Delta\theta is related to the shift to the left \Delta s by the following equation:

\Delta s=r\Delta\theta

where r is the radius of the wheel.

Firing the projectile gives the projectile some initial velocity. This velocity can be used to compute a change in position for the projectile. The velocity changes over time due to acceleration due to gravity.

2D animation

First, we produced a 2D animation of this model. This involved several steps:

  1. Read in 3 objects : the projectile, and the wheel and the barrel which make up the cannon. These objects can be drawn on-screen in a certain order so the layering appears correct
  2. Move them to their initial position.
  3. For each screen refresh, update the positions of the objects based on the equations.

Initially, the projectile has 0 velocity, so item #3 above really means that nothing will change in each screen refresh. As soon as the user presses the fire key, the projectile is given a fixed amount of velocity (and the wheel is given an amount of recoil velocity).

3D animation

Making the 3D animation was the more challenging part of the project. While the 2D animation was written in about 10 hours, the 3D animation took 44 hours to write, including a 13 hour rabbit trail. I spent about 13 hours working on a version that ended up not working.

The 3D animation extends the work of the 2D animation. Instead of a 2D wheel, barrel, and projectile, we now have 3D objects. These objects are first generated and then stored in .dat files. There are now 6 objects: the ground, 2 wheels, the axle, the barrel, and the projectile.

Based on the source code from our textbook, the 3D objects are put into a scene which can be viewed from different perspectives. We added code to rotate and move individual objects based on the amounts specified by our physics model.

Here is a demonstration:

The video demonstrates two methods of rendering that our 3D cannon animation supports. The first is z-buffering, and the second is hidden line elimination. These two methods are described in my first post about computer graphics class.


[1] This is a raw count, produced by doing wc -l *.java. Not all 4000 lines are original code for this project. Some of this code was first implemented in our textbook,  Computer Graphics for Java Programmers, 2nd ed., by Ammeraal and Zhang. There are also 6 Java programs for generating the data files for the various on-screen objects. These Java programs have a lot of code duplication.

What I learned from computer graphics class

Besides my course in evolutionary computing, which I learned a lot from, last semester I took a class in computer graphics. In this class, I learned about different aspects of computer graphics, including modeling and rendering objects to a screen.

I definitely appreciated how computer graphics class built upon knowledge that I had already gained in previous subject areas:

  • Geometry
  • Linear algebra
  • Java programming

Modeling 3D objects

We modeled 3D objects as collections of points in a 3D space. Each object has a number of faces, where a face is a collection of points (a polygon) that make up a face of that object. Each face must be flat. So, for example, a cylinder would be made up of a finite number of long thin faces.

Rendering 3D objects

A simple rendering is to draw a line around the edges of each face in the object. This would be called wire-frame rendering. A slightly more complex rendering is to ensure that lines are hidden if a face falls in front of that line. This is called hidden-line elimination. A cylinder is displayed here as rendered with hidden line elimination:

Cylinder displayed with hidden line elimination

A more realistic rendering gives a color to each face and fills it in. The face’s color can be shaded according to the direction the face makes with a light source. One simple method of rendering these shaded faces is called z-buffering. Here’s a screenshot of a z-buffer rendering of the same cylinder:

Cylinder drawn using z-buffer

What I learned from Evolutionary Computing

875 lines of code and 55 hours. Evolutionary Computing was one of the most challenging courses I have taken at SCSU. These statistics come from our final project, which was a month-long research project. Our assignment was to build on previous projects and produce a 5-page paper suitable for publication at a conference.

I learned a lot while completing this project. My project studied spanning trees on complete graphs, and how to evolve them using a genetic algorithm. Maybe in a follow-up post I’ll introduce these concepts. For now, I want to show what I learned while writing those 875 lines of Python code.

  • Write a testing function for non-trivial tests. I was checking something by hand, and thought my program wasn’t working. Then I ended up spending several hours debugging something that was not in fact a bug.
  • Dropbox is an automated version control. Several times I used it to retrieve an old version of a file that I had not committed. This came in very handy.
  • In vim, you can do :edit! to reload a file from disk. (documentation here)
  • Time estimating is important when running your jobs on other people’s machines.
  • I learned how to profile code using cProfile. On Ubuntu, cProfile can be found in package python-profiler.

Git

In git, you can choose to commit only certain changes in a file. It can be done by using git gui. The GUI basically shows you the output of git diff for the changed files in your repo, and you can right click on a line in the diff and choose “Stage Line For Commit.”

I only used this a few times, but it is a lot better than doing the following:

$ cp dandelion.tex dandelion.tex.bak
$ git checkout -- dandelion.tex
$ #manually copy over the changes that you do want to commit
$ git add dandelion.tex
$ git commit -m "This is the hard way"

LaTeX

When writing tables in LaTeX, you must put the label in the caption in order for it to refer to a specific table number. If you don’t, your ref will only refer to the number of the section. Here is a working code snippet:

The times can be seen in Table ref{wallclock}.

begin{table}
caption{Wall clock times in seconds for a single
run of the genetic algorithm using the PyPy interpreter.
 label{wallclock}
}
begin{tabular}{l r}
...
end{tabular}
end{table}

Careers for computer science students

I have often described to people the difference between computer science and information services (IS) majors. Both are offered at St. Cloud State University (SCSU), but they have much different purposes.

Computer science is a technical major, focused on data structures and algorithms. Computer science students take several math classes, as well as some science electives. IS is a business major, and it focuses on computer programming’s usefulness to business. IS majors do not have many math courses to take.

These different majors present different career options.

Computer scientist

First, there is a computer scientist. These workers “are the designers, creators, and inventors of new technology,” in other words, they do research. Most of them have a Ph.D. (In computer science, of course.)

Majoring in computer science at SCSU, I get a taste of some different topics that computer scientists study. For our undergraduate degree, we are required to take 5 upper-level electives. This semester, I am studying evolutionary computing and computer graphics. These classes present material that is fascinating to me, and we only scratch the surface of what a computer scientist would study. (For an example, see a description of evolutionary computing.)

Computer systems analysts

This summer I had an internship at a financial company. The work I did there would fall under the career of computer systems analysts. I would say a computer systems analyst could be anyone with problem solving skills, including both computer science and IS majors.

“Computer systems analysts use IT tools to help enterprises of all sizes achieve their goals. They may design and develop new computer systems by choosing and configuring hardware and software, or they may devise ways to apply existing systems’ resources to additional tasks.”

An example of a project a computer systems analyst would work on is to determine a good tool for doing version control on application software that the company maintains. The programming of the application itself falls to the computer programmers, but the configuration of the version control, as well as the build environment falls to the computer systems analysts.

Computer programmers

Then there are computer programmers. Programmers apply problem solving skills to the creation and improvement of software. Their work can be further divided into systems programming and applications programming. To be a computer programmer, you need sufficient programming experience, and a good way to get this experience is in the computer science or IS major.

Computer programmers can work in many different industries. Some industries are more technical than others. An obvious way to tell the difference is by the interview requirements. One of my fellow interns from the summer wrote this article about his interview experiences.

The financial company I worked for this summer has a very business-focused interview. Jon describes it well: “They want to know that you think logically enough to work things out so they can train you to do things their way.”

Other firms, like developer firms, have more technical interviews. These focus on coding ability. For example, they will have an interactive editing session with you and say, “write a stack.” This allows them to screen applicants who do not have sufficiently deep knowledge of programming.