Saturday, December 31, 2011

Books I read in 2011


These are the books that I read this year (in case I read a translation the original title is after the translated one and between parentheses):

February
- The Pragmatic Programmer: From Journeyman to Master, Andrew Hunt & David Thomas
- The Hobbit, J. R. R. Tolkien (third time)

March
- Sombras quemadas (Burnt Shadows), Kamila Shamsie
- El coronel no tiene quien le escriba, Gabriel García Márquez

April
- En el camino (On the Road), Jack Kerouac
- Sostiene Pereira (Una testimonianza), Antonio Tabucchi (second time)
- Focus, Leo Babauta

May
- Juego de Tronos (A Game of Thrones), George R. R. Martin
- Choque de reyes (A Clash of Kings), George R. R. Martin
- Storm of swords, George R. R. Martin
- Apprenticeship Patterns: Guidance for the Aspiring Software Craftsman, Dave Hoover, Adewale Oshineye

June
- A feast for crows, George R. R. Martin
- Object Oriented JavaScript, Stoyan Stefanov

August
- A dance with dragons, George R. R. Martin

September
- Clean Code, R. C. Martin

October
- Test Driven Development: By Example, Kent Beck

December
- Estructura de la informació. UOC.
- Estructura i tecnologia de computadors. UOC.

Example of using variadic functions in C

This is the translation into English of an old post.

In my master's thesis I used variadic functions (functions that can accept a variable number of arguments) to be able to pass different differential equations systems to the function in charge of computing Lyapunov exponents.

Since that code is too big to fit in here, I'll post the tiny tests that I did to understand how C variadic functions work.

You can find a great explanation of C variadic functions in a section of apendix A of The GNU C Library manual. In this manual there is an example in which an undetermined number of integers is passed to a function that adds them.

This is the example where I've highlighted and commented the most important lines:
#include 
#include 

int sum (int count,...){
    va_list ap; // List of arguments
    int i, sum;

    /* Initializes the list of arguments */
    va_start (ap, count);         

    sum = 0;
    for (i = 0; i < count; i++){
        /* Obtains the next argument */
        sum += va_arg (ap, int);    
    }
    
    /* Frees the list */
    va_end (ap);       
    
    return sum;
}

int main(void){
    /* This prints 16. */
    printf ("%d\n", sum (3, 5, 5, 6));

    /* This prints 19. */
    printf ("%d\n", sum (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    
    return (0);
}

Several important things regarding this example must be remarked:
  • To work with an undetermined number of arguments we must include the header stdarg.h of the C Standard Library.
  • In the function's list of arguments three dots are used to signal the start of the undetermined arguments.
  • A variable of type va_list must be declared. This variable will contain the arguments list.
  • The macro va_start is used to initialize the arguments list. We must tell the macro which is the last determined argument.
  • The macro va_arg is used tp extract the arguments from the list. We must tell the macro the type of the extracted argument. 
  • The macro va_end is used to free the memory.  
This is the result of executing the previous example:
$ ./test1 
16
55

In the next example, I used the same procedure to pass an undetermined number of arguments. In this case, though, the type of the argument is a user defined type:
#include <stdarg.h> 
#include <stdio.h>

typedef struct s_Date{
    unsigned int year;
    unsigned int month;
    unsigned int day;
}t_Date;

void setDate(unsigned int day, unsigned int month, unsigned int year, 
             t_Date *date);
void printDate(t_Date date);

/* Prints several dates */
void printDates(unsigned int numberOfDates, ...){
    va_list ap;
    int i;

    /* Initializes the list of arguments */
    va_start(ap, numberOfDates);         

    for(i = 0; i < numberOfDates; i++){
        // Extracts next argument from the list
        printDate(va_arg (ap, t_Date));
    }
    
    printf("\n");
    
    // Frees the memory
    va_end(ap);          
    
    return;
}

int main(void){
    t_Date date1, date2, date3;
    
    setDate(1,1,2009, &date1);
    setDate(2,2,2009, &date2);
    setDate(3,3,2009, &date3);
    
    /* This prints 1/1/2009 */
    printDates(1, date1);

    /* This prints  1/1/2009 2/2/2009*/
    printDates(2, date1, date2);
    
    /* This prints 1/1/2009 2/2/2009 3/3/2009*/
    printDates(3, date1, date2, date3);

    return (0);
}

/* Initializes a t_Date variable*/
void setDate(unsigned int day, unsigned int month, unsigned int year, 
             t_Date *date){
    date->day = day;
    date->month = month;
    date->year = year;
}

/*Prints a date*/
void printDate(t_Date date){
    printf("%d/%d/%d ", date.day, date.month, date.year);
    return;
}
This is the output:
$ ./test2 
1/1/2009 
1/1/2009 2/2/2009 
1/1/2009 2/2/2009 3/3/2009 

Friday, December 30, 2011

Interesting talk: "Brownfield to green"

I found this talk very interesting:
It's about the re-engineering of a legacy system using some Agile techniques.
The speaker is Nat Pryce one of the authors of "Growing Object-Oriented Software, Guided by Tests" (which is in my reading queue).