Viewing entries in
Programming

3 things every programmer should know

Comment

3 things every programmer should know

Many students obtain their degree and they don't know about the practice of programming. I consider three skills essential and unfortunately they are not always taught.

Step by step

When writing any piece of code, don't write everything and then start testing. You are heading for a long debugging session. Instead, you should write the smallest piece of code that improves the behavior and test that it did not break anything. Ideally you should run a full regression suite, but most people won't have this. At least you should run the code once.

Version control and diff tools

This step by step approach brings me to the next thing. Use version control! Version control allows you to document these incremental steps you made. And what is even better, it allows you to easily see what changes were made (use the diff tool) and where the bug might be.

Debugger

Finally, many starting developers do not know of debuggers. These are wonderful tools that can help you pinpoint the precise point a problem appears.

Summary

So, make sure that, by the time you start programming for a living, you work step by step, use version control and use the debugger.

Comment

C designated initializers

1 Comment

C designated initializers

Assigning values to a structure at declaration time was confusing because it was not clear what field from the structure was being initialized. Also, adding a new field to the structure could break existing code.

C99 fixed this with designated initializers:

brush: c
struct my_struct { int field1; int field2; };
struct my_struct s = { .field1 = 2, .field2 = 4 };

Fields can be given in any order and can be omitted as needed.

Arrays can also benefit from this to create clearer code:

brush: c
int unit_array[3][3] = {
  [0][0] = 1,
  [1][1] = 1,
  [2][2] = 1
};

1 Comment