Computers, Science, Technology, Xen Virtualization, Hosting, Photography, The Internet, Geekdom And More

Why I reverse arguments

Posted on | October 16, 2008 | No Comments

I was asked an interesting question about my “C style” by someone who was reviewing my code this morning. Since I’ve been asked the same question on several occasions, I’ll explain in a post.

When testing stuff, its easier for me to see what I’m testing for first, rather than what is being tested. When I read my native spoken language (English), I read from left to right. For instance:

if (NULL == foo) {

A lot of people would simply write:

if (foo == NULL) {

Its easy for me to see that I’m testing whatever comes after == for NULL, this helps speed up debugging (for me, anyway). The above is trivial, take the next one as example #2:

if (NULL == (fp = fopen(foo.txt, "r")))

So now I’m testing the results of an assignment. Reading left to right, its easier for me to read. Finally, example #3:

if ((NULL == (tmp = strdup(foo))) || (-1 == (some_func(foo, FLAG))))

… its just easier for me to read. Interestingly, people who read their native written language from right to left also reverse arguments, but for the completely opposite purpose that I do.

If I’m working on something existing, I just follow the existing style. If I’m working on something from scratch, I generally reverse arguments. This leads to my point (yes, I have one), when putting work out under contract .. ensure you include some guides about coding style. This avoids having someone like me go through a huge tree and re-reverse everything :)


Leave a Reply

  • Monkey Plus Typewriter
  • Stack Overflow

  • Me According To Ohloh

  • Meta