What's the right balance for logging, commenting, and testing while coding?

One thing I noticed about when I code is that commenting out, writing and writing test cases takes me out of whatever thread I am working in and slows down my coding speed. So I'm wondering if I'm doing it too much / more than necessary. If I use a logger for debugging, is there such a thing as logging Too much stuff? Also, is there a way to make the color of the output log file consistent so that I can easily find problems / places where the value is not what I expect? Also, should I comment on all or only certain passages explaining why I am doing something? Finally, what should I do for the test cases? 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 a log line after each one if I say what I found Student

in High School

? Or should I just put in a debug line that says the number of people found raw and one after that says the number of people found in high school, high school, elementary and staff? For test cases, should one write one for each if statement in that function, or for the function as a whole? Also, for such a function, do you have separate files that you use as "test data" and the expected output? Finally, in this function, should I comment before each of the if blocks what each one is looking for, or just a comment before the first parsed_output

to explain that the reason I created the dict in parsed_output[i]

was to avoid an index error?

+3


source to share





All Articles