1

I am trying to simplify the following conditional statement. Are there better way to do this? Thanks.

 if ($element.is('#resize')) {
     css.left =+ 20;
     css.top =+ 3;

     if ($('#holder .Main').length < 5 ) {
         img.css('display', 'none');
     }

     img.insertBefore($element);
     img.css(css);

 } else if($element.is('#bt_id3')) {
     css.left =+ 20;
     css.top =+ 3;

     if ($('#id .Main').length < 5 ) {
         img.css('display', 'none');
     }

     img.insertBefore($element);
     img.css(css);
 }
3
  • 3
    You may want to post this over on codereview.stackexchange.com Commented Sep 10, 2012 at 19:30
  • 1
    What do you want to simplify? Commented Sep 10, 2012 at 19:31
  • It would also help if you detailed what you were trying to do/accomplish versus just posting the code. Commented Sep 10, 2012 at 19:31

5 Answers 5

2

The only thing different between the if and the else clauses is the selector in the internal if. You could isolate that this way:

var eltId = false;
if ($element.is('#resize')) eltId = '#holder';
if ($element.is('#bt_id3')) eltId = '#id';
if( eltId !== false ) {
    css.left=+20;
    css.top=+3;
    if($(eltId + ' .Main').length<5){img.css('display', 'none');}
    img.insertBefore($element);
    img.css(css);
}

That makes it a little DRYer.

Sign up to request clarification or add additional context in comments.

1 Comment

essentially voting for my answer, as both are the same (except for eltId vs actually saving the reference, and spacing :-).
1

This does not change any workings, but it is still not too pretty. Lots of magic numbers and lack of comments and context make for weird code...

var e = false;
if ($element.is('#resize')) e = $('#holder .Main');
else if ($element.is('#bt_id3')) e = $('#id .Main');

if (e) {
      css.left=+20;
      css.top=+3;
      if (e.length<5) {
          img.css('display', 'none');
      }
      img.insertBefore($element);
      img.css(css);
}

Comments

1
if($element.is('#resize') || $element.is('#bt_id3')){
    var elem = $element.is('#resize') ? $('#holder .Main') : $('#id .Main');
    css.left=+20;
    css.top=+3;
    if(elem.length<5){img.hide();} // USE HERE THE 'elem' VARIABLE and '.hide()'
    img.insertBefore($element);
    img.css(css);
}

2 Comments

css top and left only get applied if the element is one or the other - but element may not be of either type!
One problem, Roko -- this code will execute if element isn't #resize or #id, which is not equivalent to the original code. Otherwise it's very pretty.
0

Here

$element.is("#resize") ? (css.left = 20, css.top = 3, 5 > $("#holder .Main")
.length && img.css("display", "none"), img.insertBefore($element), img.css(css)) : $element.is("#bt_id3") && (css.left = 20, css.top = 3, 5 > $("#id .Main")
.length && img.css("display", "none"), img.insertBefore($element), img.css(css))

Comments

-1

A quick re-factor yields:

css.left = +20;
css.top = +3;

if ($element.is('#resize')) {
    if ($('#holder .Main').length < 5) {
        img.css('display', 'none');
    }
} else if ($element.is('#bt_id3')) {
    if ($('#id .Main').length < 5) {
        img.css('display', 'none');
    }
}

img.insertBefore($element);
img.css(css);

1 Comment

Same problem as Roko's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.