2006-09-11

The Software Development Process

Here are a few quotes I found out there that I thought make sense:

Quote 1:
The game says there is a "process" that takes "inputs", generates "subtasks", and "schedules", which go to "motivated programmers" that "follow the clear directions", and use "laest tools" to generate "quality code" that can be "integrated" and "tested" and "shipped".

Quote 2:
Whatever you do, choose a process with rapid prototyping and testing that also allows for the specification to be changed because the majority of the bugs will be in there.

Quote 3:
People may write requirements in abundance but they do not write what they need the system to do and which functionality they prefer over the vast list "essential" features! That part is discovered during the development. If your process is cheap, you can work your way out of it - if it is "formal" you will die under a mountain of papers, inspection records, progress meetings and what not.

Quote 4:
Program bottom-up until it works. Then go top-down and clean it up.
Most people just skip that last step.

Quote 5:
0. Plan the code
1. Write code
2. Read code
3. Test code
4. Release
but most people skip 2, sometimes 3. Also, if you program bottom-up, 0 is a continuous process.

Quote 6:
The fastest way to get it working is to slow down and do it right the first time.

2006-03-06

C++ | 'Good Classes' should have virtual destructors

I know this sounds like "Good boys should behave BLA BLA BLA" but heres why :)

When you write a class, make sure you write a virtual destructor for it. Also, when you want to subclass an existing class, make sure the parent class has a virtual destructor(s).

Pointers and references to the base class object can actually point to a derived class object. If the derived class object is deleted using the base class pointer and the destructor of the base class is virtual, the destructor chain (through all subclasses) will be called. Therefore, one should publicly inherit only from classes with virtual destructors.

Heres a bad example :

class Base {
public: ~Base() { // non virtual
// ...
}
};

class Derived: public Base { // yikes ! parent has a non virtual destructor
public: ~Derived() {
// ...
}
};

int main() {
Base * p = new Derived; //seems OK
delete p; //trouble, Derived's destructor not called
}

So be sure to double check this recommendation everytime you write a new class or inherit from an existing one.

2006-02-06

C++ | What are Smart Pointers Anyway ?

Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time.

Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed.

Smart Pointers (currently Boost Smart Pointers), are expected to make their way to the C++ Standard. You can find all relevant details at http://boost.org/libs/smart_ptr/smart_ptr.htm

2006-01-31

Python | Declaring 'Class' and 'Instance' variables

How should one declare 'class' and 'instance' variables in Python ?

Well, in Python, assignment is declaration. You just need to assign the variables in the appropriate place.

If you intend to declare a 'class' variable, that is, a variable stored for the class and accessible through each instance, you code 'name = value' for that variable inside the class statement.

If you intend to declare a separate attribute for each instance, you code 'name = value' for that variable it in the '__init__' method.

2006-01-24

Python | Socket Starter

I dont know why, but almost every programmer who learns a new language want's to program sockets right away. If you are one of these species, and you've just learnt python, here's how it can be done. Very simple really.

import sockets
session = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
session.connect((ipaddress, port))

thats it !

Of course you will need to understand more than that. So here's a URL for you.
http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf

2006-01-23

C | C++ | Python | Control Rounding when converting Floats to Integers

I have faced this problem in the past, but had not thought of a solution yet. Was chatting with a friend lately, and he suggested a very simple idea.

The problem is that when converting Floats to Integers, they always round down. But you want them to round up sometimes, for whatever reason. You could do this :

Integer = Float + 0.5f

This will add .5 to the float number, and it will round down to either trunc (float) or trunc (float + 1).

Thus, 1.1 rounds to 1, and 1.7 to 2. You get the idea.

2006-01-20

C++ | Be careful when writing assignment operators

If you are writing an assignment operator for a class, be very careful and ensure each of the following :

- that you have copied all the attributes
- that you have allocated new memory for each non-stack attribute (i.e. pointer variable) before copying it.

The second one is the one where most mistakes are made. One tends to copy the address pointed to by the source variable into the destination variable, which is perfectly legal but rarely intended. And then somewhere during runtime, the address in the source copy is freed and later, the address in the destination copy is accessed. And then guess what happens !