Psychedelic GNU diary

Hack and bugs from the opensource and electronics.

11/6/09 03:54 am - Haxor's guide to perfect code

I'm trying to learn how to write readable and maintainable code for some time (but often have to deal with very broken one) and here is what I want to highlight for everybody whose code I might be reading in future.

Most of the people think that programming is a process where you sit in front of computer and type the code. For a most of the cases this is not true. There are sure situations when you sit and write, but most of the time, you need to think first. So the work starts a little bit earlier, usually when you are walking in the park, cooking dinner or taking a shower. When you are sitting in front of the computer you should at least have a general idea.

Do not create overcomplicated solutions. There are more and less complex problems to be solved, complex problems calls for complex solutions but still they should be clear and understandable. Make the code as simple as possible but not simpler.

Be consistent. The code you write should be consistent, especially when you are creating library interface. There is nothing more confusing that library with inconsistent interface. When you start using same naming convention, use it in whole project. For example when one data structure has method Delete, similar ones should name it same way.

Break complicated things into small parts. It's a good practice to build complicated functionality from small parts with well defined functionality. This is called encapsulation. This way you could think of parts of a code as they are black boxes that does something and the good part is that you don't need to focus on how it's done. However sometimes it's hard to find optimal cuts. In General you should optimize cuts so that your functions are not too long and have small number of parameters. Individual test for every black box is also good idea.

Avoid meaningless comments in code. Commenting code is good habit indeed but don't get overexcited, don't forget that you are writing code not poem. Also quite number of comments comes from silly named functions/variables. It's always better to name variable as iter_cnt instead of naming it g with comment /* this variable holds number of backwards iterations */. But beware names like my_little_iteration_counter, stay descriptive and short.

Found a dead end? Sometimes programmer finds out, in the middle of writing code, that one of the assumptions made on start is not correct anymore or wasn't correct at all, in that case don't be afraid to use delete key. But beware that delete is a powerful servant but a bad master.

When speed is not critical it's sometimes better to optimize for code readability and shortness than for speed. Typical example are initialization runtines that are called once at the start they could be a little bit slower, but on the other hand you should never write code that is way too slow if there is possibility to write it fast enough.

Okay that's all for today. May be more to come in the future.

11/5/09 01:46 am - lm2576 stepdown and PCB prototyping

I've finally had some time to write down some electronic howtos, see http://metan.ucw.cz/?page=electronics, more to come.

10/6/09 02:36 pm - Debuggin programs in C

Well, I've never used to use all the shiny and colorful debuggers. Instead of that using simple printf() was enough in most of the cases. However sometimes the code doesn't have stdout to print to, or stdout is too bloated with meaningless messages. For that purpose I've been using simple library that could redirect debug messages either to file or socket. Lately I've been asked by a friend of mine for that functionality. So I've put the code on the internet. Follow here to download the library. Note that this is just my internal library and is released as it is but at least it's a nice example how to use C99 macros with variable number of parameters and va_list.

9/14/09 06:51 pm - Widgets for C

Lately I've been trying to create quite simply application to test evfilter library. For that some drawing into single window with several text boxes is needed, however if you stick to plain C, there are just two possibilities. Either learn GTK+ with all that comes in the way or use SDL and SDL_gfx to draw lines and print text.

GTK+ GDK and friends are good when you need simple window with button, but is not that easy to create window to draw to. At least there is not too much documentation and the api looks bloated. But I've been told about devhelp and cairo now.

SDL is nice for graphics, but there are some issues too. It's currently limited to single window and SDL_gfx (that's needed when you are not willing to write functions for drawing lines yourself) has several problems. The most problematic one is that colors are not exchangeable with SDL itself. Just because SDL_gfx has fixed order of colors (RGBA) and SDL just uses the same order as SDL_Surface so on my pretty standard PC there are two different values for the same color accordingly to which library the function comes from. Also naming for functions is not consistent enough and there is no nice namespace as in SDL. Anyway you don't have all that widget functionality (like in GTK), however in such a simple case it's rather advantage than disadvantage.

9/14/09 06:35 pm - lftp now usable alternative to scp

Lftp is great command line software to handle ftp functionality. It can handle automatic retry, background jobs and supports ftp, http, ftps, https...

This week I've been told by a friend that lftp supports file exchange over ssh. This could be done either using fish or by sftp ssh based subsystem implemented in lftp. To try it just type lftp fish://user@machine or lftp sftp://user@machine and you will be prompted for password.
Tags:

9/7/09 02:55 pm - evfilter library

Good news everyone, evfilter library, that I was working on for a long time, has it's git repository and documentation.

Basically it's library that extends linux input intefrace on the userspace side and makes it easy to develop any application using input devices.

It also creates filter interface so that events from hardware can be modified/filtered accordingly to configuration.

For any further information follow: http://metan.ucw.cz/evfilter/

6/5/09 10:57 pm - pcb_utils on gedasymbols.org

My library for generating footprints for pcb is now officially on gedasymbols.org. Check it out!

1/3/09 01:26 am - pdb2txt

I've created a set of simple and clean utils for dumping data and content from pdb text document files. The tarball consists of the pdb-reading library and tools 'pdbdump' and 'pdb2txt'. Pdbdump allows you to see every part of the file or header in human readable format and it's also an example how to use the library. Pdb2txt is pdb to plaintext decompressor with a lot of sanity checks. Download GPLv2 code here pdb2txt.tar.bz2. The code is not finished yet but reading files works pretty well. Compression and ability to save changes are on my long term todo.

10/28/08 11:34 pm - crossdev -t avr

Okay here is my struggle with avr-gcc on gentoo. After reading several bugreports you should do:


  • Edit /usr/sbin/crossdev and change line 334 to WITH_DEF_HEADERS="no"

  • Select version of binutils and gcc (as default ones doesn't compile) for example crossdev -t avr --b 2.18.50.0.9 --g 4.1.2

  • Create symlink for linker scripts on x86_64 use ln -s /usr/x86_64-pc-linux-gnu/avr/lib/ldscripts/ /usr/libexec/gcc/avr/ldscripts

Tags: ,

10/14/08 04:06 pm - Kexec in debian armel

I need kexec to test new kernels (because flashing is too lazy) but there is no kexec-tools for debian armel. So I've had no choice but build it myself. I've taken debian sid source package and use apt-get to build it, however this fails because file kexec/arch/arm/kexec-zImage-arm.c is using deprecated include asm/page.h. Just changing this to unistd.h works and the compilation could be done after this change. To build this package you need to tweak kexec-tools-20080324/debian/control to include armel architecture.

Anyway package for sid armel is here.

See bug here.
Tags: ,
Powered by LiveJournal.com