HOW TO DEVELOP
IN AN EFFICIENT AND SAFE WAY



By TuO / SkyTecH
Article from Hugi #17
With permission of the author



Introduction


Many demo coders don't think to the way they are coding. They just consider the result. But now demos are being more complex, the source code is know really complex, and huge. This is why you should be careful when you write some code. It is true for all languages, C, ASM, Pascal, Cobol, or everything you can think about.


Of course there's not a "miracle way" to write understandable code. There's just some tricks it's good to know when you're a brand new coder. This article deals with a second problem, which is really close to the first (even if it is not obvious) : the source safety. What do I mean? Just one simple thing : never hang computer. There's simple tricks to avoid hanging computer, which means reboot, and more time wasted.



What's that fuck? I can read my sources!


Yeah, while you are updating a part of you new mega powerful top speed extra funny 3D engine, you can remember all subtilities of the sources. But what after 1, 2, 6 months? If your code is not properly written, it's just hours wasted trying to understand why the hell you're doing this here instead of there!


What you should keep in mind when you write some code, it's that it won't be easy to read it if there's no explanation at all. There's two ways to make code understandable :
- use self-explained variable names
- comment the code




Hey my progs never hangs (or not often)!


Hum there's a lot of programs or demos that hang for a totally unknown reason. In fact, there's often a simple explanation to give. And some simple rulez could make it easier not to hang (or hang really less).


One important thing to do is to check arguments in a function, and react accordingly. If you expect a pointer from the user (even if the user is you!), then be sure to check that pointer is not NULL. Maybe it is a non valid pointer, but hey then you can't do anything. If you expect a value from the user that must be in a given range, be sure to check the value against this range, or you can have problems. (tip : often ranges are 256 or 65536, so use BYTES or WORDS values to store them, and you'll be sure the value is in the range).


If you are sure the values you are working with are correct, there's no more reason to hang, except if you are doing something really stupid :)


Another thing : never forget to handle possible errors. An unhandled error can cause a crash later in the program, and it is often hard to find the real reasons of this kind of crash.


And finally, try to always reset variables when you are allocating them, or freeing them. The best should be to reset a variable you used to its former state to avoid all possible hang later, but it is often not useful.



Ending


I personnaly apply these rules (the "never hang" rule since not so much time :) and I think it's great. I'm working on my new 3D engine since 6 month now, and even after 5 month I didn't read my assembly sources for point transformation, I still understand it because of all comments I put in (note that it is a full ASM FPU function, with a lot of stack operations, so it could be a bit confused if not commented). And I made a 3DSmax plugin with this 3D engine. I hanged my computer only once, and it was just because I made a stupid mistake! Not all my 3D engine is secured yet, and this is why I have some problems. But when I experiment problems, I add security where the engine crashed, it allow me to found the problem, and everything goes well!


I really hope this piece of text will help beginners to learn faster the marvelous world of programmation, and that all coders that had problems with misterious hangs will apply these rules and finally find all their bugs!


TuO / SkyTecH




Edito Rubriques habituelles Planè Atari Internet Techniques et programmation Interviews Jeux Vidéo Humour Musique Chroniques, etc. Divers