Tuesday, April 12, 2011

ASSEMBLED: Why don't we get custom cars?

With so many innovations happening around us, especially in the field of computer sciences, why is it that we still have no clear and complete knowledge of the things we use everyday. Take for example, the Car. How many of us actually know what composes a car and what to do when something goes wrong?!

Despite the fact that we have so many things around us about which we do not have a complete understanding, I am going to concentrate on the cars. We use our cars and bikes everyday! At least, back here in India, one in 2 houses has a car or a bike. Yet only 1 in 1000 people know about what a car is made of. Fewer still, only 1 in 100000 really know what each internal part of a car does. When it comes to fixing a car, the ratio is much much smaller. I am not providing an exact ratio or even an approximate ratio but you would understand yourself that the ratio that I have chosen is not too absurd either.

What is the reason for so much disparity?! Well, I believe that this is because there are still many domains about which we do not share information! Look at the Internet, look at the domain of computer sciences! Do you ever feel the need to buy books on programming languages or database systems?!

No.

Why!?

Cos most of the information and tutorial that you need is available on the Internet. The literature on Computer Sciences has been made available anywhere and everywhere. So much so that assembled desktops are now the most sought after product and most desktop owners can now assemble their own desktops. Not just assemble, but also customize; both in looks and performance.

Its not just the Computer Sciences domain that has easily accessible information. There are many such domains. A few that I know of are

The Polymath Project: a massively collaborative environment for the study and research of sciences, especially that of mathematics.
(http://polymathprojects.org/)

The Human Genome project: a project with completely open data access and with legal backing which states that if you have to work on the human genome, you have to agree to abide by certain principles. Principles that state that whatever find you make, must be available in the public domain. And so, GenBank was born.

When such domains can exist, can a domain exist for the study and research of the automotive technology? We do have a forum where discussions of many sorts happen on cars and other automobiles: TeamBHP. But why is there an absence of a portal to learn about your beautiful car or your not-so-beautiful car. A Wikipedia kind of domain completely dedicated to the technology of automobiles so that you will be equipped to learn and control and custom your ride. Do throw in your opinion.

Monday, January 24, 2011

Public or Private or Protected: The trouble with access specifiers!

The object-oriented paradigm introduces a new concept towards accessing variables using, what are called, access specifiers. All of you who had been introduced to the OOP (Object Oriented Programming) paradigm, would definitely have heard and used these access specifiers. There are, in general, 3 levels of access specifiers in OO languages: public, protected and private.

Many new learners of the object-oriented paradigm often get confused by the purpose of these access specifiers, especially when it comes to assigning access specifiers to variables. Most literature on this explain the existence of these access specifiers as providing secure access to these variables. But what kind of security are they talking about?!! They simply use the abstract term security without ever mentioning why this security is required or how this security is helpful.

Here I try to explain one of those cases where the use of these access specifiers is very handy and definitely provides, what is called, security. Please leave a comment if you know of any other scenarios where access specifiers are helpful.

Say I have a class called Person, which has a variable that stores a value for the age of a person
class Person {
    .........................
    unsigned int age;
    .........................
    .........................
};
Now there are 2 ways to access this variable, age. One is using the dot(.) notation and the other is via some functions (called the getter/setter functions).

Case 1: If I happen to declare this variable with public scope, then I can access this variable directly using the dot(.) notation. Let assume that in my application I create an object of this class and I name it obj. Then I can read the age of a person into another vairable var by a simple statement:
var = obj.age;
and I can set the age to a new value using the statement:
obj.age = 20;

Case 2: If I happen to declare this variable with private or protected scope, then I cannot access this variable in the fashion show above. I would have to use member functions of the Person class to read or write values to this age variable. These functions are called getter/setter functions in the OOP paradigm.
Lets say, I have declared 2 such functions:
/* The setter function for the age member variable */
public void setAge(unsigned int age) {
    this.age = age;
}
/* The getter function for the age member variable */
public unsigned int getAge() {
    return (this.age);
}
Then I can read the age of a person into another vairable var by using the statement:
var = obj.getAge();
and I can set the age to a new value using the statement:
obj.setAge(20);

Now that these 2 cases have been established, its time to decide when to use either of these cases. This is often a confusion among beginners, who generally always stick to one of the access specifiers without giving a thought to the logic and the different scenarios that their logic would have to deal with.

Here I am going to discuss one of the scenarios, in short.

Consider a system which maintains a record of everything you have done so far since the minute of your birth (Scary isn't it!? :) and in such a system, while entering some information, lets say that the age has to be specified and the form does not have validation code programmed into it. Then, if I accidentally enter 1000 (which is unthinkable right now except if you are HanCOCK), this method would set the age to 1000 without any fuss in both the cases (described above). But this is wrong! We need to validate the value for age before it is saved but there is also a restriction that this validation cannot be written in the form code. So what do you do!?

This is where the implementation of Case 2 comes into play. Here you can program the validations in the setAge(unsigned int age) function just before saving the value in the variable. So now our setAge(unsigned int age) function would look more or less like:
public void setAge(unsigned int age) {
    if (age <= 100) {
    // Considering the biological maximum age of humans
        this.age = age;
    }
}
This is just a simple validation. More complex validations can be added depending on the logic that is to be implemented. The advantage of programming the validation within the setter function is that we can club together the validations and the operation (in this case its an assignment) together without having to touch each and every piece of code where ever this assignment is made.

But this explanation should not serve as a support for tagging all variables under the private or protected access. Always remember that an assignment via a function call is always slower than a direct assignment. Use the dot(.) notation access (otherwise called public scope access) when the variable does not control any critical logic. For example, a system where a variable is required to store the latest warning or error message, among a series of messages just before the exception is thrown, can very well have the variable, that stores this error message, with public access and used with the dot notation.

And a very important thing is that these access specifiers are applicable only to variables within a class!!!