2 ms·
I did that just yesterday - I needed to modify some code which I wrote in 2005. I could write decent code back then, it worked, it was mostly easy to follow. It
by viraptor 13y ago
I did that just yesterday - I needed to modify some code which I wrote in 2005. I could write decent code back then, it worked, it was mostly easy to follow. It was very bad compared to what I'd write these days though... But I learned a lot things from it that I need to keep in mind in the future. Today, I could probably criticise every single line of it...
So - here are my lessons - maybe someone will find them useful (keep in mind this is C code for attiny chip - it's really minimal stuff):
signed short int P[P_NUM]={0}; // real points
unsigned short int A[P_NUM]={0}; // meas points
Never name global variables with single letters. Even these are "the" global variables everything else in the code uses. Also comments do not have character limit - if it's "measured points", spell it out. It's useful for grepping.
unsigned char last=0; // last point+1
I have no idea what +1 means at this point, since it starts at 0. Was the last point -1? I don't think so - again, always add details to comments.
if(lcd_buff[j]&_BV(i))
PORT_LED|=_BV(PIN_AB);
else
PORT_LED&=~(_BV(PIN_AB));
PORT_LED|=_BV(PIN_CLK);
PORT_LED&=~(_BV(PIN_CLK));
Horizontal whitespace matters - code above is barely readable. Defines for constants are good, but could be more descriptive than "PIN_AB". At least PIN_CLK is self-explanatory.
eeprom_busy_wait(); eeprom_write_byte((uint8_t *) 0x00+1, last);
eeprom_busy_wait(); eeprom_write_word((uint16_t *)(2+setup_pos*4), A[setup_pos]);
eeprom_busy_wait(); eeprom_write_word((uint16_t *)(4+setup_pos*4), P[setup_pos]);
If some function repeats so often that you end up prepending it before "actual work", you're probably missing a level of abstraction.
a*=2;
a/=dif;
if(a&1)
a++;
a/=2;
Even if some code is specific to that project and crucial to the way it works - comment on "how" and "why". I guess it tries to round the division... but it could be a lot clearer.
//#define CORRECT(a,b) ((A[b]-A[a])/(P[b]-P[a]))
//#define CORRECT2(a,b) ((A[b]-A[a])/(P[b]-P[a])>>1)
Never comment out unneeded code. Delete it and let the version control keep the old things. Now I'm not sure if that was commented out for testing, or was it never needed...
Also found out that any in-line state machine will grow until it fills the memory (not hard on attiny ;) ). If you don't design proper macros to construct it, it will become a single function with lots and lots of copy-paste code. It doesn't matter that it was clear at the start and only had 4 states that fit on the screen. Code grows on it's own :)
Also I found a CVS folder in that project. There's one issue with it - I only have the client part - so I can't see any history anymore. DVCSs are a really cool idea.
And for the end -> writing code for a very small chip doesn't mean you have to make the code itself really short, but somehow it seems I was trying to do that. It's the resulting binary that is supposed to be small. Keep in mind how separated are you from the end-result.