fulup-bzh / DatePicker

Date Picker compatible with Zurb Foundation under Angular framework.
Other
11 stars 11 forks source link

Wrap the code in an IIFE #8

Open CaryLandholt opened 9 years ago

CaryLandholt commented 9 years ago

This will eliminate the unneccessary public variable bzmDatePicker.

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

fulup-bzh commented 9 years ago

Why would you want to eliminate public function or variable ? Anonymous function do not take more memory, then do not consume time. Outside of namespace issue what is the real disadvantage ? I use public function each time I think it makes the code easier to read and maintain, but I have no religion about it.

In the case of bzmDatePicker I could have simply embedded bzmDatePicker as an anonymous function of DatePicker.directive that itself could be an anonymous of angular.module('bzm-date-picker') but what would be the advantage ? I doubt it would be easier to understand or to debug and after minimizing it does increase size of more than 10 Bytes.

Are you ready to loose some easiness of readability and maintainability for 10 Bytes ?

CaryLandholt commented 9 years ago

It's not about readability. It's about polluting the global namespace. Best practice is to avoid it.

fulup-bzh commented 9 years ago

Polluting global name space with something that is prefixed by "bzm" does not loop a top priority for me, is it for you ?

CaryLandholt commented 9 years ago

To be clear, I mean the entire file should be surrounded in an IIFE. The code is fine.

CaryLandholt commented 9 years ago

yep - should be avoided

fulup-bzh commented 9 years ago

I'm probably to old to take care about this time of coding religion :)

I like to keep my code as simple and as basic as possible. Until someone will have show me some real technical advantage to anonymous function I doubt they will come on my priority list. It is not article like http://stackoverflow.com/questions/10273185/what-are-the-benefits-to-using-anonymous-functions-instead-of-named-functions-fo this one that are going to convince me

Now if you have some reference that show real performance improvement, I would be more than happy to change my mind.

pixels4nickels commented 9 years ago

It is considered a best practice....not "religion". That seems very condescending. If you want people to use your work in a complex environment then this should be something you care about.

CaryLandholt commented 9 years ago

@fulup-bzh your link is about anonymous vs names. This is not the argument. Avoiding global variables is best practice. There are many articles on the subject.

All you have to do is surround your final code:

(function() {
  // code from https://github.com/fulup-bzh/DatePicker/blob/master/dist/bzm-date-picker.min.js
}());
CaryLandholt commented 9 years ago

I'm old too

pixels4nickels commented 9 years ago

Me too sigh

pixels4nickels commented 9 years ago

And as Cary stated, the code is good. Thanks for this.

fulup-bzh commented 9 years ago

You might be right but you still did not convince me enough to make this a priority :) This being said if one of you want to: fork, patch, test, ... I would be more than happy to merge you effort with my work.