views:

176

answers:

4
+3  Q: 

Smaller Methods

Following the guidelines given in "Clean Code" by Uncle Bob Martin, I'm trying to make my methods smaller.

One recommendation he gives is that methods that contain a trys should invoke other methods that don't involve the exceptional cases.

My problem is one of naming.

Usually by the time my method only contains the try expression, there's not much left and the name of the method perfectly describes what it does except for the exception.

What conventions do you use for naming the "non-exceptional" method that the exceptional one will call?

As an example, this is a method I'm looking at:

private void generateAndAttachDocumentFromTemplate(File templateFile) {
  try {
    File generatedDocument = generateDocumentFromTemplate(templateFile);
    if (generatedDocument != null) {
      attachDocument(generatedDocument, container);

      attachmentsPanel.reload();

      SystemControl.openDocument(generatedDocument);
    }
  } catch (Exception ex) {
    Notifier.notifyIT(App.user().getEmail(), ex);
    Dialogs.complain("Can\'t Generate Document");
  }
}
+1  A: 

You could use the "Impl" convention.

private void generateAndAttachDocumentFromTemplate(File templateFile) {
try {
    generateAndAttachDocumentFromTemplateImpl(File templateFile);
} catch (Exception ex) {
    Notifier.notifyIT(App.user().getEmail(), ex);
    Dialogs.complain("Can\'t Generate Document");
}
Steve Wall
I would just call the method "doGenerateAndAttachDocumentFromTemplate". "Impl" usually implies "implementation of an interface".
matt b
Very good point. Though in my app I don't follow that particular convention, so it works.
Allain Lalonde
Actually, I like the "do" convention better. It's a good point the "Impl" convention has an already established meaning.
Steve Wall
+1  A: 

Some alternatives:

  • method > doMethod
  • method > method0

The "non-exceptional" methods should be private.

Bozho
+5  A: 

I use the convention (which I think he suggests in the book) where you have methodName and tryMethodName.

willcodejavaforfood
You can found this approach in MS .NET, i.e. Parse vs TryParse
Guido
Must have missed it. I like this. Thanks.
Allain Lalonde
So, would the method that catches the exception be called "try___" or the other one?
Allain Lalonde
I cannot remember exactly what he recommends so when I say I follow the book I lied. I use methodName to call tryMethodName as I think that reads better. So tryMethodName contains the code that causes the exception and method name deals with the exception
willcodejavaforfood
+2  A: 

anytime a method has doThisANDdoThat() is a bad method. methods should do ONE thing and only one thing. Regardless of how "small" they are.

fuzzy lollipop