Skip to main

AK#Notes

Notes from How I program in C by Eskil Steenberg

In the beginning you always want Results. In the end all you want is control.

This post is a collection of rules of programming that I made for myself by watching Eskil Steenberg talk "How I program in C"

I highly recommend you watch the talk before reading the my rules, and making your own rules instead of blindly copying other rules.

The talk consist of rules that Eskil applys to help him write better programs. Although Eskil made these rules for his C development, I'm going to use following rules for every language that I make. But some rules will be modify to better suit my language need. Because every language has it's own structure of doing things.

As Eskil said the goals of these rules is make our program code more maintainable and consistent. There are some other goals but these two I think are most important for good programmer.

Note some rules are my own and have no relation to anything that Eskil said.

Rules

Make technology footprint small

Simple is good

Clever is evil

Crashes are good.

Wide code is good code

Reuse names

Sequential Code is good

Function Naming

Function are better then objects

.c
Thing = ObjectCreate();
ObjectDoSomething(Thing);

// is better then

Thing = Object();
Thing.DoSomething();

Pointer

Structs

Memory

Don't recomute the data

.c
// Bad programming
typedef struct {
	float Width;
	float Length;
	float Area; // Bad
	// If you have width length then you can recomute the area.
	// If somebudy updateds the Width & Length but forgot to update Area then
	// error will happen.
	// Also calcuation on data is easeir then storing the data or access the 
	// data
} my_plane


// If you have to save area, because recomputing the are more expensive.
typedef void my_plain;

void  MyPlainWidthSet(my_plain *plain, float width);
float MyPlainWidthGet(my_plain *plain);
void  MyPlainWidthSet(my_plain *plain, float length);
float MyPlainLengthGet(my_plain *plain);
float MyPlainAreaGet(MyPlain *plain);

Fix code now

Squre root aproxmiation

.c
float DSqrt(float Number)
{
	int i;
	float x, y;
	x = number * 0.5;
	y = number;
	i = * (int *) &y;
	i = 0x5f3759df - (i >> 1);
	y = *(float *) &i;
	y = y * (1.5 - (x * y * y));
	y = y * (1.5 - (x * y * y));
	return number * y;
}

Random number genrator

.c
uint FRandom(uint32 Index)
{
	Index = (Index << 13) ^ Index;
	return ((Index * (Index * Index * 15731 + 789221) + 1376312589) & 0x7fffffff);
}
Table of Content