dimanche 18 décembre 2011

Getting started with the javadoc

If you are using eclipse, working with javadoc is pretty easy. It's not that complicated using the command line either, personally I'm an addict to the command line, but I didn't bother too much with generating the javadoc using it since I tried already and I failed miserably. I had a problem when trying to set the classpath, since there was a lot of dependencies to include. I should have written an ant script, but I was too lazy. All programmers are :). So, I just chose the easy way for once, selecting project > generate javadoc... You have then to go through the usual straightforward process, choosing some options and then next, next.

java heap space configuration


And don't forget to configure the JVM heap space, especially when you have a big project with a lot of comments in it. This way, the javadoc will be generated a lot faster, and you won't be at the risk of having an error with heap space.
My advice is, don't forget to document well you code. A good code is a self documenting code. Compare:

int x;
String y;

and

int hitPoints;
String playerName

A lot better, isn't it?
And if you have a self documenting code like the last one, when you put comments to explain it, if you are not going to add some value to the explanation, it's better to leave it as it is.



/** calcualte the number of the likes */ 
public int calculateNumberOfLikes()

That's what I would call a waste of time. Who'd do that anyway, well, more people than you think, not all people have learnt to program the way programming should really be.




/** This function calculates the number of the likes of a friend.
 * 
 * This function calls an FQL query which determines the pages a friend likes,
 * and calculates the number of times he liked an item in that page
 *
 * @author someone 
 */ 
public int calculateNumberOfLikes()

This is just a little example to show the forms a javadoc comment has. The first sentece is a summary of what the function does. Then a more detailed description, then some javadoc-specific tags about the purpose of the function, we can add html tags to this description in order to format it the way we want. There is a lot of tags to know about, which can give us a lot of information about a function, a class or a variable. So, it's good to have the habit of well documenting our code, cause we are not the only ones that work in our code, and we can make others win a lot of time by making it easier for them to understand our code. And in case of a solo programmer working on his hobby project, you'll eventually end up coming back to the code you wrote after some months, and then, there is no telling how much of your code you will remember and how much time you will waste because of a poor documented code.

jeudi 10 novembre 2011

Let's talk about finalize in java

So, what is finalize, how do we use it?
Well, the answer is simple, we don't. To be more precise, most of the time we don't.
The finalize method gets called when an object is garbage collected to clean any mess caused by that object. All java object have that method, since it is implemented on the Object class. And as you are already aware of, it's the mother class of all other java classes. If the object which was just garbage collected, was using system resources, the finalize method will take care of releasing them. An example of these resources could be an open file. So be careful when you try to use it, it won't clean your java objects, it will only take care of non-java resources.

The finalize method shouldn't be used since it's not a very reliable method, since there is no guarantee that the object will be garbage collected during the lifetime of an application, in addition to the fact that it takes care only of non-java resources. But if you want to use it, here's how to proceed.

You have to declare your method this way:

protected void finalize () throws throwable
Now, inside the method:

Since the method is error-prone, it should contain the famous try-catch-finally statement


try
{
    //you do your personalized cleaning operations here
} finally {
    //then you have to call the parent finalize method
    super.finalize()
}
 
Now you have your finalize() method ready for action, just don't forget, don't rely on it too much, because maybe it will be called, and maybe it won't.

mercredi 26 octobre 2011

AI contest

If you are interested in game programming and AI, you might want to take a look at the AI ants challenge. It is a challenge sponsored by google, and this is the presentation of the contest by the official website :
The AI Challenge is all about creating artificial intelligence, whether you are a beginning programmer or an expert. 
All you have to do is pick your favourite programming language. There is already about 20 of the most popular languages, and the contest organizers are willing to add another ones if they receive requests on the forum. Once you know what programming language you are going to use, you can download the tools that will help you in your quest, and then download the starter kit for that language. The game engine is written in python, so you'll need to install python on your computer. Don't worry if you don't know any python at all, you don't need to. As for the installation of python, it's pretty straightforward. If you are on a Linux machine, it should be installed already. If you are on windows, you can download a windows installer which will do the work for you, or you can use cygwin.

You should participate regardless of your level. You could get a good rank in the leader board, which will look good on your resume, but even if you don't, it was a good learning opportunity, and you'll have some prerequisites for future contests.

Good luck everyone!

vendredi 21 octobre 2011

Creating an object in actionscript

The creation of a visual component is an operation that requires many steps.
First, we start by constructing the component, either using the default constructor or any constructor you have defined on your own

var imDaButton:Button = new Button(); 

Now the component needs some properties, "hey, I'm like all the other buttons out there, give me some properties so that I feel a little bit different", said imDaButton
Here you go

imDaButton.label="warrior";

imDaButton.setStyle("cornerRadius", 2);

imDaButton.addEventListener(MouseEvent.CLICK, doStuff);



Ok, now our button is happy, it has some properties of its own. A label to be proud of, some weird corners, and it does stuff when it's clicked.

Once you've got your button all ready to hit the road, you can add it to a parent.


someParent.addChild(imDaButton); 

The method initialize() is called automatically when we call addChild(). This method is used to initialize the internal structure of the component.
It first dispatches a preinitialize event which could be used for example to load some external resources, like loading styles dynamically or loading resource bundles. Be careful when you use a listener for this event in your container and try to access children for the container. For example, if you have:


someParent.addEventListener(FlexEvent.PREINITIALIZE, parentPre); 


private function parentPre(event:FlexEvent):void

{
    imDaButton.enabled=false; 
}


In this case you'll have an error #1009 null has no properties since imDaButton wasn't created yet. So, when is it created?

That's simple, the method createChildren() is going to do that for you. That's where the component internal structure is created. It could be the class managing the button's label, which is UITextField in this case. This method can be overriden in custom components so that you create the children you want, and place them where you want once this method gets called


Now that your button was created, along with any other component in your container, the initialize event is dispatched, and you can now play with the children of your container using the listener to this event. 


After that, creationComplete event is dispatched to notify us that the children, and their children were created. Note that many other steps happen during the creation of the internal structure of a component: property processing, measuring, layout, and drawing.


dimanche 16 octobre 2011

Pointers

Pointers is such a difficult concept for beginners. Back when I started learning C, I was never able to understand them, not until I've read countless tutorials about them. So I'll try to explain them for anyone who will face the same issues as me. You already know what variables are and what we use them for. Let's take the example of the city you live in.  It's a mixture of houses, and different kinds of buildings. Our variables here are the buildings.

int my_var;  

Every building has an address. Likewise, every variable has an address in memory.

/** we can access the address of a variable in memory using &my_var

to print this value, we have to use %p in printf to print an address*/  

printf("the variable my_var  has the address: %p ", &my_var);

So what's a pointer, a pointer is also a variable. A variable which contains the address in memory of another variable. How do we illustrate this concept in our city. Suppose you want to send a postcard to your friend. In order for that postcard to reach its destination, you have to write the address of your friend. So a pointer in real life is that postcard. A pointer could be also, a direction sign pointing to your city. So when someone needs to get to your city, he has to "ask" that pointer. In C, some functions, the functions that take a pointer as parameter need to "ask" the pointer to know the value of the variable they point to.

/** declaring a pointer is pretty straightforward, you just give him the type of the variable he needs to point to, and the variable he points to */

int my_var = 7;

int *pointer_to_my_var = &my_var;

printf("the value of my_var is: %d", *pointer_to_my_var);

printf("the address of my_var is : %p", pointer_to_my_var);


To access the variable my_var using the pointer we have to use *, which is also called the indirection operator. Therefore, you should be careful, as you can see, it's not *pointer_to_my_var that contains the address of my_var, but pointer_to_my_var. So the following declaration is equivalent to the previous one.
int my_var = 7;

int *pointer_to_my_var ;   

pointer_to_my_var = &my_var ;


Ok, so what about functions that take as parameter a pointer.
int add(int * x, int * y) {

     return *x + *y;

}

//to call that function we have two methods

int x=7;

int y=9;

//we use the address of the variable directly

add(&x, &y);

// or declare a pointer to x and to y and pass them to the function

int *pointer_to_x=&x;

int *pointer_to_y = &y;

add(pointer_to_x, pointer_to_y);


That's what I called earlier asking the pointer to know the value of the variable it points to, it's like the postman read the address on your postcard so that he could deliver the postcard to your friend.

vendredi 14 octobre 2011

Thoughts about programming

IMHO, programming is not just about writing code, correcting bugs, learning a couple of programming languages, or knowing how to do some tricks with them. Programming is about solving problems, or even better, it's about the approach taken to solve a problem. Sometimes there is only one way to do things. But most of the time, there are many ways. What makes the difference between an average programmer and a good one is that the latter will always find a better way to solve the problem.

It's not that the first one is not able to find that solution, it's just that when he found the first solution that came to his mind, he didn't take the time to think further about the problem, he didn't ask himself the questions you should ask yourself: Is this the only way to do it? Isn't there a better way? Why don't I try to solve this problem in more elegant way. You have to challenge yourself, try to be better than yourself.

That's what makes the difference. There are average programmers who remain average either because they don't want to go that extra mile and try to find that better solution, or because someone taught them in the beginning some way to think, which was not the best way and they didn't have the chance to learn another way. As for the rest, those who aspire to become always better, stop writing code for a second, take the time to think deeply, try to read some code written by professional people and ask yourself why did they do it this way not that way. And as long as you are passionate about programming and you keep learning new things, you'll end up being the one finding the best solution.

dimanche 11 septembre 2011

Tomcat 7 manager

I've installed tomcat so many times before, but when I did it this time, I had some trouble finding how to run the manager app. If you go to the manager documentation, they are going to tell you add the role manager-script. I added that role, and I'm still having the 403 error, I was wondering, what the hell is going on, after so many retries, I've made this search on google, tomcat 7 manager roles, I found this result. Well, it's true that apache has a good documentation, and that these new roles are a good way to separate different tasks, but why would they put this under migration guide instead of the manager how-to.

lundi 14 mars 2011

Les victimes de Pwn2Own

Bonjour tout le monde,

Le fameux challenge de piratage a eu beaucoup de victimes cette année, ce qui est toujours le cas, on trouve dans la liste,le BlackBerry, l'iphone, le mac qui a été pénétré en 5 secondes, bah ouais, 5 secondes, c'est même inférieur au temps que ça prend de taper "le mac a été piraté en 5 secondes". En effet, celui qui l'a piraté a juste exploité des vulnérabilités dans le browser safari. Un autre browser a été exploité dès le premier jour, vous l'aurez tous deviné, c'est IE8 bien évidemment. Google et Firefox n'ont pas été touchés parce que personne n'a accepté le challenge, personne n'a donc eu le prix de 20.000 dollars proposé par Google pour l'équipe de recherche qui réussira à pirater son navigateur, mais ça ne prouve pas que ces navigateurs n'ont pas de faiblesses, bien au contraire.

Le concours Pwn2Own est organisé par HP TippingPoint et elle offre aux chercheurs des sommes d'argents pour la découverte des vulnérabilités dans un les navigateurs et les plateformes mobiles. Ensuite, ces points faibles sont fournis aux vendeurs associés afin qu'ils effectuent des corrections dans leurs produits.

Ce qu'il faut retenir surtout, c'est qu'il n'y a pas le concept de sécurité absolue dans le monde de l'informatique, il est possible de tout pirater, il suffit juste de posséder les outils nécessaires.Vous pouvez toujours utiliser chrome ou firefox, mais ça n'empêchera pas quelqu'un qui veut vous pirater d'accéder à vos données, alors prenez toutes les mesures nécessaires de sécurité, comme l'installation des dernières mises à jour pour le système d'exploitation, les différents logiciels et surtout votre antivirus.