62 lines
1.0 KiB
Plaintext
62 lines
1.0 KiB
Plaintext
Well-named functions can allow the users of your code to understand at a glance what to expect from the function - even before reading the documentation. Toward that end, methods returning a boolean should have names that start with "is" or "has" rather than with "get".
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
public boolean getFoo() { // Noncompliant
|
|
// ...
|
|
}
|
|
|
|
public boolean getBar(Bar c) { // Noncompliant
|
|
// ...
|
|
}
|
|
|
|
public boolean testForBar(Bar c) { // Compliant - The method does not start by 'get'.
|
|
// ...
|
|
}
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
public boolean isFoo() {
|
|
// ...
|
|
}
|
|
|
|
public boolean hasBar(Bar c) {
|
|
// ...
|
|
}
|
|
|
|
public boolean testForBar(Bar c) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
== Exceptions
|
|
|
|
Overriding methods are excluded.
|
|
|
|
----
|
|
@Override
|
|
public boolean getFoo(){
|
|
// ...
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|