Re: Need help on reducing cognitive complexity

2022-07-21 Thread Michael Thomas
Personally I think the code has a very weird "smell"... Assuming you don't
have any way of rewriting the calling code and are stuck with editing this
single method, I'd suggest something along the lines of:

@staticmethod
def in_circle_check(obj_type, item, uuid):
if obj_type not in ['trees', 'flowers']:
return None

# trim the trailing 's', so this can be used elsewhere...
obj_type = obj_type[:-1]

if not (plant := get_plant_with_circles(uuid)):
return None

if plant[f'in_all_{obj_type}_types']:
return True

circle_objects = plant[f'{obj_type}_circle']['items']

if isinstance(circle_objects, list):
if not isinstance(item, (list, tuple)):
items = item
else:
items = [item]

if all([i in circle_objects for i in items]):
return True

return -1


On Thu, Jul 21, 2022 at 1:19 PM Shaheed Haque 
wrote:

> The first step is to ensure you have a precise and accurate understanding
> of the code before optimization. Right now, I am suspicious of at least 2
> things in the code.
>
> First, the function returns any of {None, -1, True}. While this is
> certainly allowed, is this correct? If not, please correct the code. If so,
> then can you explain the rule that governs the return type (assuming it is
> not literally what is written).
>
> Second, as your previous respond ant hinted, the first 2 lines look like
> an optimisation attempt, except for the return type issue. Can you clarify
> if the purpose of these lines is optimisation, or not?
>
>
>
>
> On Thu, 21 Jul 2022, 06:53 Sencer Hamarat, 
> wrote:
>
>> Hi everyone,
>>
>> I have a code block with high cognitive complexity below:
>>
>> @staticmethod
>> def in_circle_check(obj_type, item, uuid, item_is_array=False):
>>
>> if obj_type not in ['trees', 'flowers']:
>> return None
>>
>> plant = get_plant_with_circles(uuid)
>> if not plant:
>> return None
>>
>> if obj_type == 'trees':
>> if plant['in_all_tree_types']:
>> return True
>> circle_objects = plant['tree_circle']['items']
>> else:
>> if plant['in_all_flower_types']:
>> return True
>> circle_objects = plant['flower_circle']['items']
>>
>> if isinstance(circle_objects, list):
>> if item_is_array:
>> for item in item:
>> if item not in circle_objects:
>> return -1
>> return True
>> else:
>> if item in circle_objects:
>> return True
>> else:
>> return -1
>>
>> return -1
>>
>>
>> I try to move the first 3 if statements on the beginning of the block
>> into new methods but that maneuver ends up with raising complexity.
>>
>> Can anybody help me to learn how to reduce cognitive complexity in code
>> blocks like this?
>>
>> Kind regards,
>> Sencer HAMARAT
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CACp8TZhBa9EbekcT1ApmzdDRVk2vCb64%3DvvXHrSawO2RJSySpQ%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHAc2jewMaP41PJZN8OVFqZ2hw41A9r8c8DDjRJc0VG%3DqP9X5g%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEdx1fpaLC4C4Lo%3DXW6-vqb4_Mk7RLOh5qa9254cKkmMEZ5obw%40mail.gmail.com.


Re: Need help on reducing cognitive complexity

2022-07-21 Thread Shaheed Haque
The first step is to ensure you have a precise and accurate understanding
of the code before optimization. Right now, I am suspicious of at least 2
things in the code.

First, the function returns any of {None, -1, True}. While this is
certainly allowed, is this correct? If not, please correct the code. If so,
then can you explain the rule that governs the return type (assuming it is
not literally what is written).

Second, as your previous respond ant hinted, the first 2 lines look like an
optimisation attempt, except for the return type issue. Can you clarify if
the purpose of these lines is optimisation, or not?




On Thu, 21 Jul 2022, 06:53 Sencer Hamarat,  wrote:

> Hi everyone,
>
> I have a code block with high cognitive complexity below:
>
> @staticmethod
> def in_circle_check(obj_type, item, uuid, item_is_array=False):
>
> if obj_type not in ['trees', 'flowers']:
> return None
>
> plant = get_plant_with_circles(uuid)
> if not plant:
> return None
>
> if obj_type == 'trees':
> if plant['in_all_tree_types']:
> return True
> circle_objects = plant['tree_circle']['items']
> else:
> if plant['in_all_flower_types']:
> return True
> circle_objects = plant['flower_circle']['items']
>
> if isinstance(circle_objects, list):
> if item_is_array:
> for item in item:
> if item not in circle_objects:
> return -1
> return True
> else:
> if item in circle_objects:
> return True
> else:
> return -1
>
> return -1
>
>
> I try to move the first 3 if statements on the beginning of the block into
> new methods but that maneuver ends up with raising complexity.
>
> Can anybody help me to learn how to reduce cognitive complexity in code
> blocks like this?
>
> Kind regards,
> Sencer HAMARAT
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACp8TZhBa9EbekcT1ApmzdDRVk2vCb64%3DvvXHrSawO2RJSySpQ%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHAc2jewMaP41PJZN8OVFqZ2hw41A9r8c8DDjRJc0VG%3DqP9X5g%40mail.gmail.com.


Re: Need help on reducing cognitive complexity

2022-07-21 Thread Mike Dewhirst

On 21/07/2022 3:52 pm, Sencer Hamarat wrote:

Hi everyone,

I have a code block with high cognitive complexity below:

    @staticmethod
    def in_circle_check(obj_type, item, uuid, item_is_array=False):


Partly because the args seem weird.

Try writing a docstring which says what the method needs to do. Then 
rewrite the code as nested if statements, bearing in mind that if 
nothing fits, it will fall through to the end and return either None or 
whatever you want to return, perhaps -1.




        if obj_type not in ['trees', 'flowers']:
            return None

        plant = get_plant_with_circles(uuid)
        if not plant:
            return None

        if obj_type == 'trees':
            if plant['in_all_tree_types']:
                return True
            circle_objects = plant['tree_circle']['items']
        else:
            if plant['in_all_flower_types']:
                return True
            circle_objects = plant['flower_circle']['items']

        if isinstance(circle_objects, list):
            if item_is_array:
                for item in item:
                    if item not in circle_objects:
                        return -1
                return True
            else:
                if item in circle_objects:
                    return True
        else:
            return -1

        return -1


I try to move the first 3 if statements on the beginning of the block 
into new methods but that maneuver ends up with raising complexity.


Can anybody help me to learn how to reduce cognitive complexity in 
code blocks like this?


Kind regards,
Sencer HAMARAT

--
You received this message because you are subscribed to the Google 
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACp8TZhBa9EbekcT1ApmzdDRVk2vCb64%3DvvXHrSawO2RJSySpQ%40mail.gmail.com 
.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fbbd1ba9-47ac-64de-88cc-5a784e151db3%40dewhirst.com.au.


OpenPGP_signature
Description: OpenPGP digital signature


Re: Need help on reducing cognitive complexity

2022-07-21 Thread Agnese Camellini
The only things that comes into my mind is taking away that first if which
is redundant with the third block.
An other way might be to include all the if in a big switch...
My 2 cents
Agnese

On Thu, 21 Jul 2022 at 07:53, Sencer Hamarat 
wrote:

> Hi everyone,
>
> I have a code block with high cognitive complexity below:
>
> @staticmethod
> def in_circle_check(obj_type, item, uuid, item_is_array=False):
>
> if obj_type not in ['trees', 'flowers']:
> return None
>
> plant = get_plant_with_circles(uuid)
> if not plant:
> return None
>
> if obj_type == 'trees':
> if plant['in_all_tree_types']:
> return True
> circle_objects = plant['tree_circle']['items']
> else:
> if plant['in_all_flower_types']:
> return True
> circle_objects = plant['flower_circle']['items']
>
> if isinstance(circle_objects, list):
> if item_is_array:
> for item in item:
> if item not in circle_objects:
> return -1
> return True
> else:
> if item in circle_objects:
> return True
> else:
> return -1
>
> return -1
>
>
> I try to move the first 3 if statements on the beginning of the block into
> new methods but that maneuver ends up with raising complexity.
>
> Can anybody help me to learn how to reduce cognitive complexity in code
> blocks like this?
>
> Kind regards,
> Sencer HAMARAT
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACp8TZhBa9EbekcT1ApmzdDRVk2vCb64%3DvvXHrSawO2RJSySpQ%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACXuh-RWFoxAk1zrtzgH0FAHD-XXMwRiDLGyiUr8ZRkhgnihfg%40mail.gmail.com.