o9s
is a microblog
home archives search feed blogroll


Methods With Lots Of Parameters

What do you do when a method has a lot of parameters? There are a million ways to do it. I’ve used code like this in the past.

public int myMethod(MyObject myObject, MyObject myObject2,
  MyObject myObject3, MyObject myObject4, MyObject myObject5) {
  // do stuff.
}

Here’s the code style I am planning to use from now on.

public int myMethod(
  MyObject myObject,
  MyObject myObject2,
  MyObject myObject3,
  MyObject myObject4,
  MyObject myObject5
) {
  // do stuff.
}

Every parameter has its own line. You don’t need to decide where to insert new lines based on some arbitrary visual-based feeling. It also makes it easy to read.

) { acts as a separator between the signature and the body of the method. If you put ) { next to the last parameter, the signature and the body will start at the same indent level making it harder to discern which is which. Not good.

Posted on 2018-03-12   #java  






← Next post    ·    Previous post →