⇤ home

my n°1 programming tip

i recently read an article titled “my top 1 programming tip ever”.

it was shit. here’s mine:

carve your data

let me explain. i often write code like this:

def read_excel(excel):
    data = pandas.read_excel(excel)
    data = data.to_dict()
    data = [datum['x'] for datum in data if datum['y'] == 42]
    return data

i know. it’s a cardinal sin. data is a dataframe, then a dict, then a list of “stuff”.

it breaks the first commandment of:

thou shalt have variables that behave predictably

and thou shalt not reuse them

wtf art thou doing?

but compare this to the alternative.

def read_excel(excel):
    df = pandas.read_excel(excel)
    dict_data = df.to_dict()
    filtered_data = [datum['x'] for datum in dict_data if datum['y'] == 42]
    return filtered_data

there is a difference.

anyone reading the first code will understand: i want my data in a certain way, and i will not rest until i get it.

i call that carving data.

and it matters a lot. i spend a lot of time doing this. but the logic flows effortlessly afterwards.

carve it.

now.