What is the right balance for logging, commenting and test cases when coding?

Viewed 158

One thing I have noticed about when I code is that commenting, logging and writing test cases takes me out of any flow I am in and slows down my coding speed. So I am wondering if I do it too much/more than necessary. If I am using a debug logger, is there such thing as logging TOO much stuff? Also, is there a way to make the output log file color coordinated so that I can easily find issues/places where the value is not what I expect? Also, should I comment everything or just certain places explaining why I do something? Finally, what should I be making test cases for? Is it every single if statement or just every function?

Take this code for example:

def parse_data(raw_):
    # Assume raw is a dict
    parsed_output = dict()
    for i in raw_.keys():
        if i["type"] == "child":
            parsed_output[i] = {"type": "Student"}
        else:
            parsed_output[i] = {"type": "Teacher"}

        if 14 <= i["age"] <= 19:
            parsed_output[i]["division"] = "High School"
        elif 11 <= i["age"] < 14:
            parsed_output[i]["division"] = "Middle School"
        elif i["age"] < 11:
            parsed_output[i]["division"] = "Elementary"
                    else:
            parsed_output[i]["division"] = "Staff"
        parsed_output[i]["name"] = i["name"]

    return parsed_output

Should I add in a logging line after every if saying that it found a Student in High School? Or should I just put a debug line saying the # of People found in raw, and one after that says the number of people found in High School, Middle School, Elementary, and staff? For test cases, should I write one for each if statement in this function, or for the function as a whole? Also, for a function like this, do you have separate files you use as "test data" and expected output? Finally, in this function, should I comment before each of the if blocks what each is looking for, or only comment before the first parsed_output to explain that the reason I created a dict at parsed_output[i] was to avoid an index error?

0 Answers
Related