Ugh! Your shorthand is too short!

Started by
42 comments, last by l0calh05t 10 years, 1 month ago

Unicode identifiers, FTW.

At least as long as you are developing on a Mac (after all these years, I still have no idea how to type greek letters on a PC keyboard).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

I keep seeing it happen, and the problem is that some people actually believe this is optimizing code. The problem becomes code readability, which hurts in the long run, especially as other developers get on with the project.

I don't think that shortcutting var names really optimizes the process. To be frank, we spend a lot more time reading code than writing code, so we should optimize readability, no ease of writing it. Just my 2 cents.


To be frank, we spend a lot more time reading code than writing code, so we should optimize readability, no ease of writing it.

Plus, if there is one thing modern compilers are good at optimizing away, it is variable names :)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I knew this would happen. I am starting to see why people try to keep their code as short as possible. I mean, when you are writing hundreds and hundreds of lines of code, it gets annoying typing in long names.

Now, in the event of a tutorial, I think names should be longer and more descriptive. I actually think names should always be descriptive anyhow (short or not). Perhaps the reader doesn't have to get it, but the writer should. Now, if you are not maintaining the code, then you probably want to make them descriptive.

Or what if you want to refer to the code some time in the future? Will you recall the meanings of the variables then if you perhaps want to modify it a bit?

I am trying to create a naming convention so that I can keep my stuff consistent throughout, yet easy to maintain. Certainly when doing math functions and iterations it is reasonable to use single letter variables.

This post was a result of me browsing through someone else's code for a small GUI they made using lua. I just need to be more modular perhaps.

They call me the Tutorial Doctor.

I've found that a lot of code is written by mathematicians or physicists, and on paper the formulas all have one-letter-variables, so they will copy that one-to-one when they implement it in code.

For instance, an electronic engineer will know that the formula for calculating a charging voltage of a capacitor in an RC circuit is:

U = U0 * (1-e^(-t/(R*C)))

So in code, I will reflect exactly that:


double U = U0 * 1-pow(e, 0.0-t/(R*C));

Every non-electronic engineer is going to look at that and go "WTF IS THIS CRAP".

I've had moments where I've done the same thing at first glance, but as soon as I research the algorithms being used, the variable names begin to make a lot of sense.


I mean, when you are writing hundreds and hundreds of lines of code, it gets annoying typing in long names.

One word: auto-complete.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

Yeah comet, that is what I saw. Even for variables that aren't math formulas I see why such short names were used, and I have started to do the same myself. I am still going to stick with doing longer names for tutorials.

I don't think I have auto-complete, as I am using a plain text editor, without the help of a big IDE. Sometimes I do use Notepad++ which might have auto-complete, but mostly I use Scite.

They call me the Tutorial Doctor.


For instance, an electronic engineer will know that the formula for calculating a charging voltage of a capacitor in an RC circuit is:

U = U0 * (1-e^(-t/(R*C)))



So in code, I will reflect exactly that:

double U = U0 * 1-pow(e, 0.0-t/(R*C));

Every non-electronic engineer is going to look at that and go "WTF IS THIS CRAP".

The electrical engineer might wonder though why you wrote 0.0-t/(R*C) instead of just -t/(R*C) (is -0.0 an issue for the pow function you are using?). And the computer scientist might wonder why you are using pow(e,x) instead of exp(x) which is usually faster. And both might notice you forgot an opening parenthesis wink.png In any case it wouldn't hurt anyone to write

double voltage = starting_voltage * (1. - exp(-time/(resistance * capacitance)));

Instead, as then both the electrical engineer and the computer scientist would probably have a rough idea what its about, at the cost of only a few keystrokes (which tend to be fairly cheap wink.png).

This topic is closed to new replies.

Advertisement