dimanche 12 septembre 2021

Improve python code with many if statements

I've seen a code on a project and would like to know how to right a code with less statements.

Here is the code:

    if not self.test_done:
        phase = "Test"
    else:
        if self.need_video and not self.video_sent:
            phase = 'Video'
        else:
            if not self.screening1_made and not self.screening2_made:
                phase = 'Screening 1'
            else:
                if ( self.screening1_made and not self.screening2_made ) or not self.passed_screening:
                    phase = 'Screening 2'
                else:
                    if not self.passed_phase1:
                        phase = 'Interview 1'
                    else:
                        if not self.passed_phase2:
                            phase = 'Interview 2'
                        else:
                            if not self.passed_phase3:
                                phase = 'Interview 3'
                            else:
                                if not self.got_offer:
                                    phase = 'Waiting offer decision'
                                else:
                                    if self.accepted_offer is None:
                                        phase = 'Waiting decision from candidate'
                                    elif self.accepted_offer == "Accepted":
                                        phase = 'Accepted offer'
                                    elif self.accepted_offer == "Declined":
                                        phase = 'Declined offer'

What would be best practices to avoid nested if statements?

Aucun commentaire:

Enregistrer un commentaire