Java getters/setters

25 07 2007

One of the things I dislike about Java are having to write getters and setters. In this discussion on Sun’s Developer Forum, someone was wanting to know how to auto generate getters and setters. They seem to frustrate a few folks to the point where some want to ignore them altogether:

i gave up on gets/sets about 2weeks after my lecturer introduced them to us :/

if a var can be modified, then make it public. Though adding a get/set method does provide encapsulation, it also requires more typing, bloats code and is also a fraction slower.

Sometimes gets/sets serve a purpose, but most of the time theyre just a waste of time.

Its quite funny watching a newbie programmer start writing a class, they identify the classes required attributes, then write 200lines of gets and sets before they even consider tackling the ‘hard’ bit[s] of the class :]

rob,

While I find not using getters and setters a bit extreme, I don’t like the fact that I need an IDE to auto generate getters and setters. What would really be nice is if Java could “just know” when it finds a member variable at runtime with no getter/setter, it’s implied. It’s a lot like the default constructor behaves.

For example, it should suffice to be able to write the following User class, and be able to call it’s getters and setters.
public class User {
private String name;
private int age;
}

Then I can say:
User u = new User();
u.setName(”fname lname”);
assertEquals(”fname lname”, u.getName());

If I want to override the default functionality of Java, then I can create the getter or setter like I do today.





Project Zero

4 07 2007

I stumbled on IBM’s Project Zero today. It actually looks promising for folks that want to write simple Java web applications. Yes, simple and Java in the same sentence is usually an oxymoron. Project Zero uses a layout similar to Ruby on Rails.  Project Zero allows for 2 scripting languages: PHP and Groovy. I’ve never really liked Groovy because it seemed like a half-baked idea, but in the context of Project Zero, it seems rather useful.

Checkout the simple todo demo: watch the demo.





15 05 2007

Ruby on Rails vs Java - RailsEnvy.com Commerical

Java sucks





cruisecontrol’s jira needs spam protection

20 04 2007

I was looking to see if my latest cruisecontrol problem was a bug, I stumbled on this bug entry, which is in dire need of spam protection:

http://jira.public.thoughtworks.org/browse/CC-515

spam





varargs in java 1.5

18 03 2007

Apparently while I was away, or should I say coding with one hand tied behind my back, varargs were added to Java 1.5. It’s about time.

Pre 1.5 you had to do this monstrosity:

Object[] arguments = {
    new Integer(7),
    new Date(),
    “a disturbance in the Force”
};

String result = MessageFormat.format(
    “At {1,time} on {1,date}, there was {2} on planet ”
     + “{0,number,integer}.”, arguments);

Now if you simply add 3 dots (…) to the method declaration you can get true varargs:

    public static String format(String pattern,
                                Object... arguments);

which results in a much simpler usage:

String result = MessageFormat.format(
    "At {1,time} on {1,date}, there was {2} on planet "
    + "{0,number,integer}.",
    7, new Date(), "a disturbance in the Force");

Why did it take Sun so long to add such a useful feature? It only took 5 revisions of the language
to get us this. Maybe with the recent GPLing of Java, we’ll get features faster.





python runtime class creation

17 03 2007

I needed a way to dynamically create a class at runtime in python, similar to the following Java code:
clazz = Class.forName("Image")
obj = clazz.newInstance()

python has similar capabilities:

clazz = eval("Image")
obj = clazz()

But python takes it a step further. In Java, newInstance() requires a default constructor to be present, where in python I can have any constructor. For example,

class Image:
   def __init__(self, name, type):
      self.name = name
      self.type = type

clazz = eval("Image")
obj = clazz("foo", "JPG")

Another feature is that in python I can define a class dynamically, but I haven’t tried that yet.

UPDATE
Michael Dehaan corrected my eval faux pas with the following code for loading classes. I wanted to get it formatted in the blog instead of in the comments:

# if just searching one module
# module = __import__(modname)
# or ...
for x in list_of_modules_to_search:
   try:
        classobj = getattr(module, classname)
        return classobj
   except AttributeError, ae:
        pass

if classobj is not None:
   inst = classobj(...)
else:
   # boom




Rails

13 02 2007

So I’ve been playing with Ruby on Rails lately while readingAgile Web Development with Rails. While Ruby can take some getting used to, especially for a java guy. It can be quite nice. I’ve been playing with other languages since falling off the java train, notably python and I tabled in that line-noise of programming languages known as perl.

I haven’t gone deep with any of the languages, except java. So I won’t comment yet about ruby vs the others. What I will talk about is how cool Rails is compared to java + JSF or Struts or SEAM or framework of choice. The problem with all the java frameworks is the amount of configuration you need to get anything done. For example, struts-config.xml, or checkout the SEAM tutorial not one but four! xml files. This is where Rails excels, I don’t have to edit an xml file or any file to connect my presentation to my controller. Rails uses “convention” versus “configuration”. Struts could easily have used “convention”. I mean if I created a path for my jsp that matched my action, Struts could easily have connected them. But I am forced to define the url in the struts-config.xml just to hook up the action and presentation. While I’m not saying Rails is the be all end all solution, I firmly believe that we should use the right tool for the job, the java world could learn a lot from the simplicity of Rails.





Why is maven popular?

24 01 2007

So why is it that among Java folks maven is so popular? The pom.xml is an abomination, it’s is difficult to maintain and I’ve had simpler ant build files. I know maven has dependency resolution which you can get with ant using Ivy. Maven supports multiple projects, but that can be done with ant as well.

So what is it that makes maven so popular? skeptical minds want to know.





dist-jar.sh

23 11 2006

Tonight I created a shell-script to allow our developers to add jar files to our Ivy repo. As java on Linux developers, we use rpm packaged jars (see jpackage.org).

The shell-script will take in a host and a list of rpms and/or jars. For the rpms, it will extract the jar files to a cpio archive, then copy the jars to the repo.

So for example, if you download antlr-2.7.6.noarch.rpm from jpackage and want to add the jars to your ivy repo, it’s as simple as this:


./dist-jar.sh –host ivyrepo.foobar.com antlr-2.7.6.noarch.rpm

DONE! Next step is to make this a little less dependent on our internal buildsystem so that it is useful for other ivy users.

Happy Thanksgiving everyone.





IVY phase 1 integration complete

15 11 2006

I finished integrating ivy into our project. As you know we were developing on linux using jpackage rpms which is a great way to install java on your linux box. But we hit a snag when we had to work in different branches with different jar dependencies. So we introduced ivy to our ant build process. I know “Why not use maven?” Because I don’t like it. That’s why. For an interesting comparison of maven and ivy click here.

My next goal is to create some scripts that will allow me to specify a set of jpackage rpms, and have the ivy repository populated and autogenerate the ivy.xml files in the repo.  This will ensure when we build our rpm of java code, we develop against the same versions that we will ship with.

Time for bed, been a long day.