Test & remove, or always remove

I have a function to show or hide a certain UI control. In my example case below, the framework is jQuery I’m using for an event to show a tool tip that may fire a LOT (based on mouse movements) – but the underlying problem is more general and may also arise in a similar manner in lots of other UI frameworks. In fact, the problem could be even more generalized to any kind of component with two states to be switched often, not just UI components.

Consider:

Code A:

$(this).plothover(function(item) { 
   if(item&&item!="undefined"){
      show tooltip
   }
   else 
     remove tooltip
});

Code B:

$(this).plothover(function(item) { 
   if(item&&item!="undefined"){
      show tooltip
   }
   else {
      if(tool tip is shown){
           remove tooltip
      }
   }
});

item will exist at defined Cartesian points where datapoints exist (ie, graph coordinates on a canvas). In other words, the odds are ~10000:1 in the favor of the else condition firing on a DPI basis.

As the mouse moves,

Code A removes it with no regard to any condition
Code B only removes it if it was already created, but has to test the creation first

So, which is considered ‘right’?
Blind hide the control whether it is visible or not, or only hide it if it is visible?

I’ve benchmark’ed it through jsperf and the differences are negligible performance wise but does lean towards the blind destroy. But if it’s ‘wrong’ by theory to do it, i’d rather chose the best practice route.

— EDIT —
To clarify, The actual invocation is not important (ie, how is it removed). Nor am I looking for a code review. The above given example is one of many times I’ve run into this case.

Rather, I wish to discuss the theory involved in “Destroy what may exist” or “search and destroy if it exists”.

9

Here’s my post from Code Review I posted before the question got closed there:

Why test in the first place? When you are trying to avoid an expensive operation, one that is significantly more expensive than the test itself.

Your performance gain comes from selectively calling remove and gaining the time not spent always removing it.

So your answer is, given 2 actions, test and remove, and p the probability of the tooltip being shown, test and remove when time(remove) > time(test) + p * time(remove).

Which is exactly why how it’s being removed matters.

When there are two equal alternatives to implement something, where one needs more code and the second less (and “less code” is indeed simpler, not a “clever hack” which stresses your mind), I would typically prefer the simpler one over the more complicated. Easier to read and understand, easier to maintain, easier to change.

Of course, in a case like the one shown above, you have be sure that the API of the component you are using allows to apply “remove” regardless of the tool tip to be shown before, and that you can really leave out the test without introducing a bug (if the API design is sane, one could IMHO expect that, but not all APIs are robust). On the other hand, adding a test like if(tool tip is shown) though it is not needed shows that you did not understand the API fully and leaves you with a bad example if you are going to write a similar code snippet later again.

You have design-patterns(composite,memento,singletom …), you have principles of OOD (SRP,Dry,LSP …) and you have tactics (CJ, Paranoid, NAG …). The two examples follows different tactics.

Tactics decide if you need performance, logging or readability. They are typically specified by the senior developers or CTO.

Because you say performance is negligible and its Javascript, so we are in View of MVC and you are near to the User i would advice this NAG style (also lets use OOP a little):

function ToolTipAppearanceManager(){
  /** @param show {Boolean} <code>true</code> to let it appear, 
   *                        <code>false</code> to hide.
   */
  this.setVisible = function(show){
    if (show){
      if (tool tip is shown) {
          if(console && console.warn)console.warn('Already visible. Maybe bug!');    
      }
      show tooltip;
    } else {
      if (not tool tip is shown){
          if(console && console.warn)console.warn('Already removed. Maybe bug!');    
      } 
      remove tooltip;
    }

  }
}
var appMan = new ToolTipAppearanceManager();
$(this).plothover(function(item) {
   appMan.setVisible(item && item != "undefined");
});

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật