Which Programming Language Should I use?

 

Popularity:

In astronomy, here is my subjective league table of language popularity:

Rank

Language

1

Fortran

2

C & variants

3

Perl

4

IDL

5

Python

6

Java

 

Fortran and C are used for high performance applications. Most code more than 10-15 years old (in its initial form) will be in Fortran. IDL is used for data analysis and graphics. Perl is used for scripts and short programs. Python seems to be gaining popularity fast, for data analysis and virtual observatory work. Java is used for user interfaces (such as the Gemini observing tool). C is used for real-time telescope and instrument control.

 

In the real world, there have been many attempts to gauge the popularity of different languages, but most modern league-tables look something like this:

 

Rank

Language

1

C & variants

2

Java

3

Perl

4

Visual Basic

5

Python

6

Pascal/Delphi

7

COBOL

8

Ruby

9

Tcl/Tk

10

Fortran

 

Advantages of a language that is popular in the real world are the existence of web resources such as tutorials, a wide range of libraries of existing code you can use and modify, and better job prospects for you if you ever want to venture into the real world.

Easy to Write

There have been various studies of how many lines of code are needed to write a given program in different languages. Languages seem to fall into two camps:

Most studies find that ~ 5 times more lines of code are needed in low level languages to perform a given task. Given that the number of bugs scales as a fairly high power of the number of lines of code, this can be a HUGE advantage.

 

Fast to Run

Once again you can break languages up into two groups: fast and slow.

Most studies find that the fast languages are 3 – 100 times faster, depending on what you are trying to do. So donŐt try to simulate a billion stars in PerlÉ

 

Availability of Libraries

There are immense amounts of existing code out there that you can borrow and re-use in your own programs. This generally will save you months of effort – if you are not using these libraries regularly, you are shooting yourself in the foot.

 

Numerical libraries (solving equations, matrix manipulations, random number generation).

Interfacing with Astronomy Data:

Data reduction:

Text/Web handling:

Virtual Observatory:

Plotting:

 

Conclusion:

If speed of execution is crucial (eg n-body, hydrodynamics, anything on a supercomputer):

Use C or Fortran. Probably C would be the language of choice unless you are building on existing Fortran code. This is because (a) there are more libraries available, and (b) it might get you a job outside of astronomy at some future time.

A common variant is to write most of the code in Perl or Python, but write the performance-critical bits of code in C or Fortran. They can easily be called from Perl or Python.

 

If write-time is much greater than run time.

Most programs take days or weeks to write and debug, but only seconds to run. Perl or Python are best for these.

 

Scripting.

Scripting is writing a program whose main purpose is to run other programs repeatedly. Such as taking lots of data files, sorting them out and processing them. Use Perl or Python for this. An alternative is a UNIX script, or Tcl/Tk.

 

Very big code.

If your program will have more than ~ 100,000 lines of code, and is being developed by a team of 10+ programmers, JAVA is apparently the code to use, as itŐs designed to avoid common problems that beset this sort of mega-project. C# would be a (Microsoft-only) alternative.

Interactive Data Analysis.

IDL or Python. IDL has more pre-written code available, but is expensive so not everyone may be able to run your code. Python is probably the nicer language and is free, but is harder to install.