Advertisement

Share your wisdom! Sage thoughts related to programming you've discovered over the years

Started by January 22, 2013 05:39 AM
42 comments, last by MilchoPenchev 12 years ago

Use descriptive variable names. Seriously, this is one thing I don't understand. Probably 3/4s of my job is reading code and 1/4 writing; prioritize making code easy to read.

GODS YES. QFT again!

The codebase I work on currently (dayjob-wise) is so utterly illegible thanks to shortened function names and variables that I spend 5x longer than I should have to referring back to headers for commented struct fields. It's a joy afterwards to dive back into my own project and actually read my code instead of translate it.

I can't add anything new, all my favorites and "amen" moments are already in the list.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Use descriptive variable names. Seriously, this is one thing I don't understand. Probably 3/4s of my job is reading code and 1/4 writing; prioritize making code easy to read.

QFT.

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.

Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous. sad.png

Looks like he was texting the compiler...

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Advertisement

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.

What the—

I do sometimes use abbreviations, but only those that are pretty much well-known in programming anyway (like ptr for pointer or len for length). Otherwise I don't use abbreviations. I still aim for short names, though (just because you have a huge monitor doesn't mean you have to make function names longer, it eventually becomes too hard to read just because it's too long). I noticed that a lot of long function names involve either them being too overly descriptive (whatever happened to context?) or are functions that are doing multiple tasks at the same time (which usually means there's a problem in the design).

If your "long" function name is something like draw_level_background, that's probably OK - concise but makes very clear what it does. If your function name is something along the lines of this (and I've seen some APIs do that) then we have problems =P

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.


If your "long" function name is something like draw_level_background, that's probably OK - concise but makes very clear what it does. If your function name is something along the lines of this (and I've seen some APIs do that) then we have problems =P

I'm beginning to think that I'm actually not a bad code writer.

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.

This. Never looked back.

This has come up a couple times very recently when helping others fix their code, so I'm going to say it here.

If your API functions return error codes, check them.

and for the exception-related corollary:

putting "throws exception" everywhere in your code is like throwing a psychotic mass murderer in jail and forgetting to lock the cell.

Advertisement

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.

This. Never looked back.

That only solves the writing code problem, not the reading code problem.

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.

This. Never looked back.

That only solves the writing code problem, not the reading code problem.

By that description it solves the problem, doesn't it?


This. Never looked back.


Believe me, the hideous naming scheme was the least of the problems...
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Have previously worked on a code base where the original programmer didn't like typing so everything is named using 3 letter abbreviations strung together.
Want to calculate the distance between two points? Call the ClcDstBtw2Pts method! Need to know the value of the taxable amount on account balance? It's in the txAmtAccBal variable. And that's not a typo, the 'a' in tax was felt to be superfluous.

This. Never looked back.

That only solves the writing code problem, not the reading code problem.

By that description it solves the problem, doesn't it?

It requires rewriting the code to achieve that. Good luck doing that in a huge project... You're guaranteed you will break something, and you're guaranteed you will upset everybody else working with you (and especially your boss).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement