My own programming style convention for most languages

I write code mainly in R, and from times to times, in C, C++, SAS, bash, python, and perl. There are style guides out there that help make your code more consistent and readable to yourself and others. Here is a style guide for C++, and here is Google’s style guide for R and here is Hadley Wickam’s guide for R. For R, I agree more with Google’s style guide than Hadley Wickam’s because I absolutely hate typing the underscore (personal preference) and because Google’s style guide seems more related to that of the C++’s guide. Style guides differ by languages because the languages are different (restrictions on names, etc.).

My brain goes crazy if I have to remember and apply multiple styles, so I want to use a convention that I can use consistently for all languages. This boils down to refraining from using special characters such as “-“, “.”, and “_” in names as these characters can have special meaning in different languages. Here it goes:

  • constants: the letter k or n followed by a description. For example, kConstant or nSim.
  • variable names: description words where the first word is lower case and every subsequent words are lower case but with an upper case first letter. For example, varBeta1 for variance of beta1.
  • function names: a verb followed by description words, with each word capitalized except. For example, savePlot and computeGRhoGamma. This is the same as the variable names. I originally was going to follow the Google R style guide instead of the C++ style guide, but opted for the latter because the reasoning made sense: the distinction is obvious based on syntax when a function is called (has parentheses and possibly arguments).
  • function arguments: depending on the type of argument, use the variable names or function names style for each. To help distinguish an argument that takes in a function, the argument should begin with f if it takes in a function. For example, drop=TRUE and fSummarize=summarizeDefault.

When breaking these conventions lead to a better understanding of the code (easier on the brain), I will not hesitate to break them. For example, using i, j, k as iterator variables, using na.rm as functional argument in R, or rKM for a function that draw random numbers from a Kaplan-Meier survival curve.

Now, if only I can just magically transform all of my own code into this convention. I’m going to really hate going back to old code that doesn’t follow my own style, especially when they refer to code in packages that I will update according to my new convention.

Dissociate C-i, C-m, and C-[ in emacs

Every now and then, I bind functions to keys such as C-m, C-i, and C-[ , but run into issues as these keys are identical to RETURN, TAB, and ESC. See this and the named ascii list for more information. I tried many solutions before but never got them to work. I finally ran across a solution that works, namely Caio’s solution on this post. At the time of this writing, it shows as the last place answer on stacked overflow, so I (and many others) must have missed it. Note that this works only on GUI instances of Emacs and not terminal ones. I will reproduce the solution:

 <pre class="src src-sh">;; Translate the problematic keys to the <span style="color: #00ffff;">function</span> <span style="color: #87cefa;">key</span> Hyper:

(keyboard-translate ?C-i ?H-i) (keyboard-translate ?C-m ?H-m) ;; Rebind then accordantly: (global-set-key [?H-m] ‘delete-backward-char) (global-set-key [?H-i] ‘iswitchb-buffer)

Basically, translate the keys using the hyper, and define the new keybindings using the hyper key. For my use, I did

 <pre class="src src-sh">(global-set-key (kbd <span style="color: #ffa07a;">"C-]"</span>) <span style="color: #ffa07a;">'elscreen-next)</span>

(keyboard-translate ?C-[ ?H-[) (global-set-key (kbd “H-[“) ‘elscreen-previous)


flags for compiling source code: pkg-config command

so i was trying to compile some C code on my server at school that involves the math and the GNU Scientific Library (gsl) libraries. that is, the source has the following lines:

 <pre class="src src-C"><span style="color: #b0c4de;">#include</span> <span style="color: #ffa07a;">&lt;stdio.h&gt;</span>

#include <math.h> #include <gsl/gsl_integration.h>

those link the header files. however, when compiling, gcc would complain that they can’t see functions such as sqrt, abs, etc, from the math library. it also complains about my call to a function from the gsl library. i googled the math library stuff and realized i need to pass the ‘-lm’ argument in gcc for the math library. similarly, i saw somewhere that refers to the ‘-gsl’ argument. however, this did not work for me. i tried many things. some more googling, i found this thread that shows the ‘pkg-config’ command that shows exactly what arguments u need for which library.

So, i get:

 <pre class="src src-sh">$ pkg-config --libs gsl

-lgsl -lgslcblas -lm

so i just need to execute:

 <pre class="src src-sh">gcc -lgsl -lgslcblas -lm myprogram.c

i really don’t understand how the GNU compilers work. on my mac, i don’t have to pass those arguments. on my debian server, i need to. at first i thought that including the header was enough. will need to learn more about this stuff.

PS I also remember using this command when i had a hard time compiling shell-fm on my mac os x.

UPDATE: so on my mac os x, after installing gsl, i need to use ‘gcc -lgsl myprog.c’ to use the gsl library. things are fine now. I also want to note that i could not compile GSL form source on my debian server…had to rely on apt-get.