Archive for programming

Add all items in an array to a collection

So I wanted to add all the items in an array to a collection in Java easily. A look into the util package documentation offered a solution which I now share.

yourCollection.addAll(java.util.Arrays.asList(yourArray));

The collection can be any object from the collections framework (List, Set, Queue) and yourArray is the array with the elements to be added.
If you are using generics, the component type of the array has to be a assignment compatible with the type of the collection. In other words, if your list is something like List<CharSequence>, then you can add an array of type StringBuffer[], String[], or CharSequence[], but not Long[].

Comments

A Comment on Comments

A mighty fine day it is today. The sun is shining bright, not a cloud in the sky. Ignore the slight smog and it’s a perfect day. Then you start to hack some code. But what the h-e double hockey sticks does it do?

Now, properly commented code allows you to just scan through a file and you immediately see what it does. No need to trace functions in your head or a handy scribble filled piece of paper (like yours truly). A few lines read in a 200 lines per file and everything is clear. If it’s not working then that’s a whole different affair, but at least you know what it’s supposed to do.

It’s a habit. It is also a courtesy to those working in the same project. It is, in fact a necessity. And it is not that difficult. Write a function, make a comment. A particular conditional spans many lines or has complex conditions? Write a comment. Long blocks of nested statements (loops, conditionals) end, say which one ended please.

Who are comments for? Well, I like to say it is for those who don’t know what the code does, including yourself a few weeks from the time you write it. So you are doing a favour to yourself as well. There’s no excuse to slack off either. Even if a deadline is looming and you must type ‘em fast and furiously, there is a very good chance that someone will come back to it. Code written in express mode is most prone to mistakes after all, and one line of

# this function goes fetch data to x file

is not likely to make you late, right?

There’s another type of quick, in place, code documentation I was almost forgot: naming. That’s the naming in variable naming, method/function naming, any word you use to refer to some object or action in the code. If it is the area in a web page that contains the log-in fields, it’s the loginPane, loginDiv, loginWhatever, you get the idea. But what exactly are Panel1, Section42, and Button6? I have no idea. I would have a very good idea of what buttonSubmit and textPassword would do. In fact the search function of any half decent text editor can be used to search for password and one of the matches would be the password field. Who would search for TextField45? Sensible name selection helps everyone.

I can’t tag all the blame of bad naming on the developer or designer, though. Sometimes some IDEs make it too easy to just place elements in a drag-n-drop fashion and assigns names in the same fashion as the previous paragraph’s examples (I’m looking at you Microsoft Visual xx series). So be careful with those, especially if one person places widgets and another one codes them separately.

It’s simple and quick, so no excuses. Do it for yourself, your peers, do it for the children. Use comments, otherwise the chicken will get you.

Comments