Thursday, 27 January 2011

Android - OutOfMemoryError: bitmap size exceeds VM budget

When you see this error in your console, you're most certainly using some large bitmaps in your application which are taking up a of memory (especially T-Mobile G1, limited to 16 MB).

The solution to this problem is to create a temp storage and pass it to BitmapFactory before decoding. Take into consideration that this error isn't necessarily triggered when handling bitmaps programmatically, but also when specifying them through xml.

If you do specify the bitmaps through xml, for example a background image for a RelativeLayout, just remove the background declaration from xml, and declare the background programmatically, after properly decoding the bitmap.

So, here's the code:


BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
     
Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(), R.drawable.background, options);

If you want to set this bitmap as background, just create a BitmapDrawable, and set it as background:


BitmapDrawable bitmapImageDrawable = new BitmapDrawable(bitmapImage);
RelativeLayout yourRelLayout = (RelativeLayout) findViewById(R.id.YourRelativeLayoutId);
yourRelLayout.setBackgroundDrawable(bitmapImageDrawable);

Also, you might want to tile you image when setting it as background, so you'll need to do this before calling setBackgroundDrawable


bitmapImageDrawable.setTileModeX(Shader.TileMode.REPEAT);
bitmapImageDrawable.setTileModeY(Shader.TileMode.REPEAT);

Don't forget to call bitmapImage.recycle() after usage.

Monday, 17 January 2011

Funny Violence BlackBerry Version - part 1

Funny Violence is a BlackBerry device application developed on Koprol API. The basic functionality is this:

  • 1. The user logs into the application with his Koprol credentials.
  • 2. He has the ability to search for a specific location or to choose one from the “Top Places Nearby” section
  • 3. The user has a range of funny weapons.
  • 4. On the selected place he sees all users that checked in.
  • 5. He can "Attack" his friends on Koprol with these weapons.
  • 6. The user can select a weapon at any time.
  • 7. After the attack, a stream is published under the specific location in the form “@attacked_user, you have just been sheep'ed by @attacker_user' at specific_location”.


One of the first things that this application needed was a library for XAuth, because the BlackBerry device cannot play the HTTP re-direct game normally played in the "3-legged" OAuth model. As there isn't a BlackBerry library for XAuth, Oauth Signpost for BlackBerry needed to be adapted.

Now, the Signpost source files, under oauth → signpost → basic there's a file called DefaultOAuthProvider.java which extends AbstractOAuthProvider. We'll talk about AbstractOAuthProvider later. In DefaultOAuthProvider.java, you'll se that the request is created and different request properties are set. Now, we'll create a new class, DefaultXAuthProvider.java, which will extend AbstractXAuthProvider (later in this post). The first thing we'll change is the constructor. Oauth uses requestTokenEndpointUrl, String accessTokenEndpointUrl and authorizationWebsiteUrl as parameters (see Oauth docs). For XAuth we need requestTokenEndPointUrl, which is bassically the url that accesses the access token directly (XAuth basically short-circuits Oauth) and the username and password used to login into the target server. So the code will look like this:

public DefaultXAuthProvider(String requestTokenEndpointUrl, String username,
            String password) {
        super(requestTokenEndpointUrl, username, password);
}

Now, let's set some request parameters. They may vary given the service provider, but in this case (Koprol), we need to set the following:

connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

Now, let's move on to oauth → signpost → signature in the Signpost source codes and open the AuthorizationHeaderSigningStrategy.java. Here, the request is signed and the parameters are added to the request header. We'll add some extra parameters to the header (according to XAuth docs): XAUTH_MODE, XAUTH_USERNAME and XAUTH_PASSWORD. I've defined these values in oauth → signpost → Oauth.java. I'll paste the modification from this file too, but first, our new request parameters:

if (requestParameters.containsKey(OAuth.XAUTH_MODE)) {
        sb.append(", ");
        sb.append(requestParameters.getAsHeaderElement(OAuth.XAUTH_MODE));
}
if (requestParameters.containsKey(OAuth.XAUTH_USERNAME)) {
        sb.append(", ");
        sb.append(requestParameters.getAsHeaderElement(OAuth.XAUTH_USERNAME));
}
if (requestParameters.containsKey(OAuth.XAUTH_PASSWORD)) {
        sb.append(", ");
        sb.append(requestParameters.getAsHeaderElement(OAuth.XAUTH_PASSWORD));
}

The previous code goes right after

sb.append(OAuth.toHeaderElement(OAuth.OAUTH_SIGNATURE, signature));

Now, the modifications from Oauth.java:

public static final String XAUTH_MODE = "x_auth_mode";
public static final String XAUTH_MODE_VALUE = "client_auth";
public static final String XAUTH_USERNAME = "x_auth_username";
public static final String XAUTH_PASSWORD = "x_auth_password";

Ok, let's move on to AbstractOauthProvider.java which we talked about at the beginning of this post. You'll find it under oauth → signpost. Here, normally, the token is requested and retrieved. Of course, for XAuth we need to make some changes, so, we'll create a new file AbstractXauthProvider.java, based on AbstractOauthProvider.java.

Here, we only need accessTokenEndpointUrl, username and password so we can get rid of requestTokenEndpointUrl and authorizationWebsiteUrl. Next, get rid of callbackUrl parameter from retrieveRequestToken method.

Next, retrieveRequestToken will change to retrieveAccessToken with consumer as parameter. Inside this method we'll call the retrieveToken method which will be modified as follows: after

if (this.listener != null) {
        this.listener.prepareSubmission(request);
}

we'll add:

HttpConnection connection = (HttpConnection)request.unwrap();
            
StringBuffer requestBody = new StringBuffer();
requestBody.append(OAuth.XAUTH_MODE + "=" + OAuth.XAUTH_MODE_VALUE + "&");
requestBody.append(OAuth.XAUTH_USERNAME + "=" + username + "&");
requestBody.append(OAuth.XAUTH_PASSWORD + "=" + password);
byte[] body = requestBody.toString().getBytes();
connection.setRequestProperty("Content-Length", "" + body.length);
         
OutputStream os = null;
try {
    os = connection.openOutputStream();
    os.write(body);
} finally {
    try{
        if(os != null) {
            os.close();
        }
} catch(IOException e) {}
}

We created a new connection and added the specific XAuth parameters.

Finally, we're handling the exceptions:

public String getAuthorizationWebsiteUrl() {
        throw new RuntimeException("Method is not supported");
    }   
    public String getRequestTokenEndpointUrl() {
        throw new RuntimeException("Method is not supported");
    }   
    public void retrieveAccessToken(OAuthConsumer consumer, String oauthVerifier)
            throws OAuthMessageSignerException, OAuthNotAuthorizedException,
            OAuthExpectationFailedException, OAuthCommunicationException {
        throw new RuntimeException("Method is not supported");
    }   
    public String retrieveRequestToken(OAuthConsumer consumer,
            String callbackUrl) throws OAuthMessageSignerException,
            OAuthNotAuthorizedException, OAuthExpectationFailedException,
            OAuthCommunicationException {
        throw new RuntimeException("Method is not supported");
    }   
    public void setOAuth10a(boolean isOAuth10aProvider) {
        throw new RuntimeException("Method is not supported");
    }

Finally, there's a very important configuration that we need to make in order for the headers to not be altered prior their arrival to the server. Edit the following file: C:\Program Files\Research In Motion\BlackBerry Email and MDS Services
Simulators\MDS\config\rimpublic.property (the location may vary depending on your installation path. In my case, the path is: C:\eclipse\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components\MDS\config\rimpublic.property) and change this line:

application.handler.http.AuthenticationSupport = true

so it'll look like this:

application.handler.http.AuthenticationSupport = false

End of Part 1. Exciting continuations coming to you soon

External JAR problems on BlackBerry projects under Eclipse


I've been recently trying to parse some external XML's and for this I wanted to user the KXML2 library. But I came across a pretty nasty problem.

To be able to use this library you obviously need to add the jar file on your project. Everything ok until BlackBerry signals that it couldn't find the jar file specified. I wasn't the only one with this problem (see http://www.thinkingblackberry.com/archives/204) so it wasn't something I did wrong.

Anyway, there's a simple solution for this shortcoming. Here are the steps:

1. Download the jar file

2. Preverify the .jar file

This can be done from the command line. Before this, be sure you have java set in your environment variables (in my case “C:\Program Files\Java\jdk1.6.0_20\bin”). Now, copy your .jar file in JDE_HOME/bin and run the following command:

preverify.exe -classpath ..\lib\net_rim_api.jar your_jar_file_name.jar

Where JDE_HOME is the BB JDE home directory or the component pack home directory. The “preverify” command will generate the file in the “output” folder in JDE_HOME/bin.

3. Add the .jar to your project

Create a lib folder in your project, copy the preverified .jar file in there. Right-click on your project, go to “Properties” → “Java Build Path” → “Libraries” tab → “Add JARs”, select the preverified .jar file that you've just copied in the lib folder, click and “OK”. Now, while in “Properties” window, go to “Order and Export” tab and check the newly added .jar. Click “OK” and you should be done.

BlackBerry Simulator - Enable Internet Access


Testing your BlackBerry device application that connects to the internet obviously requires your simulator to initiate a connection. This can be done very easy from Eclipse IDE. Here's what you can do. There are 2 ways:

1. From Properties

Right-click on your project, go to "Properties" -> "Run/Debug Settings", click on your application from the list and click "Edit". Go to the "Simulator" tab, check the "Launch Mobile Data System Connection Service (MDS-CS) with simulator" box and click "OK".

2. From Eclipse IDE folder

Go to %ECLIPSE_DIR%\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components\MDS\ and launch run.bat.

For this case, remember to launch run.bat before you start the simulator.

General Note

The internet access is not global so it can be enabled only for individual projects.

BlackBerry Screen Transition Sample Code


Screen Transition Sample

The code that I'm about the share and go through illustrates a slide and a fade transition. When the user opens the application, the first screen appears on the BlackBerry® device and displays a button. When the user clicks the button, a second screen slides onto the display from the right. The second screen automatically fades off of the display after two seconds.

First, let's add the imports:

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;

Now, let's continue with the application:

public class ScreenTransitionSample extends UiApplication implements FieldChangeListener {
    private Screen secondaryScreen;
    private Runnable popRunnable;

As you can see, we initialized two local variables. As the official documentation says, you must explicitly initialize local variables in a method. We've initialized the secondaryScreen variable, because Screen is the base class for all screens and popRunnable. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. We'll implement run later in the code.

Any BlackBerry application that provides a user interface must extend the UiApplication class. Our class also implements FieldChangeListener. This is because Classes that want to handle change events should implement this interface.

public static void main(String[] args) {
        ScreenTransitionSample theApp = new ScreenTransitionSample ();
        theApp.enterEventDispatcher();
    }

Class ScreenTransitionSample must have one method: main(String[] args) which is the entry point into our application. We need to create an instance of our application and start it by getting it on the event thread. Next, we will create the secondary screen:

public ScreenTransitionSample () {
    secondaryScreen = new FullScreen();
    secondaryScreen.setBackground(
    BackgroundFactory.createSolidBackground(Color.LIGHTBLUE) );
    LabelField labelField = new LabelField("The screen closes automatically in two seconds by using a fade transition");
    secondaryScreen.add(labelField);

and create the transition object:

TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
transition.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
transition.setIntAttribute(TransitionContext.ATTR_STYLE, TransitionContext.STYLE_PUSH);

which helps us create the animation. Next, we create an engine object:

UiEngineInstance engine = Ui.getUiEngineInstance();

As the official documentation says, a UI engine maintains a stack of Screen objects. As it pushes screens onto the stack, it draws them on top of any other screens already on the stack. When the application pops a screen off the stack, it redraws the underlying screens as necessary. Only the screen on the top of the stack receives input events.

Now, apply the transition, create a new one and apply that too onto the secondary screen:

engine.setTransition(null, _secondaryScreen, UiEngineInstance.TRIGGER_PUSH, transition);
transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
transition.setIntAttribute(TransitionContext.ATTR_KIND, TransitionContext.KIND_OUT);
engine.setTransition(secondaryScreen, null, UiEngineInstance.TRIGGER_POP, transition);

Now, create the base screen and attach a button:

MainScreen baseScreen = new MainScreen();
baseScreen.setTitle("Screen Transition Sample");
ButtonField buttonField = new ButtonField("View Transition", ButtonField.CONSUME_CLICK) ;
buttonField.setChangeListener(this);
baseScreen.add(buttonField);

Push the screen onto the stack and paint it:

pushScreen(baseScreen);

then continue with the run method that we discussed about at the beggining and the rest of the code:

popRunnable = new Runnable() {
        public void run() {
            popScreen(secondaryScreen);

here we remove the base screen from the display stack, and updates the secondary screen.

}
        };
    }

    public void fieldChanged(Field field, int context) {
        pushScreen(secondaryScreen);
        invokeLater(popRunnable, 2000, false);
    }
}

We added a listener onto the button and now we took care of the action that needs to be taken on it.

That's about it. Here's the full code sample:

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;

public class ScreenTransitionSample extends UiApplication implements FieldChangeListener {
    private Screen _secondaryScreen;
    private Runnable _popRunnable;
    public static void main(String[] args) {
        ScreenTransitionSample theApp = new ScreenTransitionSample ();
        theApp.enterEventDispatcher();
    }

    public ScreenTransitionSample () {
        secondaryScreen = new FullScreen();
        secondaryScreen.setBackground( BackgroundFactory.createSolidBackground(Color.LIGHTBLUE) );

        LabelField labelField = new LabelField("The screen closes automatically in two seconds by using a fade transition");
        secondaryScreen.add(labelField);
        TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_SLIDE);
        transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
        transition.setIntAttribute(TransitionContext.ATTR_DIRECTION, TransitionContext.DIRECTION_RIGHT);
        transition.setIntAttribute(TransitionContext.ATTR_STYLE, TransitionContext.STYLE_PUSH);

        UiEngineInstance engine = Ui.getUiEngineInstance();
        engine.setTransition(null, _secondaryScreen, UiEngineInstance.TRIGGER_PUSH, transition);

        transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
        transition.setIntAttribute(TransitionContext.ATTR_DURATION, 500);
        transition.setIntAttribute(TransitionContext.ATTR_KIND, TransitionContext.KIND_OUT);
        engine.setTransition(_secondaryScreen, null, UiEngineInstance.TRIGGER_POP, transition);

        MainScreen baseScreen = new MainScreen();
        baseScreen.setTitle("Screen Transition Sample");

        ButtonField buttonField = new ButtonField("View Transition", ButtonField.CONSUME_CLICK) ;
        buttonField.setChangeListener(this);
        baseScreen.add(buttonField);
        pushScreen(baseScreen);
    }
}

popRunnable = new Runnable() {
    public void run() {
        popScreen(_secondaryScreen);
    }
};

public void fieldChanged(Field field, int context) {
    pushScreen(_secondaryScreen);
    invokeLater(_popRunnable, 2000, false);
}

For more examples you can check out the official documentation on http://docs.blackberry.com/en/developers/subcategories/?userType=21&category=Development+Guides

Blackberry environment problems under Ubuntu


In one of my previous entries I presented the necessary steps to set up a working BlackBerry environment under Ubuntu. It does set up the environment, but that is only the beginning. More problems arise.

There's a big problem regarding the editor. It's so buggy that it makes editing virtually impossible. You could of course write your code in another editor and import it in Eclipse but that doesn't sound too elegant, does it.

The best solution, for now, is to set up your environment under Windows on VirtualBox.

Follow these instructions on installing VirtualBox http://www.virtualbox.org/wiki/Linux_Downloads

After the installation, start your machine and follow the steps from this tutorial http://bogdannistor.blogspot.com/2011/01/blackberry-environment-setup-under_17.html

I tested the BlackBerry simulator in VirtualBox on a 2GB of RAM machine and it proved to run quite smooth and, I must say, better than on Ubuntu, on same machine.

BlackBerry Environment Setup Under Windows


Notes:

These instructions were developed under Windows XP

Before proceeding with the setup, create a developer account on http://na.blackberry.com/eng/developers/

Steps:

1.Install Sun JDK. Download the latest version of JDK for Windows from http://www.java.sun.com and install it.

2.Install Eclipse. You need to get the windows 3.5.2 version from here http://www.eclipse.org/downloads/packages/release/galileo/sr2 and extract it.

3.Start Eclipse and go to: Help -> Install New Software and click on Add. In the location field insert this link http://www.blackberry.com/go/eclipseUpdate/3.5/java and click OK. From the list that is generated select “Blackberry Java Plug-in” and “BlackBerry Java SDK 5.0.0” and start the installation. Now you'll use the user/password created on http://na.blackberry.com/eng/developers/. You'll be prompted for those. After eclipse finishes the installations, restart it.

Now you should have a working environment.

BlackBerry Environment Setup Under Ubuntu


Notes:

These instructions were developed under Ubuntu 10.04 (Lucid Lynx), wine version 1.1.42.

Before proceeding with the setup, create a developer account on http://na.blackberry.com/eng/developers/

Steps:

1. Install wine

sudo aptitude install wine

2.From winetricks you need gdiplus and msxml3. You can get winetricks from here http://wiki.winehq.org/winetricks. To install these simply run:

sh winetricks

and a GUI with a list of available packages will open.

3.Install Sun JDK for windows on wine. Download the latest version of JDK for Windows (remember to get the Windows version, not the linux one. You'll be installing it with wine) from http://www.java.sun.com. Go to your download folder and run:

wine jdk-6u20-windows-i586.exe

4.After you install the JDK you need to insert your JDK path on wine. For this, edit ~/.wine/system.reg and find this line:

"PATH"=str(2):"C:\\Windows\\system32;C:\\Windows"

Change it so it'll look like this:

"PATH"=str(2):"C:\\Windows\\system32;C:\\Windows;C:\\Program Files\\Java\\jdk1.6.0_20\\bin"

After this you can try:

wine javac

to see if the new settings are correct.

5.Install Eclipse. Do not install from aptitude. You need to get the windows 3.5.2 version from here http://www.eclipse.org/downloads/packages/release/galileo/sr2 and extract eclipse in your home folder. It needs to be in your home folder, otherwise RIM JDE won't work.

6.Start Eclipse by going into ~/eclipse and running:

wine eclipse

and go to: Help -> Install New Software and click on Add. In the location field insert this link http://www.blackberry.com/go/eclipseUpdate/3.5/java and click OK. From the list that is generated select “Blackberry Java Plug-in” and “BlackBerry Java SDK 5.0.0” and start the installation. Now you'll use the user/password created on http://na.blackberry.com/eng/developers/. You'll be prompted for those. After eclipse finishes the installations, restart it.

7.Go to eclipse_home/plugins/net.rim.ide..../components/bin and, on all .rc files, change “\” with “/”.

8.Go to eclipse_home/plugins/net.rim.ide..../components/simulator/ and change paths on SimPackage_JDE.rc

Now you should have a working environment.

Links:

http://supportforums.blackberry.com/t5/Java-Development/Eclipse-JDE-Plugin-Linux-a-working-environment/m-p/192473

Django - Set field order on extending form

Let's assume you have two registration forms, one for regular user registration and the second for special user registration

The difference between those two would be that for regular user you'd only need, let's say, an email field and two password fields, so it would look like this:

class RegularUserForm(forms.Form):
    email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'class': 'required'}), label="Email")
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'required'}), label="Password")
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'required'}), label="Confirm Password")

For the special user registration, the form might look like this:

class SpecialUserForm(forms.Form):
    username = forms.CharField(max_length=30, min_length=3, label="Username", widget=forms.TextInput(attrs={'class': 'required', 'id': 'username'}))
    email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'class': 'required'}), label="Email")
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'required'}), label="Password")
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'required'}), label="Confirm Password")
    first_name = forms.CharField(label="First Name", required=False)
    middle_name = forms.CharField(label="Middle Name", required=False)
    last_name = forms.CharField(label="Last Name", required=False)
    institution = forms.CharField(label="Institution", required=False)

Now, this isn't very nice, is it? We added emai, password1 and password2 fields to the second form. This is not a good practice, so the solution is to make the SpecialUserForm extend the RegularUserForm. Then our form will look like this:

class SpecialUserForm(RegularUserForm):
    username = forms.CharField(max_length=30, min_length=3, label="Username", widget=forms.TextInput(attrs={'class': 'required', 'id': 'username'}))
    first_name = forms.CharField(label="First Name", required=False)
    middle_name = forms.CharField(label="Middle Name", required=False)
    last_name = forms.CharField(label="Last Name", required=False)
    institution = forms.CharField(label="Institution", required=False)

But we stumble on another problem. The form fields' order would now be: username, first_name, middle_name, last_name, institution, email, password1, password2. It's not very pretty, but luckily django allows us to take control over the fields' order.

For this, we will add to the SpecialUserForm an __init__ function where we will control the fields' order:

def __init__(self, *args, **kw):
    super(RegularUserForm, self).__init__(*args, **kw)
    self.fields.keyOrder = [
        'username',
        'email',
        'password1',
        'password2',
        'first_name',
        'last_name']

Django forms optimization tips

I was reading some interesting articles on form optimization and I summarized some that I thought were important


Remove unnecessary fields

You have to keep in mind that most of the users are lazy and fields like birth date or gender will make them abandon your form. So ditch them


Keep it simple

Keep the forms as simple as possible.Just because we can use CSS to do all sorts of fancy things with text boxes, doesn’t mean we should. Keeping form fields simple will ensure that customers understand their purpose and won’t confuse them with design elements.


Clear the "Clear" button

Ditch the clear button if you don;t want the users to accidentally delete what they've just entered.


Use point of action references

Users might get confused by the information you're asking for. For example, the 3 digit CVV code found on the back of credit cards. Include a small popup link with more information or make it visible when the users tabs into the field in question.


Use Ajax form validation

Use Ajax validation. It will keep your users posted on whichever they're not doing right in the form. This can be very effective, since it does not break the flow of the process. Its easier to correct an error immediately after entering it rather than after the whole form is completed.
Here's an example from the Odeon website



Two-column vs one

The choice depends on the content of the form, a good practice depending rather on the general alignment. Below is a good example of a simple form that places each label above its related form element.



What are the benefits to this type of form layout, as opposed to a two-column form? First, the form labels have plenty of space to allow for future changes to the text inside them. Another benefit is that the form is not as cluttered looking, having plenty of whitespace in the label areas, so it’s easy to read and easy to associate the labels with the fields.

By contrast, look at the two-column form below:


Especially because of the left-aligned text and lack of color, this form doesn’t have the same clean, visual effect as the previous example.

It’s pretty and user friendly, however to achieve a clean, organized look with a two-column layout, as shown by the example below from Odeon.


Clearly indicate required fields

It’s common to indicate required fields by means of the asterisk symbol (*) in a different color than the rest of the text, so the required indicator stands out. Although most sites nowadays include this indicator, some still fail to use it properly.
The explanatory text that describes the purpose of the asterisk should be placed immediately above the form that is to be filled out, so the users see it before they begin filling it out.

In-field labels

Display labels inside form fields. The label remains until the user starts typing. Here are some good links on this

http://fuelyourcoding.com/in-field-labels/

http://www.csskarma.com/blog/sliding-labels/

Pair Programming

Pair programming is an agile software development technique in which two programmers work together at one workstation, or, remotely, through screen sharing.

The two programmers share two roles: driver and navigator. These two roles are alternated between the two at given time periods. The driver types the code while the navigator reviews each line of code as it is typed in. The navigator also identifies problems, concentrates at the overall design, so he focuses on a higher abstraction level then the driver, who is responsible for the syntax, semantics and algorithm.

A study shows that pair programming reduces the bugs present in the code by 15%-50%, although the programming time is slightly bigger. Even having the code delivered in a longer time than in the case of a single programmer, the time and costs spent for fixing the bugs are highly reduced.

As programmers work together, knowledge passes from one to another, and they pick up techniques and practices from each other.

Pair programming can be considered a social skill, and it can take some time to learn. Most often, the two programmers are not at the same level of discipline and knowledge and the higher one tends to become a mentor. Teacher-student relationship is not what pair programming is all about, some developers are poor teachers, but communicating, even if it can be avoided, and discussing ideas and possibilities is the key in pair programming.

With time, the two programmers will become more confident with the code they write, the knowledge base inside the team will increase, the productivity will increase and time management will improve.

An important factor in gaining knowledge is that the pairs should switch off regularly. This way, pairing spreads several kinds of knowledge throughout the team with great efficiency.
Many non-agile teams do not use pair programming at all and studies show that “islands of knowledge” tend to accumulate. Individual programmer tends to know a lot of important things that others are not aware of. If one of these islands leaves the team, gaps are likely to appear in the team, and projects tend to be delayed or worse, canceled.

A pair programming theory shows that by spreading large knowledge base widely within a team, exposure to staff turnover threat is reduced. In Extreme Programming, the Truck Number is described as the number of team members that would need to be hit by a truck to kill the project. Extreme Programming projects strive to keep the Truck Number as close as possible to the total team size. So if someone leaves, there are usually several developers that know something about the specific project.

Enhancing Communication

We all have something to say, especially when dealing with clients, when we need to receive and give feedback as often as possible.

Here are a few ways of improving the communication skills

Listen

The first step in good communication is to listen to what others are saying. If you are so focused on saying your piece, you’ll miss important points. Listen to the points colleagues or clients are making. When they have finished their thought, go ahead with your own. It also helps to build relationships if you can address another’s comments in your own.

For example, by saying: "I agree with Liviu that we should move the Charts portlet..." will be much more effective and demonstrate that you are a team player better than the more self-centered, "I think we should move the..." By acknowledging that you are listening, you will gain appreciation and respect. After all, this is plain polite.

Make sure you understand what the other person is trying to say. Keep an open mind. Be flexible about compromises and alternate solutions.

Communicate clearly

In communication the receiver should understand the things clearly. So the message (should it be spoken or emailed) should be very clear. When speaking, he should stop whenever necessary. Lowering and rising of tone also enhance the impact.

Right words

Complex language is useless if the receiver doesn't understand a thing. Always use simple words and translate whatever you're doing in normal language.

Think ahead

If you say everything that crosses your mind exactly when you think it, it’s likely that some stupid things might come out and you won't be taken serious in the future.

Planning what you are about to say can go a long way in avoiding embarrassment and providing with quality ideas.

Advise the client

If the client hadn't thought about something, it doesn't mean he won't approve with you. Share with him every little thought that can enhance or beautify his product.

Get to the point

In the business place, there is little time for long rambling thoughts or stories. Eliminate most of the idle chatter in a professional context and get to the point. Stories and random thoughts are great among friends at the water cooler or lunch table, but they don’t belong in the conference room. Your message should be clear and simple.

Python assignment: by value or by reference?

I was really confused some time ago regarding the assignment method used by Python. Ok, let's take it one step at a time.

Assignment in Python is done by reference, not by value

This is correct, but let's take the following example:

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1
>>>

The expected result would be 2, not 1. Let's take the following example to make things even more strange:

>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> list2[0] = 100
>>> list1
[100, 2, 3]
>>>

Well, which one is it? The answer is: integers are immutable, which means we can't change them. Let's analyze the first example: a = 1. Two things are being created: the object a and the value 1. Some a points to 1. Next, b = a. Object b is created and b points to the value of a.

Now, let's shake things up: a = 2. You'd think that the innitial value that a was pointing too will change. But, as integers are immutable, that doesn't happen. Instead, a new integer object is created. So now, a is now pointing at the new integer object specified, as for b, it points at at the same old object that a used to be pointing at.

Javascript assignment by reference vs object cloning

I run into a problem recently when trying to validate some fields using Javascript

var opt = {
    url: '/users/ajax/check/username/',
    preloading: function(obj){
        ...
    }

$('#id_username').inputChecker(opt);

var opt2 = opt1;
opt2.url = '/users/ajax/check/password/';
$('#id_pass1').inputChecker(opt2);

In JavaScript, when an object variable is assigned to another, it is assigend by reference. That means, both variables point to the same object. This has the implication that if a one variable's property is changed, it is changed in the other variable as well.

So, when I assign to opt2.url a value, op1.url received the same value. This may be the required behavior sometimes, but not here.

In this case, the solution would be to clone the object. Here's the code:

var opt2 = $.extend({}, opt); 
opt2.url = '/users/ajax/check/password/';
$('#id_pass1').inputChecker(opt2);

http://stackoverflow.com/questions/122102/what-is-the-most-efficent-way-to-clone-a-javascript-object