 Sunday, June 22, 2008
Enforce non-instantiability on static classes
Every project ends up with one or even possibly many static helper classes. If you program in Java, I'm willing to bet the house your project has a StringUtil class lurking around somewhere. These classes are easily abused, but can serve a valid purpose, such as a factory class responsible for creating instances. If these classes contain only static methods (as they usually do), there is never any reason for them to to be instantiated. However, in both Java and C#, it's easy to forget that if you do not explicitly declare a constructor, the compiler will generate a no-arg constructor by default. The consequence is that the following code is valid: StringUtil util = new StringUtil();
You've just allocated memory unnecessarily. Plus, the garbage collector will now incur extra overhead since it will have to reclaim this memory even though it was never actually used. All of the methods are static, so there is no reason to instantiate an individual object from this class.
To avoid instantiations, make sure you declare a private no-arg constructor. This will cause the compiler to throw an error any time the class is attempted to be instantiated. public class StringUtil
{
// constructor marked private since all methods are static
private StringUtil() {}
// bunch of static methods not shown
}
A side note - If you are using C# 3.0, I recommend using extension methods to enhance the behavior of existing classes rather than creating a something like a dedicated StringUtil class. First of all, any class declaring extension methods has to be marked as static and cannot be instantiated. And more importantly, it provides a much cleaner implementation to add those helper methods directly on the affected class. The added bonus is that Visual Studio is smart enough to provide Intellisense for extension methods on targeted classes.
Sunday, June 22, 2008 9:35:25 PM (Eastern Standard Time, UTC-05:00) Back To Basics | C# | Java | Programming
 Saturday, June 21, 2008
Macs to Java developers: You Want Me
At the next Java conference you're at, pay close attention to how many of the speakers are using Apple laptops. Here's a wild-ass guess - all of them. Well, maybe you'll find the one strange dude who didn't get the memo, but I'm not sure that guy is even allowed in the speakers' lounge. Now, I sort of get it. Apple makes beautiful machines. The hardware is top-notch, the UI is slick, it's UNIX based, and, probably most importantly to most Java developers, it's *not* Windows. Here's the odd part. In case you didn't know it already, OS X uses it's own JVM instead of using one provided by Sun. And Apple is notorious for lagging behind Sun with releasing updated versions of the JVM. They only recently released a 1.6 version of their JVM a month or so ago, but only with support for 64-bit Intel-based hardware (i.e. machines bought within the last 2 years). If Microsoft tried to pull off stuff like this, they would get absolutely killed in the press and blogosphere. None of this has seemed to slow down the spread of Java developers to the Mac. Is it clever advertising? The eye candy? A status symbol? The fact that it's not Windows? I think it's kind of like the whipped guy in college who was dating the super hot girl that always cheated on him. He knew it, she didn't exactly hide it, but he could never bring himself to break up with her because she was just too hot. His options were to either put up with her cheating, enjoy his turn at bat every now and then, and walk around campus with her on his arm; or go back to the minors and date somebody more in his own league. Steve Jobs famously said, "Java's not worth building in. Nobody uses Java anymore. It's this big heavyweight ball and chain." It is easily apparent to me that Apple doesn't view Java as a first class citizen on its platform. Are other Java developers blind to this fact? Do they hold out hope that things will change? Are they willing to put up with it for other reasons? As for me, I'm a happy Vista customer. :-P
Saturday, June 21, 2008 10:02:13 PM (Eastern Standard Time, UTC-05:00) Apple | Java
 Friday, May 23, 2008
No Fluff Just Stuff recap
This past weekend I attended the No Fluff Just Stuff conference here in Atlanta. If you're not familiar with NFJS, it's a traveling conference that hits most of the major markets here in the US. It's held over weekends so as not to disrupt your daily job, and it covers topics in Java and Agility. I'd been to one NFJS a few years ago here in Atlanta and thought it was well worth it. At that time, Bruce Tate was on the circuit. He was preaching (along with many of the other speakers at the time) about the bloat inherit in J2EE applications, and that Java developers should really be focusing their attention on lighter-weight J2EE alternatives such as Spring and Hibernate. Mind you, this was long before either had become mainstream. Having been a Rod Johnson disciple after reading his first book (the contents of which would eventually become Spring), I guess it's not surprising I enjoyed the conference so much. Interestingly enough, Bruce, along with Stu Halloway and Justin Gehtland (who also spoke at that conference), are now big Ruby-On-Rails guys. Now I've been doing .Net stuff almost exclusively for the last few years, so I had a slight reservation about attending this year's conference, given that I'm not doing Java day-to-day anymore and I wasn't sure how much would be relevant. But given that I'm an independent consultant who should be up on such things, I thought it would be a good idea to at least re-familiarize myself with the goings on in the Java community. I really am technology agnostic, but I do find the language of Java to be extremely noisy, tedious, and verbose anymore, especially coming from C# 3.0. Do you know how many language-level (not library-level) changes have gone into the JVM since 1.1.7? Off the top of my head, I can remember: - the new for loop (finally in 1.5!)
- the crappy implementation of generics
- annotations
- static imports
- enums
- varargs
That's not a whole lot (or really anything significant) for a language that is over 10 years old. And in case you are not following it, there is a huge debate in the Java world if/how closures should be implemented in the Java language. Have you seen the candidates? Good lord, they make the syntax for anonymous classes look simple! Put me firmly in the camp of leaving Java the language itself good and well alone and concentrate your efforts on other languages that run on the JVM. So given the stale nature of the Java language and the rise of my interest in dynamic languages, I figured this would be an excellent chance for me to explore Groovy and Grails. I attended the sessions by Jeff Brown from G2One on both technologies the first day, and I was hooked right away on both. Groovy compiles to native Java bytecode, and thus can be used interchangeably with standard Java classes (along with the innumerable number of Java frameworks out there). Plus, it has strong support for the things I love in C# 3.0 - properties, extension methods, and closures. Grails is rapid web application framework for Groovy similar to Rails. It favors convention over configuration and uses a classic MVC architecture that allows for easy testability. Under the hood, it uses best of breed Java technologies as plumbing infrastructure (e.g. Spring and Hibernate). Just by creating a new project through scaffolding, it creates a nice project layout structure that provides a beautiful separation of concerns. It really makes it easy for you to write unit tests (no more excuses!). Basically, it creates a solid project architecture that would be roughly close to something a strong Java architect would formulate. I learned of a term from Scott Hanselman called "The Pit Of Success". The concept is that you make it easy for developers to do the right thing and perform best practices. Don't make them have to overcome the language or framework, but rather fall into success by default. That's exactly what Grails does for you. I stayed on the Groovy path the second day of the conference, attending sessions on meta-programming using Groovy. Meta-programming in Groovy is quite powerful, which makes it damn easy to build DSLs. One of the gems of the language is the simplicity in which you can create XML documents using a DSL in its XML builder. If you've ever struggled over a unwieldy ant file (or jumped through hoops just to perform a simple "if" statement in the XML file), you really owe it to yourself to check out Gant. My last day was my Neil Ford day. I attended all four of Neil's sessions the last day. The first two were on Agile project management and metrics while the last two on Ruby/JRuby. Neil is a very entertaining speaker (and Atlanta resident the 10 days a year when he is not traveling). Again, to reference Scott Hanselman, the joke is that Scott got hired on by Microsoft and now gets paid "to be Scott Hanselman". I think Neil is exactly in the same camp. He gets paid "to be Neil Ford". Where do I get the gig "to be Chris Rauber"? One last comment to make on NFJS. One thing I like about Java conferences in general and No Fluff Just Stuff in particular is that they don't seem to favor "technology" over "methodology". NFJS has a strong Agile underpinning, and offers a number of session on practicing Agile methodologies. Where it did offer technology sessions, it seemed that it targeted as how a particular technology implements Agile. For example, I mentioned in a previous post how Sun announced JavaFX at JavaOne this year (again). This was a mere two weeks before the NFJS conference. Yet there was hardly any mention of JavaFX at NFJS. If this were a .Net conference, nearly half the sessions would be about this new technology. Now, you could make the argument that JavaFX isn't really all that appealing to everyday Java developers, and truth to be told, you wouldn't get much argument from me. But I thought it was very interesting nonetheless. The bottom line - if NJFS comes to your town, I would highly recommend checking it out. They really seem interested in providing top-notch speakers and giving you your money's worth in a conference. Now ... anybody looking to hire a Groovy consultant???
Friday, May 23, 2008 12:08:33 AM (Eastern Standard Time, UTC-05:00) Conferences | Java
 Thursday, May 08, 2008
JavaFX announced (yet again) at this year's JavaOne
In a stunning move at this year's JavaOne conference, Sun announced the upcoming release of JavaFX, its framework for building Rich Internet Applications (RIAs). Stunning, you see, because they announced the same thing at *last year's* JavaOne conference.
I read the headline and thought to myself, "Hmm ... this must be some mistake." But, no, it was not. Apparently, 2007 was *not* the year of Java on the desktop, and we are forced to endure Sun's desperation pleas of, "All is well!!!" for one more year. Now, they enter an increasingly crowded field of RIA frameworks, with Flash firmly entrenched and Silverlight gaining momentum.
Java has never had a strong success story when it comes to building GUI applications, despite the ironic fact that the initial vision of Java was that its strength would be on the client-side due to its "write once, run anywhere" cross-platform JVM. The thought being you could write one client application and distribute that application across Windows, Mac, Linux, etc... Well, that vision has never materialized. Swing applications were cumbersome, buggy, resource intensive, slow (with horrendous start-up times), and let's face it, just down right ugly. Users wanted native-looking apps, and Swing was like a herd of pink flamingos poised conspicuously in the desktop front yard.
Swing has come a long way since its initial inception, but unfortunately for Sun, they haven't been able to shake users' initial perceptions. In another ironic fact, perhaps the weaknesses of building rich UIs in Java led to its unbelievable success in the late 1990s/early 2000s as a server-side language and for developing web-based applications. The browser turned out to be a better cross-platform GUI than Swing, plus it required no installation or deployment concerns.
Now, here we are in 2008, and Java is looking a little long in the tooth, even for building web-based applications. Steve Jobs famously declared Java as being dead (i.e. your act is about as fresh as a Foghat concert) as his reasoning for not including it in the iPhone. I wouldn't go that far. Java is so entrenched (especially in Fortune 500 companies) that I'd venture to say that it's going to outlive me. Ha! Take that, Steve Jobs! But as far as building next-generation UIs, as a developer I am left scratching my head as to why the world needs a Java-based RIA so late in the game when Java has never had success in that space in the first place.
Let's hope in 2009, the JavaOne crowd is not left asking, "Thank you, sir, may I have another?" as Sun announces, "JavaFX - this time we *really* mean it!".
Thursday, May 08, 2008 9:36:44 PM (Eastern Standard Time, UTC-05:00) Conferences | Java | Silverlight
 Saturday, May 03, 2008
Sort and Remove Unused Usings in Visual Studio 2008
When I first starting developing in .Net, I had been a Java programmer for many years. I remember when I first started the transition, a long-time VB developer told me how much I was going to *love* Visual Studio. It was as if programming Java in Eclipse was akin to living in the stone age and he had suddenly granted me the programming gift equivalent to fire. "Here you go ... but use this power wisely. With great power, comes great responsibility." Hey, it's not like I was using vi on an terminal emulator! (by the way, have you seen any old school guys program in vi (and still choose to do it)? It's *scary* how fast they are.) Anyway, it's 2004 and I start my era as a .Net developer using Visual Studio. Dare I say, I actually begin to *miss* some things that were in Eclipse. Example - In Java, you have "import" statements at the top of every class you write. They are very similar to "using" declarations at the top of a C# class. They basically tell the compiler what packages (i.e. namespaces in .Net) you need to compile the class. Whereas in .Net you only can include an entire namespace, in Java you have the ability to import an entire package: import java.util.*;
or individual classes within a package without having to import the entire package: import java.util.Calendar; import java.util.Date; import java.util.List;
Flash back to circa 2002 or so. I was working on a J2EE project with a team of developers. Another developer and I were working on a couple of the same files. After he checked a file in and I was going to work on it, I would do a diff on the file to see what he had changed. I noticed every time he checked a file in, along with the changes he had made, he would rearrange the import statements. Now, the usual ordering of imports in a java class are that all the java.* classes go first, then the javax.*, followed by any org.*, and finally your application classes last. The project we were working on was for the state of Georgia, so some of our application packages started with gov.*. Well, he would always move those imports above the java.* packages. I would move them back to the bottom (where they actually belonged). When he checked it back in, they would be right back at the top. I thought to myself, "Man, this guy must be really anal to go through the effort of expanding out and ordering all the imports statements that way. I wonder why."
One day I finally asked him about it. "Why do you re-order the imports statements every time you check a file in and put the gov ones first?" He looked at me with a puzzled look. "Huh?" Then he realized what was happening. "Oh, I just hit Ctrl-Shift-O every time before I save a file to organize the imports." And he showed it to me. In Eclipse, you could use that keyboard shortcut (or access it through the menu) to automatically figure out your individual class imports and arrange them via a configuration of your choice (since he hadn't set up the gov ones in any order, it defaulted them to be first alphabetically).
Wow! Pretty cool! This was 6 years ago mind you. So I got in the habit every time I saved a file: Ctrl-Shift-O, Ctrl-S ... Ctrl-Shift-O, Ctrl-S ... Ctrl-Shift-O, Ctrl-S. I quickly became a "creature of habit".
Back to starting out in Visual Studio in 2004 ... Let's see, I'd like to organize my using declarations. Ok, Ctrl-Shift-O doesn't do that. Hmm ... I wonder where that setting *is* in Visual Studio ... Looks of looking, lots of googling ... Nothing. Well, damn. That kinda sucks. You mean I actually have to manage this myself?!?!? I think to myself, "Am I the only one out there who is wanting this sort of thing???"
Time passes, I am surviving by organizing my usings by hand (barely). The more and more .Net blogs I read, I start hearing about people using and loving ReSharper. Last year, after going solo and starting my own company, I finally decided to pull the trigger and buy myself a copy. And boy, was I glad I did! Along with a bunch of other great things, I notice that it let's you sort and remove unused usings. "There it is, there it is!!!" Ctrl-Alt-O! Organize and remove usings!
Last year, Visual Studio 2008 comes out, but damn - ReSharper is not quite ready for it. Well, work still needs to get done so I need to carry on, albeit without ReSharper for the time being. I am playing around with Visual Studio 2008, and I notice a new menu option under Edit->Intellisense. Whoa, what's this? "Organize Usings". I click on it, and lo and behold, there it is - three options: 1) Remove Unused Usings; 2) Sort Usings; 3) Remove and Sort. Finally!!!
There is no native keyboard shortcut mapped to the "Remove and Sort". Well, we can change that easily enough:
There, I've mapped Ctrl-Alt-O to Remove and Sort. I'm back baby! Ctrl-Alt-O, Ctrl-S! Let's see it in action ...
Before:
And after:
The bottom line is - yes, Visual Studio is a great IDE. But don't fool yourselves .Net developers. Eclipse, IntelliJ, and even NetBeans (my preferred IDE for working in Ruby) are all very strong IDEs that in reality actually do some things (gasp!) *better* than Visual Studio.
Saturday, May 03, 2008 2:52:09 PM (Eastern Standard Time, UTC-05:00) .Net | Eclipse | Java | ReSharper | Visual Studio
|

Subscribe to this feed
 Email Me
 Follow Me On Twitter
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Search
Navigation
Tag Cloud
Archive
Blogroll
|