Skip to content

Files

Latest commit

 

History

History
40 lines (31 loc) · 720 Bytes

use_to_and_as_if_applicable.md

File metadata and controls

40 lines (31 loc) · 720 Bytes

Pattern: Missing use of to___()/as___() for method name

Issue: -

Description

PREFER naming a method to___() if it copies the object's state to a new object.

PREFER naming a method as___() if it returns a different representation backed by the original object.

Example of incorrect code:

class Bar {
 Foo myMethod() {
  return Foo.from(this);
 }
}

Example of correct code:

class Bar {
 Foo toFoo() {
  return Foo.from(this);
 }
}

Example of correct code:

class Bar {
 Foo asFoo() {
  return Foo.from(this);
 }
}

Further Reading