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.
17/06/26
okay no i changed my mind.
the most important tip is folding.
fold
an algorithm is a list of instructions.
when you start, your simple code is a simple list of instructions.
code gets bigger.
it’s still a list of instructions, those instructions just became list themselves. of instructions. it’s turtles all the way down.
and you never fold?
fold my son, fold
everywhere i look… nobody folds, people use a minimap like it’s lol, split code in a bazillion files, why, why? why suffer?!
i swear i should make a code editor called “origami” where all shortcuts are fold as rehab therapy.
done for now? fold
ctrl-shift-/: fold.
ctrl-shift-p> command panel> type “fold”> “fold all”.
and the second most important advice would be: comment.
i mapped ctrl-/ to comment.
look how close it is to fold shortcut.
and then yeah carve your data or whatever, who cares.