next up previous
Next: Plotting Up: AY575 class notes Previous: Presentation/communication

Subsections

Programming

overarching concepts

philosophy

From Quora, response to a question about value of formal training in programming vs. picking it up on your own:

Upvoted by Alagunarayanan Narayanan, Software Engineer " I was a self-taught programmer for about 4 years, before taking a few courses as extras in my EE degree. I am now pursuing a MSc in CS. So my answer is mostly observations on how I do things differently now compared to how I did them before.

Architecting projects is a big thing. Given a list of things the software is required to do, how would you lay out your program so that it -

  1. Does what it's supposed to do
  2. Is maintainable
  3. Is easily understandable for other people (and yourself in a few months)
  4. Is easily extensible if the requirements change (as they always do)
  5. Uses the most suitable design patterns to make the code intuitive

Usually self-taught programmers do 1., but don't pay nearly as much attention, or only pay lip service to the other points. I thought I knew about all those other points, but as I found out later, I really had no idea.

Another big difference is how much trial and error. I did a lot more trial and error back in the days, because I didn't have good understanding about how "the whole stack" works. I used a lot of things without bothering to truly understand how they work under the hood.

Nowadays I almost don't do any trial and error at all. I think through everything, and most things I actually start coding actually work on the first try (not counting typos, etc). I know exactly how memory management works all the way from kernel to malloc. I know how schedulers work (having written a few for a course), and I know what the synchronization mechanisms are, the pros and cons of each, the usual patterns in which they are used, as well as how they are actually implemented on the instruction level. As a self-taught programmer, I knew how to use mutexes and that's about it. As it turned out, I actually (poorly) reinvented a few of the other standard mechanisms.

Self-taught programmers are also usually not used to reading a lot of code written by other people, which is a very important skill when working in teams.

Knowledge of the existing algorithms is another one.

When a non-trivial problem is encountered, a bad programmer dives head first into coding a solution. A better programmer looks for solutions, and tries them out. A good programmer looks for solutions, analyzes them for time and space complexity as well as other constraints, and implements the most likely one.

Most self-taught programmers start coding too early.

And like you said, things like AI and ML. Most self-taught programmers never learn those things, because usually they only learn things they need, and if you don't know AI and ML, they won't seem like possible solutions to your problems, and you'll never think about learning them. It's a chicken and eggs problem. Self-taught programmers often don't know what they don't know. One nice thing about doing a degree is that it almost forcefully introduces you to everything, so by the time you are done, at least you know what you don't know."

languages

compiled vs. non-compiled. Distinction perhaps less clear now than it was in the past. But key point is that, for certain applications, you may want to consider execution time.

open-source vs. proprietary

Languages: commonly used in astronomy: Python, C, C++, Fortran, IDL, MATLAB. Many other languages exist: C++, C#, Java, Javscript, etc. etc.

Fortran: historical language of choice of scientific computing in mid/late 20th century. Many codes developed that are still in existence/development (e.g., Anatoly's N-body and Hydro codes, synthetic spectra generation MOOG and TURBOSPEC, stellar evolution MESA, Chris' codes, others...). In addition, many routines coded for fast operation available, e.g. LAPACK. Various versions of fortran: F66, F77, F90/F95

C: foundational language for computer science. Some astronomical routines: SAO WCSTOOLS for astronomical coordinate system routines, HSTPHOT/DOLPHOT for HST photometry, SLALIB for more astronomical coordinate routines, others....

Python: major push in current astronomical software development. astropy

IDL: historical very close connection to astronomy, with much development at LASP and Goddard Space Flight Center in the public domain. Marketed through RSI, then ITT, now Exelis, as a licensed software product. Originally written in C? Major astronomical software infrastructure exists in IDL, e.g. for planetary and solar astrophysics (perhaps because of NASA / GSFC). Astronomy users library. Major component of SDSS software, although modern evolution away from it.

Getting started

see programming reference table

Let's do a simple program in all languages.

Getting started with Fortran

Basic program structure:

  1. program statement
  2. variable declarations
  3. program statements
  4. end

Basic language:

Compiling and running programs: compiling, linking, makefile, BINDIR, path

Getting started with C

Basic program structure:

  1. #include files
  2. int main() {
  3. variable declarations
  4. program statements
  5. }

Comments: double-slash (//), or embed between /* and */

Compiling and running programs: compiling, linking, makefile, BINDIR, path

Introduction to makefiles

Introduction to makefiles (see, e.g., the GNU documentations or someone else's explanations. Explicit commands, variable filenames, rules.

Defining rules, e.g. Latex into PDF, etc Standard rules.

Standard targets: install, objs, etc.

Getting started with Python

Using command interpreter: python vs ipython (use ipython so you can access its features!)

Using programs: running interactively, running as a script, running as an executable, using %run with ipython

PATH and PYTHONPATH environment variables: executables are searched for in PATH, Python imports are searched for in PYTHONPATH and system-installed libraries.

Comments: hash (#).

Python useful references: Python tutorial python4astronomers

Getting started with IDL

are searched for in PATH, Python imports are searched for in PYTHONPATH and system-installed libraries. Using command interpreter.

Using programs: running a script using @, running a program using .run, idl -e

IDL_PATH

Comments: semicolon (;)

Simple makefile

Simple makefile (note that indents must be TABs):

hello_f: 
    f95 -c hello_f.f95
    f95 -o hello_f hello_o

hello_c:
    cc  -c hello_c.c
    cc  -o hello_c hello_c.o

run:
    hello_f
    hello_c
    hello.py
    idl -e ".run hello.py"


\begin{shaded}
\textit{
Understand how to write basic programs in Fortran, C, Py...
... basic Makefile for compiling and linking
Fortran and C programs.
}
\end{shaded}

Basic programming

see programming reference table for syntactical details in each language.

program construction

Variables


\begin{shaded}
\textit{
Understand the basic variable types and the difference b...
...e declared and those in which the type is
determined dynamically.
}
\end{shaded}

Operators


\begin{shaded}
\textit{
Understand basic operators and how to use them. Understa...
... Understand how vector operations can be used in
some languages.
}
\end{shaded}

Control statments


\begin{shaded}
\textit{
Understand and be very familiar with how to use control ...
...derstand the functionality of such statements in other languages.
}
\end{shaded}

Input and output (I/O)

Python4astronomers primer on reading and writing files


\begin{shaded}
\textit{
Understand and be very familiar with how to read and wri...
...nderstand the functionality of such functions in other languages.
}
\end{shaded}

Program organization: subroutines and functions


\begin{shaded}
\textit{
Understand and be very familiar with how to use function...
...ent languages: passing by value, reference, and object reference.
}
\end{shaded}


\begin{shaded}
\textit{
Understand how external routines can be incorporated int...
...rams.
Understand PYTHTONPATH and IDL\_PATH environment variables.
}
\end{shaded}

Objects and object oriented programming


\begin{shaded}
\textit{
Understand what objects/classes are and what object orie...
...on, in particular, using tab-completion in the iPython
interface.
}
\end{shaded}

Error handling and code testing

Debugging code

IDL: IDLDE

Python: pdb

C: ECLIPSE

Coding practices:


\begin{shaded}
\textit{
Understand the importance of code readability, including...
...ent this understanding in every peice of software
that you write!
}
\end{shaded}

More advanced coding

Case study /review


next up previous
Next: Plotting Up: AY575 class notes Previous: Presentation/communication
Jon Holtzman 2015-12-11