Firefox tips and tricks

Check out these 15 firefox tips and tricks! The ones I like most are Ctrl+Tab (rotate forward among tabs) and Ctrl+1-9 (choose a number to jump to a specific tab). Enjoy!

Firefox 2

Tags:

Inspect your Java codebase with SemmleCode

This Eclipse plugin seems to be a nice tool to drill down into your company codebase. You can gather with it code conventions, metrics, styles, method name patterns, all of it using a simple object-oriented query language, classe .QL.

This is a short description of what SemmleCode can do gor you to manage your code conventions (from TheServerSide):

SemmleCode works by storing Eclipse projects in a relational database. You can then run queries to compute metrics, to find defects, to check style rules, and to navigate. For almost any frequent task, whether it involves a quality audit or a change impact analysis, there is a ready-made query that you can launch via the run menu of Eclipse. In particular there are queries for Robert C. Martin’s package metrics, and for checking J2EE coding conventions.

And about .QL:

.QL is a simple object-oriented query language for writing queries over the codebase. It is very intuitive, andtightly integrated in Eclipse with syntax highlighting, auto-completion, and continuous checking. .QL supports overriding of methods, and that is often very convenient for tailoring checks or metrics to a particular application. SemmleCode puts you in the driving seat when it comes to quality audits: when youthink of a new check, it usually takes only a few lines of .QL to implement it, and share it with other team members.

The plugin suggests us it can be a good tool to analyze our code conventions. I haven’t tried it out yet (I’ll share my thougths here when I do that 🙂 ), if you do, post your comments here.

Tags:

Method Refactoring

[info]bpfurtado has raised an important question concerning some odd practices in software programming. People who code applying the same paradigm to all computer languages, thinking the only difference between programming languages are all about their syntax and grammar. These kind of code generate a lot of bad consequences, like the one I will focus at this post.

Sometimes I see people writing methods like “doX&Y&Z”, reflecting more than one responsability inside a method. Clearly this kind of design isn’t focused on reuse and maintenance, because people who code this way don’t even know such practices, characterized in OO programming. Methods must be cohesive, that is, “a cohesive method is responsible for one and only one well-defined logical task”.

Such a method could be factored into three little private methods, named “doX”, “doY” and “doZ”. To one public method is given the responsability to coordinate the tasks between the three methods. Talking about the former approach, how can you reuse one of the computation inside the “big” method? It’s impossible. So, reuse and maintenability is better achieved in the latter approach.

It occurs that people start programming in a language without knowing the paradigm it applies. So, people need to change their mind to produce better code, otherwise real big projects will continue to suffer the same problems we are tired of hearing nowadays.

Tags:

Integrating Struts and Spring

My coworker Max has argued with me there is no relationship between Java web based frameworks, specially Struts and Spring. Well, I completelly disagreed with him when he raised this question and here I will post one of the possibilities of their integration. This alternative let you configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file. Here are the steps to apply this configuration:

Add the following XML snippet to the plug-ins section near the bottom of your struts-config.xml file:

<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”/>

Now you can configure your actions to be injected by Spring. One way you can do this is use the DelegatingActionProxy class in the type attribute of your <action-mapping>.

This way of configuration allow you to manage your Actions and their dependencies in the action-context.xml file. The relationship between the Action in struts-config.xml and action-servlet.xml is established by the action-mapping’s
“path” and the bean’s “name”. If you have the following in your struts-config.xml file:

<action path=”/employees” …/>

You must define that Action’s bean with the “/users” name in action-servlet.xml:

<bean name=”/employees” …/>

Defining your Action in a context file let you to use Spring’s IoC features, as well as instantiate new Actions for each request created. To do this, add singleton=”false” to your action’s bean definition.

<bean name=”/employees” singleton=”false” autowire=”byName” class=”org.myapplication.web.EmployeeSearchAction”>

To be more concrete, here is a complete example of an Action described in struts-config.xml:

<action input=”/index.jsp” name=”mainForm” path=”/login” scope=”request” type=”org.springframework.web.struts.DelegatingActionProxy”> <forward name=”main_page” path=”/main_page.jsp”/> </action>

And here is its version in action-servlet.xml:

<bean id=”action.loginAction” singleton=”false” autowire=”byName” name=”/login” class=”br.com.rafael.estudoweb.action.LoginAction”/>

There are more alternatives to inject your actions in Spring. If you want to know the other ones, please check out the Spring reference manual.

Tags:

Interview with Grady Booch

Interesting interview with Grady Booch, an IBM fellow who developed the first drafts of OOP and the Booch Method, which was a precursor to UML. In this interview, he explains why most companies are wrong in doing what they called SOA. Moreover, he says why UML was created and what he does now at IBM. Very nice.

Tags:

Don’t be passionate with your language of choice …

The majority of software developers have a language of choice. Some may chose their preferred language based on the paradigm it applies, others may evaluate if the language is static or dynamic, if it has weak / strong typing, nice and interesting language constructs (like closures, continuations) and other various criteria. My preferred language is Java (up to now, because I’m studying Python, and this language is becoming a good candidate to be my language of choice for a lot of reasons, but these will be part of another post :-)), because it’s a good OO language and it offers good opportunities to apply OOA/D, that is, to construct well-designed, maintainable, robust OO systems.
Therefore a problem can arise: if the developer tries to keep this preference for a specific language / platform in the organization. This can become a passion and every new project the developer is asked to give a opinion the choice will be his/her preferred language, because he/she believes it’s the best language of the world, because he/she got used to the language. If the developer participates in a open-source community and its company decides to chose a proprietary language, the fight with managers is even worse. So, always remember:

  • The language of your choice might not be appropriate to solve a specific problem. You have to evaluate other languages / platforms to achieve the best results. It’s your routine as a software developer. Perhaps other language offers a better cost / benefit than your preferred language (You can solve your problem with fewer lines of code with a different language, for example).
  • You are in a company to achieve results, not to program in Java, .NET, Python or anything else. Always have this in mind. Don’t let your passion hide your opportunities in a company.

Have you ever seen some situations like these on your development team?

Tags:

Setting up a local SVN on Linux and Windows

I was intended to create a local SVN repository to host my Eclipse projects in home. So, I decided to share the same SVN repository on Linux (a Kubuntu distro) and Windows, to synchronize my projects in an easy manner. Here are the steps to get the things done:

  1. Create a FAT32 partition to host your SVN repository. You cannot configure your local home repository in NTFS file system type because Linux can’t write at NTFS partitions.
  2. Create a SVN repository on Windows using the Tortoise utility. This tool is free and makes the repository creation very easy. If you don’t know Tortoise, take a look at the tutorial to be comfortable with the tool. You can create a local repository on any folder in your file system. It’s just click the right button and select TortoiseSVN -> Create repository here…
  3. So now your repository is created on your local file system. Now you have to download and install Subclipse (if you don’t have downloaded yet :-)), which is an Eclipse plugin to interact with SVN.
  4. When you install Subclipse, open your Eclipse platform and go to Windows -> Preferences -> Team -> SVN and select SVNKit (Pure Java). This interface allows you to interact with the repository installed on your local machine.
  5. Now open the SVN Repository Exploring perspective, click the right button and select New -> Repository Location. In my machine my SVN repository is configured in other partition (remember this partition has to be FAT32).

The dialog box starts to ask you the SVN repository URL. At home, In Windows, my repository URL is:

file:///E:/svn/projects

and at my Kubuntu distro is:

file://localhost/mnt/sda6/svn

At Linux, you can add those lines to your /etc/fstab file to mount your FAT32 partition every time the system is started:

/dev/sda6 /mnt/sda6 vfat rw,uid=1000,auto 0 0

(/dev/sda6 is my FAT32 partiton and /mnt/sda6 is where my partition’s mounting point is)

Just click Finish and try to add your first project to your local repository. If you follow those steps and things don’t work, please add comments here.

Tags:

Checked or Unchecked?

Nice post from Bruce Eckel about whether Java needs or not checked exceptions. I have to admit, I changed a lot my thougths about using checked exceptions in Java code with this post. He explains why it’s better to use unchecked exceptions instead of checked ones with some good arguments, like the possibility to swallow the exception inside an empty catch block and loose the stack trace information. The programmer is tempted to swallow the exception because he’s forced to catch it and write reams of code around the exception. Bruce also states, emphasizing the use of unchecked exceptions:

“If I want to catch the exception, I can, but I’m not tempted to swallow it just to avoid writing reams of code. If I don’t’ want to write around the exceptions, I ignore them, and if one comes up it gets reported to me during debugging, and I can decide how to handle it then. I still deal with the exception, but I’m not forced to write a bunch of code about exceptions all the time.”

I think there’s a tendency to apply checked exceptions because people are used to static type checking, people are afraid to catch errors at runtime, static type programming languages forces this. People from Python, Ruby are used to the unchecked approach, because those languages are dynamically typed, so, there’s no pain :-). Now I prefer to use unchecked exceptions rather than checked ones because I agree with Bruce’s arguments and I’m moving my thoughts forward to dynamically typed languages. And you, what do you think about using checked / unchecked exceptions? Do you have a different opinion?

Tags:

Indicating the absence of an object

Sometimes I’ve seen some Java code intended to indicate the absence of parameters, that is, code validating null parameters (every public method must have a test to check against null parameter), verifying if a certain range is respected (like date, number ranges) and other kinds of contextual validation before calling the objects’ public methods. Some validation code (written in Java) is shown below:

At class Player:

public void pickUp (Item item)
{
   assert(item != null): "item cannot be null";
   // some code here
}

The above code uses Assertions, introduced in JDK1.4, to validate the item parameter. If item is null, an AssertionError will be thrown with the message “item cannot be null”.

public void drop (Item item)
{
   if(item == null) {
      throw new IllegalArgumentException(“item cannot be null”);
   }
   // some code here
}

This above one uses an IllegalArgumentException, which is a RuntimeException, to report a null item.

public boolean addToInventory (Item item)
{
   if(item == null) {
      return false;
   }
   // some code here
}

And this above Java code snippet returns false if an item is null.

or, maybe, applying the NullObject design pattern:

class NullItem implements Item
{
}

and at Player it becomes:

public void drop (Item item)
{
   // some code here
}

The last avoids a lot of tests to see if the parameter is null, replacing the tests with an object that provides the appropriate null behavior (“do nothing” behavior).
The question is: which do you think is the best way to indicate the absence of such an object? Which do you use often? Tell me if you use another approach to report this.

Tags:

Open sourcing Java with GPL license?

CRN is announcing that Sun is set to put JavaMe and J2SE under the GPL license. Last months Sun commented about putting its Java plataform under a public license, but they were deciding which license to put Java into. Now the preferred choice is GPL. I think it’s to promote and increase Sun’s reputation in the open-source community. However, as the new said, a GPL license would require those developers making changes to the core Java platform to freely release their code. What do you think about this? Has Sun made the right choice? And what about Apache’s license?

Tags: