Wednesday, April 30, 2008

AGILE does not exist

Ok, kind of a lame post today, but everyone around is talking AGILE and just a few actually know what they're talking about, so this post is for who's secretely ready to admit they know nothing about AGILE (don't worry, you don't have to admit it).

The following bullet list sums up the GOALS of agility:
  • If your requirements change you bend (and you do it in a robust and timely manner), but you don't crack
  • You are able to constanlty improve desing and architecture without major delays
  • Give the customer what they need at the end of the development process, not what they asked for at the beginning (unless it's the same thing)
  • Do not burn people out while accomplishing previous goals

If you'd like to know HOW you'll be able to achieve all of this you might wanna have a deeper look at AGILE methodologies, bad news is there's around 1000 of them out there; they're basically fishing from the same AGILE pool of which they share the core. Talking about the core, it's easy to give a list of popular practices that can help to achieve the goals above, but how to use'em and which you should use and which not is up to which methodology you choose to adopt (you don't need to pick one, you can always create your own picking and mixing from the pool of AGILE practices, everyone seems to be doing that); just to name a few, XP is probably the trendiest, but the ICONIX process methodology is gaining lots of worshippers around, so keep an eye on it.

Anyway, here a list of popular AGILE practices, just to give an idea (random order):

  • Test stuff like crazy - Test Driven Development (TDD) paradigm lives on this
  • Involve your customer as much as possible
  • Release like crazy
  • Integrate your code like crazy
  • Collective Responsibility (everyone knows about everything - in theory)
  • Pair Programming (and pray your buddy is not an asshole)
  • Optimize team communication (put buddies near to each other)
  • Keep iterations short

Hopefully now it's clearer wtf AGILE means, but I wouldn't be surprised if not. Truth is AGILE doesn't exist.

Wednesday, April 23, 2008

[Java, SWT] Invalid thread access


Problem: You have your own GUI made with Eclipse SWT, you try to update a widget from within an external thread, and you obtain the exception:
org.eclipse.swt.SWTException: Invalid thread access

Solution: SWT is not thread safe, meaning that if you try to modify a widget from a thread you previously runned external from the one that contains the GUI, the JVM ends the application. Actually Swing is neither thread safe, but you won't receive an exception (and your application behaves in an unpredictable way).

Obviously there is a way to handle this problem, using the .asyncExec(Runnable r) or .syncExec(Runnable r) methods of the Display SWT class: passing a valid implementation of a Runnable class, SWT will allow GUI accesses as soon as possibile. This code is a way of using this pattern:

public class MyGUIClass{

private Display _display = null;

public static void main(String[] args)
{
/* main thread */
final Display display = new Display();
/* this allows a future catch of this class */
_display = display;
final Shell shell = new Shell(display);
final Button button = new Button(shell,SWT.PUSH);
button.setText("Run a thread");
button.addSelectionListener(new SelectionListener()
{
public void widgetSelected(SelectionEvent e) {
/* this class starts a new thread */
MyComputationClass mcc = new MyComputationClass ();
mcc.createNewThread(this);
}

public void widgetDefaultSelected(SelectionEvent e) {}
});
}

/* this main class exposes a public method that returns the
current Display class
*/
public Display getDisplay()
{
return _display;
}

The MyComputationClass in the .createNewThread(MyGUIClass aGUI) will try to update the GUI (from a separate thread). Inside the thread this is the correct code to make SWT dispatch your code:

public class MyComputationClass{
public void createNewThread(MyGUIClass aGUI)

/* a new thread is running */
/* make intensive computation . . .*/
/* . . . */
if(!aGUI.getDisplay().isDisposed()){
aGUI.getDisplay().asyncExec (new Runnable ()
{
public void run () {
aGUI.doSomeGUIModifications();
}
});

/* . . . */
/* thread is ended up */
}
}

This comes from a process of butchering my own Java GUI, so I'm not sure if it is the only way to do it. Anyway, like my dear old friend Giovacchia uses to say, "Don't try to be a perfect coder, just be a perfect butcher!".
See ya!

Monday, April 21, 2008

[ASP.NET] Migrate Web project From VS2003 to VS2005

You're still developing an ASP.NET application with Visual Studio 2003 (also know as Visual Studio .NET)? Well, you're not alone, but as much you want to keep it that way 'cause you "don't really need to migrate", sooner or later some creep like me will convince your boss that for portability, scalability and maintenability purposes you will better upgrade at least to 2.0 (I just pulled this trick in my company). So, if you wanna be prepared, read this article.

So, starting from a cheap but effective VS2003 web project, which are your options?
Basically 2:
  1. Upgrade to VS2005 WebSite Project

  2. Upgrade to VS2005 Web Application Project

First of all you better be sure to install VS2005 Service Pack 1 from this link: http://www.microsoft.com/downloads/details.aspx?FamilyId=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&displaylang=en . The service pack will install for you the VS2005 support for Web Application Projects (and fix an undetermined number of bugs).

A little history: Basically when VS2005 was released MS got rid of the Web Project model the way it was structured in VS2003. They introduced the Web Site, with a number of cute ASP.NET folders for Application Code, Resources, References, and other well structured nice crap. Nice as the Web Site project was, a whole lot of people started complaining that their VS2003 project were often being converted into wrecked conglomerates of pseudo-working code, and again the same people brought to attention that it would've been nice to migrate their apps without being forced to change the structure of the app (reasonable complaint we must admit). Said so MS released a VS2005 update to support the Web Application project, in order to make the life easier for those poor devils who were working on the previous version of Viz (what a nice thought).

So, what's the difference between a Web Site and a Web Application project? There's a lot out there on google you can find with a simple search if you want details, so I'll cut it short.

1) The WebSite Project has arguably better structure, with those cute ASP_NET folders (can help to keep the house tidy). The Web Site project doesn't include in your codebehind files the declaration of the server controls you put on the aspx file: they're created and placed at compile-time (so the build takes longer - if you care about this details).

2) The Web Application project keeps the same structure as the old VS2003 Web Project so you can keep working as you always did, BUT if you convert the codebehind format right clicking the solution/project or single code file and the selecting "Convert to Web Application" your classes will be split (and made partial) between the usual vb/cs code file and a designer code file, the latter generated to contain all the controls declarations. You can choose not to convert the format and keep totally working as you were BUT (again) when you add controls to the aspx in design view you'll often have to manually add the controls declarations into the vb file (definitely something you can live with).

Said so, I was dealing with a medium-size web app, with a bunch of user controls. I thought the WebSite was a nice option so I gave it a shot (following these steps - http://msdn2.microsoft.com/en-us/library/ms247241(VS.80).aspx); the overall result wasn't so bad, but when it comes to user controls - believe me - you don't wanna mess with a migration to Web Site project. As said above, this kind of project gets rid of your controls declaration so you lose any control above them, and a lot of stuff gets renamed generating unbelievable messes. For instance, one of the user controls I had on the page started throwing a Null Reference Error for no reason, and I had to dinamycally create it every page load (which totally sucks). You can get it working with a few hacks, but what you have is a wrecked ship (if you can live with that... well you'll soon get the .NET Butchering Certification, no doubt about it - just ask for it and send a sample of your work, we'll post about you and your lameness).

After this unbelievable and adrenalinic experience (I stayed late @work trying to hack the thing till the point it worked but they locked me in the building and I had to stay there till 22.30 pm on a Friday - fuck me!), I gave a shot to the Web Application (following this steps - http://webproject.scottgu.com/CSharp/Migration/Migration.aspx) and everything worked smoothly untill I tried to "Convert to Web Application", when it started bitching about something being under source control (which was obviously not the case as I disconnected and unbinded the project from source safe and brutally DELETED all source safe hooks). ScottGu might rock, but he can nothing against Visual Source Safe (look at this previous post to get a clue of why it sucks so much if you really are that curious - http://dotnetbutchering.blogspot.com/2007/11/aspnet-how-to-setup-web-application.html).=

In the end I sit with the Web Application Project WITHOUT "Converting to Web Application" (it sounds ridiculous but what are you gonna do) the file format, because of the Source Safe issue. It looked like a fair deal, and - experience (mine and of others) teaches - once you get it working in the 2.0 framework it's much easier to convert to a Web Site project (if you're one of those who must have everything), but - HEY - that's another post.

Thursday, April 17, 2008

[eVC++] Breakpoints could not be moved and have been disabled

Problem: debugging embedded VC++ (most versions) code that's running on a Windows CE device you get a dialog bitchin' about breakpoints could not be moved and have been disabled .

Solution: Go to the Settings of the Project that's using the dll on which the breakpoints are. Select the Debug tab and select from there Additional Dlls, in Local Name specify the local path of the dll, in remote Path specify the path of the dll on the device:
Local Name : C:\Whatever\Util.dll
Remote Name : \WhateverToo\Util.dll

The thing was about to drive me crazy and I know for sure I am not the only one. You could get this error if you're not generating the debug information for the dll as well (in this case go to the project settings and roam till you find a checkbox saying Generate Debug Information - or something like that - and tick it). I tested this on eMbedded VC++ 4, but my guess is it's gonna work no matter what. What's tricky about this is that's almost impossible to google the Microsoft article that tells you how to fix it: http://support.microsoft.com/kb/275560/en-us
Enjoy, and be good butchers.

Wednesday, April 9, 2008

[Java, JSP] JSP for beginners: Are you prepared?


After a week or so of inactivity, we are back with a beginner tutorial about JSP (Java Server Pages).
Are you prepared? (this is a quote from a popular MMORPG).

Basically JSP is a way to use the power of Java inside usual HTML pages.
The first step is to install a capable web server, like Apache Tomcat: JSP files have to be put in the webapps directory or in a subfolder.

As you put Java into HTML files, you can insert neither one Java line of code and simply rename an html file into JSP (.jsp extension) and it will work!
Actually, when the web server open for the first time the page, it compiles it: it leads to a slower loading (only the first time), that happens each time you modify your page (even if there is no Java code), so don't change in JSP each page of your application without discrimination!

How do I embed Java code?

<HTML>
<BODY>
<%
String message = "<p>Hello, this is my first JSP page!!</p>"
String endPage = "<p>Bye bye!</p>"
System.out.println(message);
%>
<p>Above you are reading a wellcome message.</p>
<%=endPage%>
</BODY>
</HTML>


As you can see, inside the <% ... %> there is pure Java code: this is called a scriplet.
More over using <%=endPage%> you can output a java expression, that is the same as calling the System.out class: using JSP you have not to specify the System.out.print() function and call directly out.print(), where out is a global member that outputs directly.

If you need a specific Java class, you can import its package (that should be in the classpath, depending on you web server) usign a JSP directive:

<%@ page import="java.util.Date, java.util.ArrayList" %>


If you need to insert another JSP page inside the current page, in which I suppose you have a particular functionality you use frequently in several other pages, you can import it:

<%@ include file="hello.jsp" %>


In order to interact with your HTML controls, you use the current request object (that comes from the JSP context) and as for session variabiles you'll use the session global variable:

<%

String action = request.getParameter("submitted");

if( action.equals("true") )
{
/* saves a variable as a session variabile:
* from now on it is always available */
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
}


if(session.getAttribute("theName") == null)
{
%>
<FORM METHOD="POST" ACTION="thisPage.jsp">
What's your name? <INPUT TYPE="TEXT" NAME="username" SIZE="20">
<INPUT TYPE="HIDDEN" NAME="submitted" VALUE="true">
<INPUT TYPE="SUBMIT">
</FORM>
<%
}
else
{
out.println("<p>Hello "+ session.getAttribute("theName") +" !!!</p>");
}
%>


What does it mean?
Mainly if you have already set a session variabile with your own name, so there is no need to print out a form, but just a welcome.
First of all the action string is used to store the submitted vairaible, that comes from the submission of the form with the hidden input control.

In case of submission, the action variabile is equal to true, and in that case we store the session variabile thaName with the value of the input text control named username: from now on this session variabile will be available in all your JSP pages till the end on the session (depending on the settings of your web server).

Afterwards if there is not a value for the session variable theName, than prints out the form (without using the out.println() but just as HTML content); else prints out a welcome message, retrieving the session variable theName.

There is also a way of using particular JSP tags to use common Java functionalities...but it is another story!
Stay tuned!!

Saturday, April 5, 2008

[SQL Server 2005] SQL Server 2005 hangs on "Setting [File/Registry ] Security"

Prolem: SQL Server 2005 (any edition including express) installation hangs on "Setting [File/Registry ] Security".

Solution: Either remove the network cable and restart or Go out for lunch and enjoy (it takes ages, 2-3 hours for the whole installation for big networks).

This is a very common problem that occurs often in presence of huge networks (global corporate networks are good candidates); the reason why it all happens seems to be related to some domain resolutions. The installation runs smoothly to this point, then it appears to hang for a long time, so long the temptation cancel the installation is almost irresistible. What stops you from doing it is obviously the fact that from taskmanager everything seems to be just fine. If you let it be for a couple of hours it evetually will finish the installation alright. I am at the moment not aware of other solutions rather than disconnect the machine from the network or - my favourite - go out for lunch, and make sure to have a big one.

Tuesday, April 1, 2008

[Java] Introducing GUI with Swing and SWT

These days I'm having a look at JAVA GUIs, playing with different 2D libraries: AWT, JFC/Swing, SWT and JFace.
Actually AWT is the base library for 2D issues and lacks of several common controls (see figure, taken from here).

To fulfill those lacks, the JRE comes with the Java Foundation Classes (JFC) / Swing libraries, with all kind of controls, allowing the creation of complex GUIs.
If anyway you are looking for something quick to implement, you should use the SWT (Standard Widget Toolkit) library, based upon Swing and AWT, created by IBM as the graphic constituent for Eclipse Framework (actually over SWT lies JFace, a graphic library created to support common programming features, not treated in this post).
In Swing controls are disposed automatically by the garbage collector, while in SWT you have manually to dispose them, like in the current example in which a simple window with a lable is created:
SWING

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingHelloWorld extends JFrame {

public static void main(String args[]) {
new SwingHelloWorld();
}
SwingHelloWorld() {
JLabel jlbHelloWorld = new JLabel("Just a label");
add(jlbHelloWorld);
setTitle("Example Frame");
setLayout(new GridLayout());
this.setSize(300, 100);
setVisible(true);
}
}

SWT

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SWTHelloWorld {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Example Frame");
shell.setSize(300,100);
shell.setLayout(new GridLayout());

/* controls code goes here */
Label label1 = new Label(shell, SWT.BORDER);
label1.setText("Just a label");
label1.setSize(100,20);

shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}

As seen in Swing the main class is inherited straight from the JFrame Swing class, while in the SWT version there is the Display object that is a repository of display OS-dependent features and a Shell which is the actual window: you can have several shells for one display. Moreover in the SWT code there is a while loop, which waits for the disposal of the shell (done by the close button [X]) and in that case the main class disposes the Display instance (actually the disposal of all objects is done automatically by the container's dispose event, i.e. by the disposal of the shell).

Another difference between the two libraries is the graphical look:


SwingSWT





It is clear that SWT library depends on the OS look (thanks to the Display object), while Swing uses typical JAVA interface.

If you are planning to learn basics of JAVA GUIs, try first with Swing and than switch to SWT, so you'll gain a wide view of those technologies and learn for each application the one which suites your needs.

Hope JohnnyIdol learned something... he just got served.

Stay tuned!