A Few Words About Naming – Methods (In Progress)

Table of Contents

Note: This article is a work in progress!

Maybe the topic seems trivial and worn-out, as everyone knows that you need to create clear, unambiguous names. However, it’s still a greatly neglected area. Teams are still far from understanding that the most depends on naming. No refactoring has as much power as changing a name. It is primarily the names, if used correctly, that form what is called self-documenting code, creating a clear language in the source code of the system you are building.

The topic is described in “Clean Code”, but not as specifically as I see it, somewhat superficially and from a different perspective.

Anatomy of a Method (Function)

Today, a few words about the anatomy of a method (function). Every method has a so-called signature, which includes the name, parameters, and return type, the key components from the viewpoint of refactoring, because readability consists of all three elements – not just the name.

Moreover, it can be said that there are two types of methods:

  • Methods returning a result;
  • Methods not returning a result.

Methods Returning a Result

Note: The following rules do not apply to fluent interface types.

double computeSomeResult(int parameter1, double parameter2, int parameter3 )
  • The method name must always specify clearly and unambiguously what the method does and indicate what its result will be.
  • Always create the method signature in the context of the class it is in (do not repeat the class name in the method name).
  • Therefore, methods returning information about errors that occurred during its execution are an anti-pattern (though there is a trace number of cases when this strategy might be justified – usually when working with legacy code); many frameworks suffer from this “disease”.
  • It is highly penalized to treat input parameters as a way to return a result, which most often happens in the case described in the point above.
  • The method parameters co-create meaning with the name – they must complement it and be consistent with it.

Examples and Comments

List<Content> retrieveChildren(Content parentContent)

Okay! The name suggests that the children of the parameter will be searched for, and that’s the return type as well. More fluent versions (TBD to explain what this is):

List<Content> retrieveChildrenOf(Content parentContent)
List<Content> childrenOf(Content parentContent)

Or, if it were appropriate to the context, the method childrenOf could be part of the Content class, and then its signature would be:

class Content {
   List<Content> retrieveChildren() // ..
   // or more fluently
   List<Content> children() // ..
}
interface ContentProcessorResolver {
   //…
   /* @return returns a list of content tailored to the resolver */
   public List<? extends Content> resolve(Content content);
   //…
}

Bad name – it would suggest (in the context of the interface it’s in) that the result would be an object of the ContentProcessor class.

TBC…

(Text translated and moved from original old blog automatically by AI. May contain inaccuracies.)

Related Posts

Why Agile Fails

Introduction

Implementing a methodology from the Agile family is not at all easy. The problem usually lies in management, who upon superficially understanding what it’s all about, perceive the new method as a promise that from now on, everything will magically work better. It doesn’t matter if we have subpar team members and adhere to the principle that “any specialist can be replaced by a finite number of students.” It doesn’t matter if there’s complete disregard for knowledge management and skill development in the team because there’s never time for that. It doesn’t matter if people working on projects are shuffled between projects—after all, it’s about interdisciplinarity, and everyone should know everything.

Read More

Simple, Complicated, Complex and Chaotic Systems, in Other Words Cynefin. And How Does It Relate to Software Development?

Intro

You might have already come across terms such as complex systems, complicated systems, or complex adaptive systems; especially, if you have read Management 3.0 by Jurgen Appelo or heard about Ken Schwaber’s ideas about the applicability of Scrum. It might sound intriguing, but finding logic in it is difficult without some background theory. This is the point at which Dave Snowden’s Cynefin concept comes in handy. This concept is based on complex systems theory, anthropology, evolutionary psychology, and, last but not least, cognitive psychology.

Read More

The Hacker Way

A few days ago, Paweł Wrzeszcz sent me Erik Meijer’s talk “One Hacker Way” (watch here) from the GOTO Conference in Copenhagen. It is a very provocative talk, which is great. It questions the Scrum method and challenges the status quo in Agile. Given that Scrum is a dominant framework in software development, a critical view is healthy, especially as Agile has become a significant business machine over the past 20 years. When implementing Agile at Scale, core ideas can easily become distorted. (Check out Dave Thomas’ “Agile is Dead” talk here).

Read More