cowboy / jquery-hashchange

This jQuery plugin enables very basic bookmarkable #hash history via a cross-browser HTML5 window.onhashchange event.
http://benalman.com/projects/jquery-hashchange-plugin/
GNU General Public License v2.0
1.22k stars 258 forks source link

Overkill use of regex #13

Closed blixt closed 10 years ago

blixt commented 13 years ago

I just came across a regex when looking through the source code, and it looked so horrible, I had to fix it :) See attached commit.

cowboy commented 13 years ago

Andreas, I've got to be honest here, while I love community contributions, this is a perfect example of how not to make a pull request.

blixt commented 13 years ago

Okay sorry, I guess I should have just submitted a diff and not a pull request, but I found it to be much more convenient. And my comment was not meant to be snide, just playing on a very common topic (regexes being horrible.)

I tried to follow the code syntax in the rest of the code (I found ambiguous use of parens spacing and extraneous parens for clarity, so that's probably why there was some confusion.)

And yes, you're right, the broken code was a stupid mistake by me.

I'll close with an objective reasoning: Anyone with an understanding of basic string operations will understand code that finds a hash character and gets the substring after it. However, the regex was written in such a way that it might even be confusing to someone who understands them. If you were to use a regex at all, it would be much clearer if you were to use the following:

var match = url.match(/#.*/);
return match ? match[0] : '#';

I still recommend you to implement this change, and not throw it away in spite.

cowboy commented 13 years ago

Andreas, I appreciate the pull request, and I encourage you to fix it (so that it's not broken, at the bare minimum) and re-submit it (I'll re-open this issue).

I'm a busy guy, so it's especially challenging for me to spend time right now on contributions that need a lot of back-and-forth, but that being said, I do appreciate them. I hope it's clear that my main issues were simply that your pull request comment wasn't particularly constructive, and that your code was broken.

Either way, I'd love to replace that regexp with code that is more clear and/or performant. If you notice any other areas for improvement, please let me know. Thanks!

blixt commented 13 years ago

Yup, I'm busy too, hence the little time spent on my submission, making it messy. But it was in a way disrespectful of me, so I took the time now to do it properly. I removed the old commit and added a new one that should be better. :)

blixt commented 13 years ago

Ping :)

cowboy commented 13 years ago

I'm totally aware this issue exists, but it's a bit low priority for me right now. I've been refactoring a lot of code lately, and this is just another jQuery plugin on the (very long) list!