4

I have received ownership of a code base that, although very well written, uses a rather bizarre convention:

public void someMethod(String pName, Integer pAge, Context pContext) 
{ 
    ... 
}

I'd like to make the following two changes to the entire code:

public void someMethod(String name, Integer age, Context context) { ... }
  1. Opening bracket in the same line of the method declaration
  2. Use a camelCase name for all parameters of the method, without this weird "p" prefix

Can checkstyle help me here? I'm looking but I can't find a way to rename all parameters in all method signatures to something more pleasant.

javabeats
  • 1,082
  • 3
  • 12
  • 26
  • If you are using an IDE like Eclipse, you can auto-format the code based on a coding convention you have specified. I don't know if you can achieve this for the parameters' name convention though. – Laf Dec 03 '13 at 15:50
  • Just a side note: the 'weird "p" prefix' looks like [Hungarian notation](http://en.wikipedia.org/wiki/Hungarian_notation). The original programmer was probably trained to use that (probably with C/C++) and is trying to apply it to Java. – Ted Hopp Dec 03 '13 at 15:50
  • 2
    @Ted I don't think this is Hungarian notiation. The prefix 'p' seems to be used to easily identify method parameters, compared to class members, or other variables declared at the method level. – Laf Dec 03 '13 at 15:51
  • Eclipse can do a lot work for you have a look at: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm I think "Inline" will resolve the bracket issue and with "Change Method Signature" or "Rename" you can change the parameter names – deterministicFail Dec 03 '13 at 15:54
  • @Laf - You may be right. My guess was that the "p" stands for "pointer", because all the arguments are objects. (Also, OP originally had `iAge` for the second parameter, which affected my perception of what's going on.) – Ted Hopp Dec 03 '13 at 15:54
  • @Ted Oh, I had assumed he'd have used 'n' for the `Integer` parameter, but you are right, those are objects, I missed this point ;) – Laf Dec 03 '13 at 15:55
  • @Ted, exactly, that's why I edited for clarity; it's not hungarian (which I wouldn't want to keep anyway), it's... something else :) – javabeats Dec 03 '13 at 16:02

1 Answers1

3

If you are willing to use the eclipse IDE it'd offer a very handy feature for auto-formatting code:

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcodestyle%2Fref-preferences-formatter.htm

It is pretty self-explanatory and straight-forward in my opinion.


Eclipse allows for regex based search and replace operations.

Just open Search > File... there enter the following regex for Containing text:

\b[p]([A-Z][a-z]+)\b

And tick both Case sensitive and Regular expression. Then press Replace...

In the newly popped up window enter

\1

in the With: field and tick Regular expression.

Edit: Sadly in its current version Eclipse does not support the \L flag for content groups so you are still stuck with an uppercase leading letter.


To answer your question about checkstyle: No, checkstyle is a tool used for analyzing code not for changing.

Using checkstyle to format code (Question from Oct'12)


Also did some research, here's another stackoverflow question aiming at the practically same. The solution offered there is similarly work intensive.

Can I automatically refactor an entire java project and rename uppercase method parameters to lowercase? (Question from Oct'10)

Community
  • 1
  • 1
dot_Sp0T
  • 389
  • 4
  • 26