1

Using more than one nested if/else makes the logic bloated.

How can I simplify the following code.

The more than one nested if/else format and logic is pretty ugly.


payload_dict = {"process": None, "status": None, "taskId": task_id,
                "jenkinsJobName": job_name, "msg": None}

if srv_result == "failed":
    if result:
        logging.info("srv start failed")
        response_data = {"errorMsg": "failed", "errorCode": "2", "serviceList": result}
        _msg = json.dumps(response_data, ensure_ascii=False)
        payload_dict.update(process=1, status=0, msg=_msg)
        logging.info(f"Requests:{payload_dict}")
    else:
        logging.info("srv start failed")
        response_data = {"errorMsg": "failed", "errorCode": "2",
                         "serviceList": "srv start failed"}
        _msg = json.dumps(response_data, ensure_ascii=False)
        payload_dict.update(process=1, status=0, msg=_msg)
        logging.info(f"Requests:{payload_dict}")
else:
    if result:
        logging.info(f"failed srv:{result}")
        response_data = {"errorMsg": "failed", "errorCode": "1", "serviceList": result}
        _msg = json.dumps(response_data, ensure_ascii=False)
        payload_dict.update(process=1, status=0, msg=_msg)
        logging.info(f"Requests:{payload_dict}")
    else:
        logging.info("deploy success.")
        response_data = {"errorMsg": "sucess", "errorCode": "0", "serviceList": result}
        _msg = json.dumps(response_data, ensure_ascii=False)
        payload_dict.update(process=1, status=1, msg=_msg)
        logging.info(f"Requests:{payload_dict}")
1
  • 1
    Refractor it into methods handling results. The problem here are not nested conditions, but repetitive code. Commented Aug 2, 2020 at 10:10

1 Answer 1

2

A bit improved.

payload_dict = {"process": None, "status": None, "taskId": task_id, "jenkinsJobName": job_name, "msg": None}

stat = 0

if srv_result == "failed" and result:
    logging.info("srv start failed")
    response_data = {"errorMsg": "failed", "errorCode": "2", "serviceList": result}
elif srv_result == "failed":
    logging.info("srv start failed")
    response_data = {"errorMsg": "failed", "errorCode": "2", "serviceList": "srv start failed"}
elif result:
    logging.info(f"failed srv:{result}")
    response_data = {"errorMsg": "failed", "errorCode": "1", "serviceList": result}
else:
    logging.info("deploy success.")
    response_data = {"errorMsg": "sucess", "errorCode": "0", "serviceList": result}
    stat = 1

_msg = json.dumps(response_data, ensure_ascii=False)
payload_dict.update(process=1, status=stat, msg=_msg)
logging.info(f"Requests:{payload_dict}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.