Sign in to follow this  
irreversible

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

Recommended Posts

alnite    3436

 

"Always document your code."

 

- alnite, 2013

 
 
If you don't understand why, you will sooner or later.

 

 
Code documentation is overrated and violates DRY. If your code needs documentation, it's not simplified enough. ;)
 

 


Code shouldn't need documentation in order to make what the code does understood. Code very often does need documentation of why a particular approach was taken instead of other, competing approaches.

 

Keep in mind, I'm not talking about commenting code, which is a whole different thing.

 

If I have to read code line by line to know what it does, then I know I have failed in writing a reusable code.

Share this post


Link to post
Share on other sites
HostileExpanse    120

"If you're going to be writing cryptographic code, and you're not sure what you're doing,

just put the keyboard away and ask someone qualified to write it for you. For all our sakes."

 

                                                                                                 - Bacterius, 2013

 

Or, really, the more general statement - if your code is going to be used in situations where it failing could have very real consequences, don't write the code if you cannot assume said consequences. Seriously, the Dunning-Kruger effect is omnipresent in the field of programming, and not taking it into account is a recipe for disaster. See the recent utter failure of the Mega file sharing website's launch - the developer had no clue what he was doing, and the result is painfully obvious and predictable.

I'm writing cryptographic code and I'm not 100% sure what I'm doing. Should be interesting LOL

Share this post


Link to post
Share on other sites
NetGnome    773

[quote name='alnite' timestamp='1358908519' post='5024569']
If I have to read code line by line to know what it does, then I know I have failed in writing a reusable code.
[/quote]

 

Agreed. What code does is different than how code does it smile.png

Share this post


Link to post
Share on other sites
way2lazy2care    790

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.

Share this post


Link to post
Share on other sites
ChaosEngine    5185

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

Share this post


Link to post
Share on other sites
BCullis    1955

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.

Share this post


Link to post
Share on other sites
TheChubu    9452

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...

Share this post


Link to post
Share on other sites

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

Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites
szecs    2990

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.

Share this post


Link to post
Share on other sites
irreversible    2860

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.

Share this post


Link to post
Share on other sites
SeraphLance    2603

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.

Share this post


Link to post
Share on other sites
way2lazy2care    790

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.

Share this post


Link to post
Share on other sites
irreversible    2860

 

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?

Share this post


Link to post
Share on other sites

 

 

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).

Share this post


Link to post
Share on other sites
irreversible    2860

I'm not trying to endorse the software (though I personally find it invaluable) but 1) refactoring really is much less of a pain than you would think with VAX (which, at the end of the day, might be a better option if you're stuck with a truly cryptic codebase) and 2) I was not implying it makes code easier to read, but rather that if you're going to be abusing long names in the code, always use as much itellisense/autocomplete functionality to start with. That's what it's there for. The prod wasn't at ChaosEngine, but rather the person who wrote the code.

Share this post


Link to post
Share on other sites
SuperVGA    1132

My grandfather once said

 

"The time taken to think something through before doing it is often well spent."
                                                                                   -Jørgen Jørgensen

I've yet to prove him wrong.

I never come up with anything clever to say. On purpose, at least. smile.png

Aside from perhaps...

 

    "Always listen to Bregma." biggrin.png

Edited by SuperVGA

Share this post


Link to post
Share on other sites
ChaosEngine    5185

I'm not trying to endorse the software (though I personally find it invaluable) but 1) refactoring really is much less of a pain than you would think with VAX (which, at the end of the day, might be a better option if you're stuck with a truly cryptic codebase) and 2) I was not implying it makes code easier to read, but rather that if you're going to be abusing long names in the code, always use as much itellisense/autocomplete functionality to start with. That's what it's there for. The prod wasn't at ChaosEngine, but rather the person who wrote the code.

 

I didn't take it as a dig at me, and I'm familiar with VAX; it's a great tool.

 

But in this case, the goal was to replace the legacy C++ code base with new functionality written in C# (with unit tests, yay!) so any refactoring effort on the C++ ultimately wouldn't have been worth it.

Share this post


Link to post
Share on other sites
MilchoPenchev    1178

I'm just going to throw this out there because I've suffered a lot from it:

Don't copy and paste code!

a) Common and large enough code should be in a function

and 

b) Copy-pasting will lead you to copy your mistakes, cause you to forget to rename variables and generally be bad for you

 

It's 3am here, and I just realized that the reason my code crashes at runtime is two lines that I copy-pasted but had forgotten to rename one of the parameters accordingly. F*ck me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this