Mini Review of “Secrets of the JavaScript Ninja” By John Resig & Bear Bibeault

jsninjI have started reading the new book of John Resig, which in case someone don’t know, is the creator of jQuery. I’m just in the beginning (page ~60) – but i still have a few insights that I would like to share:

A free e-book!

When you buy the book, inside of it you get a code which you can use to get the e-book version for free. That was a very nice gesture from Manning that i really like. That way i don’t need to choose, I get both. and it is a great surprise since I was not aware of that.

About the book

I really like how the book is organized, it is well written and addresses the reader in a way that reminded me a private tooter. It does not uses high language and it is very easy to follow, both the text and also the examples.

Who the book is for?

This book is not for a novice developer. If you have a good grasp of JavaScript then this book is for you, it will uncover some of Javascript black magic, with a fun approach. if you are a newbie in Javascript – this book is not for you. you should start with a more basic material and only then read this book. I think every front end developer should read this book – but if you just beginnin – read some more basic stuff and only then return to this book.

Scoping – A small point that was overseen

When the writer is explaining about scoping, he wrote that the function scope is within the entire current scope even if it is defined at the end of the current scope. And in the example that was given it was implied that a variable is not acting like that… But it is! It is available. It is only that the assigned value is available after the assignment, but the variable itself exist in the scope. That maybe sounds like an unimportant point but it could cause an unexpected bugs like in this example:

You would think that the value of ‘test’ will be “I’m out” according to the book explanation, but because of the hoisting nature of Javascript it is actually will be ‘undefined’, hoisting is not only for functions it is also for variables. The only difference is that functions will be defined even if the declaration is after the function call while in variables it is not.

So we can actually say that the above code will actually behave like the next one:

Best practice – implementing a collection view in Backbone.js

backbone

Lately I was working a lot with Backbone.js, I really love this tool! It is so easy to encapsulate my code into separate components.

What I had a lot of thought about but with no real answer is what is the best way to implement a collection view?

As I see it there is mainly two ways to approach this:

  1. Implement the model with a model view and the collection view will just be the container that contain all the model views.
  2. Implement the collection view to render also the models in it.

Each of these approaches has upsides and downsides. And the difference is really the question of where to render the model? in the model view? or in the collection view? the intuition is to do it in the model view – but that is making a few disadvantages of interaction between the models.

The downside I have with the 2nd approach is that for any addition / subtraction from the collection the entire collection view (with all the models) will need to re-render, in a collection with thousands of models – that is a huge problem! But it has an upside too – if the models have some kind of interaction between them (sorting, filtering) it is much more easy to implement it in the 2nd approach.

The downside of the 1st approach is the upside of the 2nd and vice a versa. So I could say that I should select the solution according to my needs, and that is what I did until now. Until the day that I needed both upsides…

So the question is – is there a way to implement the 1st approach but to also have the ability to have sorting and filtering on the UI? The only way I could think about that happening is that the collection view will also hold all of the model views but that just does not sounds right…

Go with the Model-View approach

Well, it is the combination of both that solves this problem. How can it be both? it is actually the first approach, each model has it’s own model view, so adding to the collection will not need to render the entire collection view. Only when we want to sort / filter – the entire collection view will be re-render.

Encapsulation

This solution is really a best practice since the models does not know about the sorting / filtering that can be made to them – this will be implemented in the collection and the final visual result will be rendered in the collection view

Untitled drawingSo when we want to render all the models that a ‘filter’ function returned us. we will run on each model and we will create it a model view and then we will append all the model views to the collection view.

 

Iphone 5 Vs Galaxy S4

[disclaimer : This is not an in-depth comparison between the two devices - it just a response post to the Apple "Why iPhone" article]

So, I was just reading about Apple “Why iPhone” article. Apple released it a few hours ago and it really looks like Apple is freaked out. You can’t say you are better – you need to be better. And at the bottom line,the points that the iPhone is better than the Galaxy S4 is mainly PR.

Apple actually said that its camera is better because it’s more popular… for real? Windows 7 is more popular than Macintosh OS, that does not make it a better OS.

The iOS lacks a lot of features that Android had from day 1, like Widgets and themes. Apple was considered better because of the interface ease of use – no more! The Android OS was improved so much in the current version and Samsung took it even further with the eye & Hand gestures.

Is iPhone better?

The short answer is NO! The long answer is that it is better in a few areas like pixel density & high-end metal cover, the buying experience is better at Apple, but this argument is about which is a better phone, not which has a better buying experience.

In most areas it falls short:

  • Screen size
  • Operating system – a lot will argue but I think that the Android current OS is much more efficient and user friendly than the iPhone iOS.
  • Camera – both hardware and software is just better, 8MP vs 13MP and check out the software to see the amazing differences.
  • Battery – iPhone 5 maybe does not have a huge battery but it also does not last much… how can they post it as a positive fact?!?!
  • Goggle music – up to 20,000 songs that I can access anywhere (Unlike the crappy iTunes)
  • iCloud – how is that better than Google Drive?

And if we put the Galaxy 4 on the side for a moment and we will compare the iPhone 5 Vs Nexus 4 for a second than we will have two great phones with an hardware advantage toward the Nexus side, iPhone has it’s LTE, which is great but since when LTE & Retina screen worth 300$? Let me answer that – it’s not!

Nexus 4 Vs iPhone 5 review

SVN Vs Git

gitLogo svnLogoWell, we know why we should use a VCS (Version Control System) is a great thing to have, but there are a lot of VCS systems out there. How to choose one from all the variety there is out there?
I will not say which is the best one because I have experience in SVN (Subversion) and Git and not in every VCS there is out there, so I’m going to compare just these two. They are also currently the most popular ones.

Let’s start with the conclusion – In my opinion Git is better than SVN. I also talked about it with other people from the industry and they all concluded that Git is better, some of them still use SVN but that is only because it is a huge deal for them to perform the migration from SVN to Git.

Let’s go into the Why’s:

Locale repository

The repository, the system that hold all the changes that were made to the files, in Git it is not only on the main repository on a remote server it is also in my local copy – that mean that if from some reason my server is gone – anyone that fetched data from the server – has a copy of the repository. that is a great backup repository strategy, all you need for that is to be updated.

Local commit


This is a huge advantage! It’s not only for the times that you are on a train without internet connection. When you work on a project you code almost everyday. sometimes there is an internet glitch and you will be stuck without a connection – if you are using SVN, you can’t commit so you will have to choose between two options :

  • stop working until the connection is back
  • keep on working but then you need to make the commit of a bigger changes, that mean that “task separated commits” can go wrong this way.

In Git – you can just do a local commit and then push all the commits at once.
It is a good solution for when the repository is from some reason not accessible.

Built to branch

In Git branching is easy and natural, it takes seconds to create a branch and it is encouraged to branch before starting to work on anything. By branching (which is also possible on SVN, just less convenient…) I save myself the problem of leaving a current half baked work, starting a more urgent one without the half baked work and when finished going back to the half baked and resuming the work. with the option to also include the last more urgent task! How great is that? Merging branches is as easy as creating branches and once starting using branches in Git I feel the peace of mind of not screwing someone elses work.

As I have said in the beginning - I’m pro-Git. I really like it and have very happy experience with it. It’s not that SVN sucks, I just think that Git is a lot the-git-father-forking-badass-clearvisionbetter.

 

 

Why should I use a VCS?

Ohh crap my data is gone

 

gone_in_sixty_seconds

I did it! I worked on my super cool new iPhone app that will upgrade my bank account status. I’m on my way to brag it to my friends and on my way the laptop fell down and.. Ohh CRAP! My hard drive is dead! What should I do? Take it to the trash? Or pay  a lot of money to restore it? and that also does not work in 100% of the cases.

Huge bug – Let’s go back!

bugThis time on my way to show off my cool app to my friend I didn’t break anything, but my dear friend found a critical bug on the new feature I just added, I wanted to put it on the app store today, but it will take me three days to take the feature down because it has a lot of dependencies. Also I know it will take me four days to fix it so it will be a waste of time to work Continue reading