/    Sign up×
Community /Pin to ProfileBookmark

Script Layout Efficiency

I have this big script. It works great.

I know the phrase “if it ain’t broke don’t fix it” but I want to make sure that the code layout is as efficient as possible without changing the formulated output (something I’m not very good at).

Does anyone see any areas in my code that can be altered to operate more efficiently without any shift in functional behavior?

Just wondering in case it’s possible to trim the code down.

My script …

[CODE]var visual;
var bandwidth;
var live;
var so;
var audio;
var playing;

function PlayTrack(track) {
if (!video.closed && video.location) visual = video.visual;
else visual = ‘closed’;
if (track != ‘mute’ && track != ‘false’ && track != ‘webcast’) {
if (bandwidth == undefined) bandwidth = flash.GetVariable(‘bandwidth’);
if (track == ‘true’) {
if (audio != ‘off’ && playing != ‘mp3’ && visual != ‘video’) track = ‘5’;
else track = ‘ignore’;
live = ‘false’;
}
if (track != ‘ignore’) {
so = new SWFObject(‘audio/’ + bandwidth + ‘/audio_player.swf’, ‘audio_player’, ‘0’, ‘0’, ‘9’, ‘#000000’, ‘low’);
so.addParam(‘scale’, ‘noscale’);
so.addParam(‘swliveconnect’, ‘false’);
so.addParam(‘allowscriptaccess’, ‘never’);
so.addParam(‘menu’, ‘false’);
so.addVariable(‘wimpyApp’, ‘audio/’ + bandwidth + ‘/audio_config.php’);
so.addVariable(‘wimpySkin’, ‘audio/’ + bandwidth + ‘/audio_skin.xml’);
so.addVariable(‘wW’, ‘0’);
so.addVariable(‘wH’, ‘0’);
so.addVariable(‘theVolume’, ‘100’);
so.addVariable(‘bufferAudio’, ‘0’);
so.addVariable(‘startPlayingOnload’, ‘yes’);
so.addVariable(‘randomPlayback’, ‘yes’);
so.addVariable(‘serveMP3’, ‘yes’);
if (track != ‘random’) so.addVariable(‘startOnTrack’, track);
else so.addVariable(‘defaultPlayRandom’, ‘yes’);
if (live == ‘true’) so.addVariable(‘autoAdvance’, ‘no’);
audio = ‘on’;
playing = ‘mp3’;
if (visual != ‘closed’) video.close();
so.write(‘audio_wrapper’);
flash.audioOn();
}
else if (playing != ‘mp3’) {
if (playing != ‘stopped’) {
document.getElementById(‘audio_wrapper’).innerHTML = ‘*’;
playing = ‘stopped’;
}
flash.audioOff();
}
}
else if (track == ‘mute’ || track == ‘false’) {
if (playing != ‘stopped’) {
document.getElementById(‘audio_wrapper’).innerHTML = ‘*’;
if (track == ‘false’) audio = ‘off’;
playing = ‘stopped’;
}
flash.audioOff();
}
else if (track == ‘webcast’) {
live = ‘true’;
if (visual != ‘closed’) video.ShowWebcast();
else {
so = new SWFObject(‘http://player.stickam.com/stickamPlayer/173556016-310’, ‘audio_player’, ‘0’, ‘0’, ‘9’, ‘#000000’, ‘low’);
so.addParam(‘scale’, ‘noscale’);
so.addParam(‘swliveconnect’, ‘false’);
so.addParam(‘allowscriptaccess’, ‘never’);
so.addParam(‘menu’, ‘false’);
playing = ‘webcast’;
so.write(‘audio_wrapper’);
}
}
active = ‘false’;
}[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 07.2007 — You might probably make the code a little bit shorter in terms of code lines length (by using some arrays and ternary operators), but this will not make your code more efficient. I guess you may keep it as it is, if it does its job properly.
Copy linkTweet thisAlerts:
@robindeanauthorOct 07.2007 — Thanks Kor.

I've never used what you call ternary operators before and my experience with arrays is shotty at best.

You say it will shorten the code but not be more efficient ... would it become less efficient per se?

If not, can you give me a basic code example?
Copy linkTweet thisAlerts:
@DokOct 07.2007 — Your code mainly consist of if/else statements and its difficult to optimize those without having any background knowledge of the code.

One place you might improve is when you have multiple conditions in an if statement. These condistions are said to be [B]short circuiting[/B] which is best explained by example.

Let c1 and c2 be any two conditions. In the test

if (c1 && c2)

c2 will only be evaluated if c1 is true. If c1 is false we can deduce c1 && c2 to be false without knowing the boolean value of c2. So if c1 is a constant and c2 is a complex function you will benefit from evaluating c1 first.

To clarify Kor's post
[CODE]// ++ is a unary operator - it involves only one operand
a++

// + is a binary operator - it involves two operand
a+b

// ? : is a ternary operator - it involves 3 operands
a ? b : c

// This is shorthand for
if (a)
b;
else
c;[/CODE]
Copy linkTweet thisAlerts:
@robindeanauthorOct 07.2007 — Thanks Dok,

I appreciate the explainations.

I suppose I'll leave it alone because I'm not quite certain I could pull off the ternary operators successfully.

After testing many "circumstancial error opportunities" everything seems to work as-is. At least I have that much going for me.
Copy linkTweet thisAlerts:
@KorOct 08.2007 — Thanks [B]Dok[/B] for explaining to [B]robindean[/B] the ternary operator. I have a single note: using ternary instead of if/else statement will not increase the speed of the code execution. It might increase the speed of the code downloading, but with very very little, as we are talking, in your case, about several code lines less... :-)

Ternary is used to compact the code. A code with fewer lines is easier to follow, and that is all...
Copy linkTweet thisAlerts:
@DokOct 08.2007 — A code with fewer lines is easier to follow, and that is all...[/QUOTE]
At the company where I work the code conventions actually forbid this as it is to difficult to read. I like it though as I find it quite elegant to make assignments like
[CODE]var q = a ? b : c;[/CODE]
×

Success!

Help @robindean spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...