Allain Lalonde
Allain Lalonde

Reputation: 93348

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");
  }
}

Upvotes: 4

Views: 774

Answers (4)

user177800
user177800

Reputation:

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

Upvotes: 3

willcodejavaforfood
willcodejavaforfood

Reputation: 44073

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

Upvotes: 6

Bozho
Bozho

Reputation: 597124

Some alternatives:

  • method > doMethod
  • method > method0

The "non-exceptional" methods should be private.

Upvotes: 1

Steve Wall
Steve Wall

Reputation: 1932

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");
}

Upvotes: 1

Related Questions