Coding and the Interwebs

If there is one thing you should know about learning to code, it is that the internet is your friend. Learning how to find what you need on the internet is an important skill to learn. A lot of the skill comes with time, but there are a few things that can get you going.

  1. Someone has had the same problem you are having. There is an answer.
  2. Stack Overflow and Stack Exchange are miracles of the internet. Don't worry which is which unless you want to
  3. Occam's Razor - Try the simplest solution first, but avoid hackey solutions.

How to ask a good question*


A good format to follow for asking Google a question is

language what you want to do

Say I am working on some C++ code, and I want to read in columns from a file. Suppose these have multiple data types, some are strings while others are integers. Example:
    Alice  1  10  1e3
    Bob    2  12  2e3

I would ask C++ how do I read in columns with multiple data types from a file. While this is doable, a Google search shows it's a rather difficult topic. The best solution I could find was cplusplus.com, which is a well known site, but if you read the thread it ends without any clear solution.

Pressing on


I could give up, but instead I simplify my search to c++ read in columns from a file. I get a result from StackOverflow and from cplusplus. The cplusplus result is only for 2 columns but it looks useful. The stack overflow result is much better. Not only is it reading multiple columns, each column has a different data type. Looking carefully, one sees that the second part of the answer replaces the ellipsis (...) in the first part.



Also note the arrows and the checkmark. On the Stack websites, when an answer is accepted it means it was the right answer for the question asker. The number indicates how many upvotes an answer got. More upvotes is generally a better answer, though sometimes a less great answer is fine for what you need.

The key now is to examine the answer and learn what you can from it. Learn what the functions they use are. If there is a link, follow it and read the documentation. If you are using C not C++, check to see if it works in C by running a short piece of code to check. If it doesn't work, don't start over, search for equivalencies in other languages.

When all else fails


Sometimes the answer isn't out there, or is just really hard to find. When this happens check other languages. Sometimes the easiest thing is to just find a work-around. Most programming languages have a way to make system calls and pipe the output back into your code. Take advantage of these.

Good sites and useful things


Some good places to find information are:

  • Stack Exchange
  • Stack Overflow
  • cplusplus.com
  • GitHub
  • Documentation for the language.

Hints that can make life easier:


  • On osx/unix/linux the terminal is a powerful tool. Take advantage of bash/csh. OSX's terminal defaults to bash. Check with echo $SHELL
  • astropy.io.ascii is great at reading in data
  • numpy.loadtxt allows you to simply read in numerical data, you can even select which columns
  • Pandas is huge in the python community. I don't know it, but it's popular
  • Mathematica is amazing in its versatility. It can parse data and do crazy stuff. More in a later post.
  • Comment your code.
  • If you write a useful function. Copy it to its own file so you can load it in other programs.
  • Write reusable code. Keep your programming as general as possible.

This is what I can think of for now...



*I didn't cheat, I actually did the search myself, which is why the first question fails

Comments

Popular posts from this blog

A Quick Look at Spherical Coordinates

Astrobites