What is Pair Programming

Pair Programming is an interesting technique of Agile Software Development. It means to program the code in pair, i.e. two programmers work on an assignment together. One writes the code, and other think the code. One who Writes Code is called Driver, who has the steering wheel in hand i.e. the keyboard. Other who Think the Code, is called Navigator or Observer. Navigator means who helps in navigation, who shows the path, who see the good or bad road ahead and also try to find the right path to move on. One who observe the every proceeding and provide a helping hand by discussing the observations for right implementation.

What does it mean to think the code? Isn't coding about typing the programming constructs in some specific language? You should be good with that language and then write the program. Isn't that all? Probably Not.

Writing something is not all about writing. It would involve much more than writing. Suppose you are writing an article in newspaper. Whatever language you use, writing this article means first to think what to write, from where to start, what to put in for right message, and right impact. It may require a lot of research, analysis and understanding of the desired topic. It may requires a lot of brainstorming also before finalizing what to write. It may need consultation with other experienced hand. Then it would need the drafting and editing or the written matter.

Similarly writing the software program is also not only about writing the code. It means much more, many things as we have written above for a new article and probably something more specific. This is what we call "Think the Code". And this is where a Navigator or Observer helps in, by doing all such things in parallel while writing the code.

We might have used pair programming one or other time while following any of the software development techniques. We (two developers) may prefer to sit together to write a piece of software. It happens sometimes when we find any part very complex, or quite vast in concepts, tricky or might be having many integration to control and so on. And we follow the same things as we mentioned above. One write the code, and other keeps thinking what should we write considering the bigger picture of the problem. One focuses on complexities of writing the code using language constructs, and other focuses on complexities of design and integration of code. So pair programming is not started with Agile Software Development only; but it is an important step of being Agile.

In this article, we shall discuss what Extreme Programming states about pair programming. It advocates the development of most of the production code in pairs. Reason is all about its benefits as we described briefly above. In more details, these are:

Vedic Math - General Squaring

Note: Vedic Math Blog has been moved to http://vedicmath.vedantatree.com/. Please bookmark the new address for new and existing blogs.

Today, the topic which we are going to discuss is the 'General Procedure to Square any number'. Earlier we discussed about the squaring of numbers near base, however, general procedure is another nice formula to do the squaring and is applicable universally. The method or sutra is "Vertically and Crosswise", but here it is used in a different sense; based on a procedure known as 'Dwandwa Yoga' or 'Duplex Combination Process' or 'Duplex'; denoted as (D).

'Duplex' term is used in two different sense; for squaring and for multiplication. And for current formula, it will be used in both the senses. If we are having a single or central digit, then 'Duplex' means squaring that digit (a2 ). Secondly it can be used for even digits number or on numbers having equidistant digits, then 'Duplex' means to double of cross multiplication of the equidistant numbers (2ab). This concept is very important to understand the current formula and will be used in future articles also. Let us see few example to understand it more:

For 1 digit  – D(a) = single digit = a2
                     e.g. D(5) = 52  = 25
For 2 digits – D(ab) = even digits number = twice the product of the digits (2ab)
                     e.g. D(26) = 2(2)(6) = 24
For 3 digits – D(abc) = product of equidistant digits from center and square of center digits
     = twice the product of the outer digits (2ac) + the square of the middle digit (b2 )
                     e.g. D(734) = 2(7)(4) + 32
                        = 56 + 9 = 65
For 4 digits – D(abcd) = product of equidistant numbers 
                    = twice the product of the outer digits (2ad) + twice the product of the inner digits (2bc)
                     e.g. D(1034) = 2(1)(4) + 2(0)(3)
                           = 8 + 0 = 8
For 5 digits – e.g. D(10345) = product of equidistant digits and square of center digits
= 2(1)(5) + 2(0)(4) + 32 
                        = 10 + 0 + 9 = 19

and so on. This is called Duplex.

What is SwingWorker

Swing is not thread safe as discussed with previous article. That means that any task which can take long time in execution should not be performed in Event Dispatcher Thread (EDT). Otherwise the EDT will be blocked and so the whole UI. So the solution, to perform the time intensive operations, is:

  • Start a new thread from EDT
  • Perform the heavy, time consuming task in this new thread
  • When task is done, update the Swing UI state or call any operation in EDT
  • EDT will invoke the operation

To perform above steps, we need to create a thread for time consuming task and also need to devise a mechanism to invoke the Swing Operation in EDT once the task is done (inter-thread communication). This involves some of the multi-threading code and may need to repeat it again and again with every such requirement. To abstract this code, and simplify this task; Java provides a SwingWorker class to hide all the complexities and hence the repeated work to perform these operations. It is an abstract class, which should be extended and some of its methods should be overridden to get the work done.

SwingWorker class has following important methods.

execute()
This method is the main method which should be called to start the work of SwingWorker class. As soon as you call this method, SwingWorker will give call to do the time consuming task in a new thread (described below).

doInBackground()
This method should be overridden by developer to do the time consuming task. This method will be called in a new thread, other than EDT, by SwingWorker. So any task performed on this thread won't affect the Swing UI operations. Once this method is done, result will be retained by SwingWorker which can be asked by using get() method. publish() method can also be called from this method to make the intermediate results available for GUI operations.

process()
It should be overridden if we want to process any data in EDT, as and when this data becomes available during doInBackground() operation. Suppose, one program is to load the heavy file from network which is being done in doInBackground method. Depending upon the data downloaded, you want to show the progress bar or some %age downloaded in a label on UI. This can be done by putting the work to update the label or progress bar in 'process' method. From 'doInBackground' method, we need to call the 'publish' method with the values which we want to publish; like %age data downloaded in this case. SwingWorker will call process method with this data in EDT. process method then can update the UI label. One more important point, SwingWorker may coalesced the multiple calls of publish in one call to process method, if previous calls were not executed till now. So if we are calling publish with suppose '%10', '%30', and then with '%50'; it might be possible that process method is called first with '10%' and then with '30%, 50%'. Process method should handle this case.

done()
This method is called by SwingWorker when task defined in 'doInBackground' method is finished. This is a place where final Swing UI updates should be done based on outcome from 'doInBackground' method. This method is called in EDT.

Object get()
This method is used to retrieve the result of task performed by 'doInBackground' method. Point to consider, that this method is a blocking method. So calling thread will be blocked till result is not available. So it must be used carefully, as if it is called from EDT, it will block the UI operations. One of the approach to use it could be to show model dialog box till result is received.

This way, SwingWorker class save us from defining the thread safe, and multi-threaded code again and again and work as a great utility class.

Why Swing is not Thread Safe

Big questions for Swing Developers - Why Swing is not Thread Safe? What makes Sun to design the Swing in a way that it can not handle its operation in multi-threaded environment? What if they made some extra efforts to make it thread safe, many developers would be relaxing now with comparatively simpler code? And so on..

So here is the answer. Swing is not made thread safe intentionally. It was a design decision by Swing team. Why? Because thread safety always comes at a cost. And access of Swing components in multiple threads may not be required that much.

What is thread safety and Why is that costly - Thread safety means that every part of the program should be safe to access from more than one thread concurrently. Program should be able to handle the concurrent access, either by serializing the access or ensuring that it is not disrupting the shared states or operations. Serializing the access means that access should be synchronized for access by thread, which will ensure that only one thread access the code block at a time after taking the necessary lock and hence guarantee the safe access to shared states. Or design has to be done in a way that program block does not affect any shared state or won't disrupt any operations due to concurrent access to states. Both of these changes are costly in term of performance or/and application design/development.

Why Multiple Thread Access may not be required - Because most of the UI operations are done in event fired by UI itself which are sequential in nature. There are few cases when we want to touch UI functions or states in parallel threads, like if we want to perform some time intensive operation to load the data and want to keep the UI active. But these cases are limited.

So if requirement to work in multi-threaded environment is very limited, and cost to make the code ready for multi-threaded environment is high; rationale is not to implement it. However, there should be provision using which developers can manage the UI operations in multiple threads environment also as mentioned above in case of loading heavy data in parallel.

This is what Swing Team analyzed and decided. Result is a high performing GUI development library for most of the scenarios with a capability to work with multiple threads also if required.

AWT vs Swing vs SWT

AWT, Swing and SWT are three main User Interface development technologies in Java. All three can be used to develop the desktop applications. Now the question is, which one to use and when? Each of these has its own pros and cons. In this article, we shall try to understand the pros and cons of each of these.

Let us start with AWT being the first Java GUI toolkit. It comes with standard Java packaging and hence no specific installations are required. This is the simplest form of GUI toolkit, where Java provide basic set of components, layout managers, events etc to design the desktop application.

AWT is dependent on host GUI controls (native peers). For every component in AWT, one equivalent host GUI control is built. This means, AWT directly utilizes the GUI control features set provided by host and is entirely dependent on this for available features. That is why it is called Heavyweight components library. As all the host does not support all the UI control, Sun decided to provide the least common denominator from all the UI controls available on different hosts. This means that AWT is having very limited set of controls and even lacks the generally required components nowadays like Tree, Table etc. If any application needs any of these components, it needs to be develop from scratch. That is a big task to develop the components which are quite commonly required in all kind of applications.

Various positive points of AWT are:
  • It is very stable toolkit
  • It is thread safe, which means it can work well in multi-threaded environment also. 
  • It disposes the controls automatically, so application need not to worry about it.
  • UI in AWT can be created in any sequence using bottom up or top down i..e we can start from parent to children or from children to parent; in any order actually.