tags:

views:

386

answers:

7

I am looking for naming guidlines for non functional (utility) extensions of the common classes.

In C# I used to use some of this:

1) ListExtensions, too long but with extension methods ListExtensions never appears. Usage is just. var productList = new List(); productList.MyExtendedMehod();

2) ListHelpers. This was in old C# 2.0 era, usage is ListHelpers.MyExtendedMethod(productList );

Now I am diving with java. What I have learned is Collections, Arrays classes. So how do you name your helper classes. CollectionUtils? Collections? CollectionsEx?

+3  A: 

I think XXXUtils is the common way. Then you can use static imports to get rid of the class name.

class FooUtils {
  public static foo(int x) { ... }
}

static import FooUtils.foo;
foo(5);
egaga
Yep, my internal c# guy do not like this, but java guy became more and more strong :). So I will stick with this naming. Thanks.
Mike Chaliy
I'm generally not a fan of static imports. It tends to make code less readable since, if overused, it can become non-obvious where a function is actually defined just by looking at the code.
cletus
@cletus: I agree. Checkstyle does have a check for that ( http://checkstyle.sourceforge.net/config_imports.html#AvoidStaticImport ), but I still use it for "Utils" classes with static constants.
VonC
@cletus, that's true, but fortunately most Java developers use IDE's. And it's more important how the code reads. Often the Class name really doesn't give anything, only clutters the code.@VonC: I consider using Checkstyle bad style. It's more irritating than useful, since there are exceptions to the rules. And also, people have a habit of blindly believing what Checkstyle says. With combination of automatic formatter, one can spoil the whole codebase in one night.
egaga
+3  A: 

Check out the Apache Commons Collections library for some naming ideas. Maybe this library does a lot of things you want to do anyway!

A_M
Thanks, helpfull, good direcection.
Mike Chaliy
+1  A: 

1) I haven't seen it a lot, but I believe the good naming scheme is to give a functional name (SynchronizedSet, etc.). You can get examples from Apache's commons-collections or google collections.

2) The "standard" suffix is Utils - SwingUtils, StringUtils, etc.

David Rabinowitz
SynchronizedSet is acceptable for instantiable classes, but not for static utility like... Anyway thanks, seems to me I will end with xxxUtils.
Mike Chaliy
@Mike, I was answering to your case #1, like your ListExtensions
David Rabinowitz
+1  A: 

Apache Commons has set quite a standard by using XXXUtils pattern.

krosenvold
Thanks, will look precisely, such libraries are good sources to learn practices.
Mike Chaliy
+1  A: 

In Java there's a pattern of using plurals like java.util.Collections to hold global function-like static methods for the corresponding class/package.

For example,

Collections.shuffle(list);

shuffles the list.

eed3si9n
Yep, but problem where to add my methods for collections. CollectionsEx?
Mike Chaliy
Are there any other examples beyond Collections and Arrays? Most of the libraries (Apache commons, spring, some places in the JDK) use the xxxUtils naming scheme
David Rabinowitz
XXXCollections where XXX is the name of the product/company.
mP
I think the word "tradition" is misleading. I am correcting it to "pattern."
eed3si9n
@David, java.util.concurrent.Executors keeps factory methods in there, but it's not very popular.
eed3si9n
+1  A: 

I always try and use the package name in plural form. Its shorter than Util or Utils and it sounds more english like.

I believe Arrays and Collections from the JDK sound so much better than if they were named ArrayUtils or CollectionUtils.

For a train package my utils would be placed inside a class called Trains.

mP
I will try both, xxxUtils are more guidline friendly, but plural form is more netural... Feel like a buridan's ass...
Mike Chaliy
A: 

Personally I try to choose utility class names such that the clase name is part of the method name. For example

list = As.list(array);
set = As.set(list);
b = All.notNull(foo, bar, qux, ...);
name = Defaults.to(input, "Joe Random");
value = For.example(StackTest.class, "withSomeValues"); 
b = Is.empty(iterable);
Out.put("Hello, Worlds!");

et cetera...

Adrian