Best practices for condensing code

Hi All,

I’m currently learning Javascript & jQuery, I’ve gone through a little PHP in the past as I work a lot with WordPress. Now that I’m aggressively expanding my knowledge of all these I’m beginning to wonder about whether I should be condensing my code after finishing.

When learning I’ve mostly read about creating easy to read names for everything so using names like “$background_image_id” or calling functions “get_published_vimeo_category_slugs()” but when I’m writing longer pieces of code I find writing long contextual names becomes extremely time consuming and confusing. When reading through other peoples code often I find variables just called “$pid” or functions “cat_slug()”.

Is it generally a good idea after finishing coding to go through your code and shorten names/references or is it something you just pick up through experience. Some of my variables are becoming ridiculously long ($co_portfolio_value[ ‘co_client_logo_image’ ]) and when combining a bunch of these together it not only becomes confusing but also I end making mistakes as names can become quite intricate.

Is this just all a case of scope, and that with time as you come to understand more you learn when and where you can cut down, condense or reuse names, etc.

Sometimes it seems what you are taught is very different from what people actually do.

Do it as you go along. Even with find/replace functions, I’d be paranoid about something not being changed as needed.

And, honestly, with the speed of computers and the internet being what they are, today, condensing code really isn’t going to make a difference (at least not noticeable to humans) in page load.

V/r,

:slight_smile:

I read somewhere that it is far better to have very long descriptive function names and variables because it makes debugging so much easier. Debugging is where most time is spent.

The difference in a manually condensed page is negligible, would complicate debugging and I think it would be far better to investigate caching.

Also investigate perceptual page loading where the page framework is virtually display instantly and images, etc are progressively loaded.

I prefer using names like
longDescriptiveFunctionName and long_descriptive_variable_name
as I have trouble easily remembering what lDFN and l_d_v_n mean.

IMHO keep your development code readable, then use a minification tool to “condense” it, and use the “min” version in your pages.

Thanks guys, it’s interesting to hear things from a practical perspective too.

I’ve been doing as taught with descriptive names but learning alone means I always have a few niggling “am I really doing this right?” questions.

I think my issue is understanding planning, a result of my “early days”, I tend to realise I need a related/similar sounding variable on the fly. I’m guessing the more I do, the more I will be able to plan and understand.
Plus my examples are normally from where I have to edit “premium” WP themes or plugins.

@Mittineague - I think I will take a look at a minification tool, I’ve umm-ed an ahh-ed it for a while, may aswell just get my hands dirty :slight_smile:

On the server-side if the length of variable names has meaningful performance implications then you probably need to start looking for another language to solve that problem – just about any compile / processor pretty much strips them on the 1st pass.

Outside of the time it takes to transfer the bytes they probably don’t matter much to modern JS interperters either. If you are gzipping the file already (mostly on by default in 2014) then you are probably not paying that much overhead even in the transfer of the file – the first thing gzip does is grab long, unique strings and encode them to much shorter things.

I prefer long names but it’s really matter of preference of a project lead’s coding standard. I would never refactor just to rename variable names.