views:

120

answers:

6

I'm not a JavaScript guru (yet). I am trying to figure out a way to cut down the number of lines below...are there any shortcuts for lets say the if statements?

function showDialog(divID) 
{
    var dialogDiv = $("#" + divID);

    var height = 500;
    var width = 400;
    var resizable = false;

    if (dialogDiv.attr("height") != "") 
    {
        height = parseInt(dialogDiv.attr("minHeight"));
    }

    if (dialogDiv.attr("width") != "") 
    {
        width = parseInt(dialogDiv.attr("minWidth"));
    }

    if (dialogDiv.attr("resizable") != "") 
    {
        resizable = dialogDiv.attr("resizable");
    }

    dialogDiv.dialog
    (
        {
            resizable: resizable,
            width: width,
            height: height,
            bgiframe: true,
            modal: true,
            autoOpen: false,
            show: 'blind'
        }
    )

    dialogDiv.dialog("open");
}
+2  A: 

You could use:

var x = first || default;

to do the initialization. It basically checks if the first value is truthy and assign that to x if it is, otherwise assign default to x. An example from your code:

height = dialogDiv.attr('height') || 500;

replaces

if (dialogDiv.attr("height") != "") 
{
    height = parseInt(dialogDiv.attr("height"));
}
Anurag
so there's no need to ever check for empty strings in JavaScript? just check it as a boolean basically?
CoffeeAddict
that depends on your needs. if checking for things like height or width, i would usually count all these values - `null`, `undefined`, `''`, `false`, `"0"` and `0` in most cases as if it didn't exist. that may not be the case with you (for example with 0s) so you may need stricter checks. The empty string is a falsy value in Javascript. `("" == false)` is `true`, however, `("" === false)` is `false` due to added type checks.
Anurag
+1  A: 

Since you've only one statement in each if block, you can do away with the brackets surrounding it, but this is mostly aesthetic.

Danten
cool, good to know... did not know you can do that in JS just like many other languages.
CoffeeAddict
A: 

Did you mean that you want to use ternary operators?

Word of advice: Scripts are written for human, not machine. It's more important for your code to be easily readable and understood, than being pre-optimized and then you forgot what you wrote there.

Either way here's one way to shorten the if statements:

height = dialogDiv.attr("height") != "" ? parseInt(dialogDiv.attr("height")) : height;
width = dialogDiv.attr("width") != "" ? parseInt(dialogDiv.attr("width")) : width;
resizable = dialogDiv.attr("resizable") != "" ? parseInt(dialogDiv.attr("resizable")) : resizable;
thephpdeveloper
I think developers are well aware of ternary operators. If they're not, tough luck. I make an exception for those because lots of languages use them and we use them quite extensively in C#.
CoffeeAddict
so it's basically got the same turnary operator syntax as C# looks like. There is nothing wrong with that. Ternary Operators rock.
CoffeeAddict
+8  A: 

You can shorten it down a bit, like this:

function showDialog(divID) 
{
    var dialogDiv = $("#" + divID);

    dialogDiv.dialog({
            resizable: dialogDiv.attr('resizable'),
            width: dialogDiv.attr('width') || 400,
            height: dialogDiv.attr('height') || 500,
            bgiframe: true,
            modal: true,
            autoOpen: false,
            show: 'blind'
    });
    dialogDiv.dialog("open");
}

This takes advantage of the fact javascript is weakly typed, actually it kind of abuses the hell out of it, but it works.

I still find this pretty readable, but I'm used to the syntax, if you're not...decide what's readable vs what's terse and which is more important to you, there's certainly a tradeoff in many cases.

Nick Craver
what if width or height is 0?
Wallacoloo
@wallacoloo - That's the "abuse" part of the answer, if `width="0"` is an attribute or it it doesn't find the width, it'll be `400` or `500` respectively...presumably you wouldn't want a `0` height or width dialog window. Also note that `.width()` and `.height()` would always return `0` when hidden, that's why we're sticking to the attributes here.
Nick Craver
jQuery has .width() and .height() methods which are a bit cleaner than the attr() method.
Peter Gibson
ok, if it's weakly typed, then what are the default values of any variable or object in JavaScript?
CoffeeAddict
@Peter - If you look at my comment...these won't work when the element is hidden, they will return 0, since he's opening a dialog, I would wager the element is hidden. @coffeeaddict - They'll be undefined/false or 0 depending on the situation, for the first 2, `val || default` will work.
Nick Craver
we're using custom attributes which is why I'm using .attr
CoffeeAddict
basically they won't be named "width" in the div. Really we're gonna name it with some of our own custom attributes like "minWidth", etc.
CoffeeAddict
what about var resizable = dialogDiv.attr("resizable") || false;
CoffeeAddict
@coffeeaddict - I can't think of a case where you would do "or false"...if you want a bool, the first half of the statement will always be the result.
Nick Craver
+2  A: 
var height = dialogDiv.attr("height") || 500;
var width = dialogDiv.attr("width") || 400;
var resizable = dialogDiv.attr("resizable") || false;
cletus
A: 

Don't do that. You want to keep your JavaScript code as readable and maintainable as possible. If you are trying to make the download times quick and to make your JavaScript fast, then what you should be doing is creating a minified copy of the JavaScript code... not destroying your original JavaScript code. When it comes to minification and optimization, the Google Closure Compiler is the best tool for the job. To read more about the Google Closure Tools (and Compiler Service) and Google Closure Library, see the links.

Michael Aaron Safyan
I disagree and I say it's ok in this case and that developers should be reducing the lines for if statements when at all possible. I just didn't know how to do this in JavaScript. Take for instance C# and other OOP languages. Yes, JavaScript is not an OOP language but the concept of the shortcut this || this is the same exact thing we have in C# where you can do string myString = !string.IsNullOrEmpty(someString) ? someSring1 : someString2;
CoffeeAddict
minification will reduce it a lot... But ultimately reduction in lines of code + minification will have the greatest impact of reducing the page footprint.
CoffeeAddict
@coffeeaddict,the closure compiler doesn't just minify; it also performs static code analysis and generates more compact and optimized code.
Michael Aaron Safyan