Qatrix icon indicating copy to clipboard operation
Qatrix copied to clipboard

global namespace clutter

Open weepy opened this issue 12 years ago • 18 comments

Hey nice frame work.

I think it's a shame to clutter the namespace with so many $xxx - it will cause collisions in some projects.

I'd consider changing it to be a single $ namespace, i.e. : $.id rather than $id

weepy avatar Apr 17 '12 07:04 weepy

  1. Global function is faster than namespaced function, because it do not have to query the namespace first and then call the function and will be better optimized for compilation on browser`s JavaScript engine: http://jsperf.com/global-function-vs-namespaced-function
  2. It will conflict with jQuery like $.ajax or others.
  3. Easier to remember and shorter the function name.
  4. Better for search engine. Try search $appned, $prepend, $ready, $hide on Google. On top right?

qatrix avatar Apr 17 '12 09:04 qatrix

  1. The test was ±0.55% which is negligible
  2. You can use jQuery.ajax
  3. No easier to remember and - only 1 character shorter
  4. No better - try searching for $.append ...

weepy avatar Apr 17 '12 11:04 weepy

On chrome, and firefox it is faster about 5-10%+

And yes, if someone want to know search the function document on Qatrix if use $.append, Google will return the jQuery as top results. But now the top of results of $append is Qatrix.

qatrix avatar Apr 17 '12 12:04 qatrix

Ah, so the naming of functions is merely for SEO purposes, instead of for best practices. Makes sense.

eliperelman avatar Apr 17 '12 15:04 eliperelman

Facepalm

shellscape avatar Apr 17 '12 15:04 shellscape

The performance is the first target on Qatrix. So we choose global but namespace.

qatrix avatar Apr 17 '12 15:04 qatrix

Trading a negligible amount of performance to violate best practices is not a valid argument.

eliperelman avatar Apr 17 '12 15:04 eliperelman

Qatrix,

Your library is getting the attention of some very notable figures in the JavaScript community. This is your opportunity to shine, or go down in flames. Please listen to the criticisms carefully, and please, please do not write bad code just for the sake of getting ranked higher on a search engine for certain keywords. Write great code, and people won't have any problem finding you.

If your library pollutes the global namespace, potentially breaking other tools I may be using, you can expect to see the community react very negatively, and this will only hurt your progress.

You've been very fortunate to receive a response by the earlier commenters - take their sentiments to heart if you want to see Qatrix do well.

jonathansampson avatar Apr 17 '12 16:04 jonathansampson

There are techniques you can use to get around the incompatibilities of jQuery or other libraries utilizing the $ namespace. For instance, you can cache the previous values, and return them to their previous owners by calling some sort of .noConflict method:

(function (global) {
    var Qatrix = function () {},
        _$ = global.$;

    global.$ = Qatrix;

    Qatrix.noConflict = function () {
        global.$ = _$;
    };
})(window);

eliperelman avatar Apr 17 '12 17:04 eliperelman

While $append indeed is the top search result, only a fool would expect that to somehow garner users. I am not greatly concerned with the global functions (who is going to use this library with MooTools, causing the $each collision, for instance?), rather, I find the $ everywhere to be onerous.

It is far better to define properties at Qatrix.each, etc. for explicitness, and a code user may choose to alias these as necessary: If I am going to use $each heavily in some loop where property lookup overhead could be a concern, I will of course var each = Qatrix.each. Or for brevity throughout, I might var q = Qatrix. This is to me far preferable to the noConflict() clobber-by-default approach that jQuery has sadly popularized.

Of note is that this project's current SEO state is actually quite poor -- search for qatrix each on Google and see that a YouTube channel is the top result even though qatrix itself is dominated by this library.

Don't optimize to avoid property access in your library code! Compilers do this for you (var q=Qatrix,e=q.each ...), and any project for which this sort of micro-optimization matters is almost certainly using a compiler already.

And great work on this library!

pigdude avatar Apr 17 '12 17:04 pigdude

You should change it to this using $ or Quatrix , Qx etc

Qatrix
Qatrix.id
Qatrix.tag
Qatrix.class
Qatrix.dom
Qatrix.select
Qatrix.animate
Qatrix.fadein
Qatrix.fadeout
Qatrix.ajax
Qatrix.json.decode
Qatrix.json.decode
Qatrix.json.isJSON
Qatrix.url
Qatrix.style.get
Qatrix.style.set
Qatrix.css.get
Qatrix.css.set
Qatrix.show
Qatrix.hide
Qatrix.offset
Qatrix.pos
Qatrix.event.add
Qatrix.event.remove
Qatrix.event.key
Qatrix.event.metaKey
Qatrix.event.target
Qatrix.ready
Qatrix.className.add
Qatrix.className.set
Qatrix.className.has
Qatrix.className.remove
Qatrix.new
Qatrix.append
Qatrix.prepend
Qatrix.before
Qatrix.after
Qatrix.remove
Qatrix.empty
Qatrix.html
Qatrix.text
Qatrix.cookie.get
Qatrix.cookie.set
Qatrix.cookie.remove
Qatrix.string.camelCase
Qatrix.string.replace
Qatrix.string.trim
Qatrix.attr.get
Qatrix.attr.set
Qatrix.attr.remove
Qatrix.data.get
Qatrix.data.set
Qatrix.data.remove
Qatrix.cache.get
Qatrix.cache.set
Qatrix.cache.remove
Qatrix.cache.inc
Qatrix.cache.dec
Qatrix.cache.flush
Qatrix.each
Qatrix.clear
Qatrix.loadscript
Qatrix.rand
Qatrix.browser

alejandrolechuga avatar Apr 18 '12 01:04 alejandrolechuga

Thank all. I will consider all suggestions and think around to make the change as possible.

But i really want to get to know the real user experience while using the framework in reality. How high the possibility of conflict will be encountered by this "namespace pollution" with inconvenient? Is it really common that to use two or three frameworks in a project at the same time and to name the variable or function name with $ as prefix?

@pigdude if the compiler will handle var q=Qatrix,e=q.each ..., why the benchmark http://jsperf.com/global-function-vs-namespaced-function show that it will be slower?

@alejandrolechuga Although it can solve the namespace problem, it can not called "less code" anymore, right?

qatrix avatar Apr 18 '12 12:04 qatrix

@qatrix, I don't think you understand what I mean. When code is passed through a compiler, only flat variables remain. the property o.p.q becomes opq.

pigdude avatar Apr 18 '12 14:04 pigdude

The new version of Qatrix can use function under the namespace Qatrix now.

And it can compatible working with jQuery with some techniques: http://qatrix.com/tutorial

qatrix avatar Jun 06 '12 23:06 qatrix

What if another code is using this name "fn" in the global scope

for (var fn in Qatrix)
{
window[fn] = Qatrix[fn];
}
window.Qatrix = Qatrix;

alejandrolechuga avatar Jun 07 '12 01:06 alejandrolechuga

@alejandrolechuga Load Qatrix framework at first. If have conflict problem, use those techniques: http://qatrix.com/tutorial

And actually in common, how high the possibility you will use $ as the prefix of common function name on your JavaScript code? If you keep that in mind, you will not encounter nothing conflict while using Qatrix.

qatrix avatar Jun 07 '12 20:06 qatrix

global functions may be microseconds faster than namespaced functions, but whats more important? - maintenance, or micosecond difference in performance? Why is require.js populate? The reason is because it keeps your dom clean. What happens when a user creates a function that happens to rewrite an existing window function? You're screwed then.

inspiraller avatar Jul 06 '12 16:07 inspiraller

@inspiraller

  1. The performance is the first goal of this framework and the global function is faster nearly 10%.
  2. If have conflict problem, just use those techniques: http://qatrix.com/tutorial

qatrix avatar Jul 06 '12 19:07 qatrix