Str Prefix Suffix
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: str-prefix-suffix
Section titled “Sourcery refactoring id: str-prefix-suffix”Description
Section titled “Description”Replace explicit str prefix/suffix check with call to startswith/endswith.
Before
Section titled “Before”name[:8] == "Sourcery"name[-8:] == "Sourcery"name.startswith("Sourcery")name.endswith("Sourcery")Explanation
Section titled “Explanation”When we read code like name[:8] == 'Sourcery' it takes a moment to understand
exactly what it is doing. Instead when we read name.startswith('Sourcery') or
name.endswith('Sourcery') it is immediate clear what the intent of the code
is. Additionally we don’t have to manually count the length of the prefix and
check it is the same as the slice end; if we ever change the prefix we are less
likely to make a mistake.