usaspending_api/references/v2/views/autocomplete.py
File autocomplete.py
has 297 lines of code (exceeds 250 allowed). Consider refactoring. Open
Open
from django.db.models import Case, F, IntegerField, Q, When
from django.db.models.functions import Upper
from rest_framework.response import Response
from rest_framework.views import APIView
from usaspending_api.common.cache_decorator import cache_response
Function agency_office_autocomplete
has a Cognitive Complexity of 23 (exceeds 15 allowed). Consider refactoring. Open
Open
def agency_office_autocomplete(self, request):
"""Returns a collection of agencies, sub-agencies, and offices that match the request."""
search_text, limit = self.get_request_payload(request)
# It's important to order by toptier fields so that results are deterministic between objects.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"