Select and de-select all checkboxes using jQuery

On almost every application I’ve worked on, there was a similar user story:
When i visit the page showing the list of unpaid/unprocessed things, I want to be able to select some of them to process automatically.

The best way to do this is with checkboxes, and a simple form that will submit to a given path, collecting the items you need to process.

Basically the next user story after that one is: I want to be able to select all checkboxes, so I don’t have to click each one if there are plenty of them.
This is where the story would get complicated, but it is really easy to do.
Let’s say that you have a check_box with an id of #select-all-checkboxes and that your form contains checkboxes with a class of .selectable-checkbox like this:

<input id="select-all-checkboxes" type='checkbox' value=1/>

<form action="/action" method="post">
  <input class='.selectable-checkbox' name='checkbox_name[]' type='checkbox' value='1'/>
  <input class='.selectable-checkbox' name='checkbox_name[]' type='checkbox' value='2'/>
</form>

The solution to toggle them all is very easy, with JavaScript:

$('#select-all-checkboxes').change(function() {
  var is_checked = this.checked;
  $('.selectable-checkbox').each(function() {
    this.checked = is_checked;
  });
});

or with CoffeeScript:

$('input#select-all-checkboxes').change ->
  is_checked = @.checked
  $('.selectable-checkbox').each ->
    @.checked = is_checked
    return

And that is about it, now you have select/de-select all checkboxes working on your site. Be sure to wrap the above code in a $(document).ready(function() {}); or some other initialiser that is fit for your application.

Getting through the famine period

If you are a full time freelancer, you have, or surely will encounter a famine period, because only thing sure in freelancing is uncertainty. Of course, with great marketing and engagement, that uncertainty can be reduced to a minimum level, but it still exists. Let’s say you encounter a famine period. That can happen for a variety of reasons, you being fired by a client, you firing a client(you can and should do that when you feel the need), client not having any more work/money to keep you on, or the worst one of all, health/family issues taking most of your time.

Whatever happens, you should always have a certain amount that goes from your earnings to a savings account. I handle that by working through an equivalent of a LLC company in the US, and pay myself a monthly paycheck. That way, enough money stays on the company’s account as a certain part of savings, and you should make sure you have at least 3 paycheck’s worth in the account, before you go and buy that new and shiny Apple product you just have to have right now. Of course, be sure to leave enough for taxes, especially if your government does not require you top prepay them for the following year.

After we have the company account, and enough money that goes into savings/taxes, you should consider your personal savings too. On that side, try to reduce your spending until you get to at least full 3 monthly burn rates of savings. If you don’t know how to achieve this, there is a great book on the subject Rich Dad, Poor Dad written by Robert Kiyosaki. It should teach you enough to get started.

When famine happens, and it surely will, the savings backbone and mentality described will help you get through the bad period much easier, and let you continue living your dream job, instead of defaulting to full time employment every time you have a bad financial week or month. The first thing you should do is take a retrospect and analyze what had happened. If the client had fired you, was it your fault, or theirs? Did you communicate all issues with the client, Maybe there was some scope creep, or you just bit more than you can chew. If you have fired the client, why did you do that, what trait of the client you found so annoying that you just couldn’t wok with them? Can you try to spot and avoid that type of client in the future? If the client ran out of work for you, can you offer them more? Or reformulate your offering to maybe suite that type of client better? If the client ran out of money, why did that happen? Was it something your work has caused, could you somehow guided the client to better decisions, and maybe profitability?

After you have summarized everything, it is time to fire up your sales funnel. If you don’t have one, go to a user group, and as if someone has extra work you could take over, if you engage with your local group on a regular basis, they will get to know you, and throw work your way, even when you don’t need it. Contact your previous clients, ask them if they may have some work for you, or know someone who has. Referrals is one of the best ways to get good clients, and be able to provide them with great value.

In the meantime, take a vacation, you have earned it. Being a freelancer, I sometimes dread for vacation time, but always wanting to do/learn more keeps me from resting completely. I know that I should try a screen free day, as soon as possible.

Go learn something new. If you are a Ruby on Rails developer, go and learn a frontend javascript framework, Ember or Angular are good enough. Learn another language, maybe a functional one, Elixir seems to gain popularity, Clojure already is fairly popular within the developer community. Invest time in yourself, you have all those savings to keep your mind at ease. You could even learn a real(non-programming) language, that will make your synapses fire differently, and knowing a foreign language never did any harm to no one. Start writing a blog, just for yourself if you have no audience. It will help you channel everything you know, learn it better, and maybe gain you some tracking in the community.

The famine period will make you desperate to find work, that is something we people feel and can’t really get away from it, but hopefully you will be able to rely on your savings, until you find a client that fits you just right. And if you can’t find a client in 4-5 months, then go and look for a full time job, maybe using some of the hot new skills you have learned.

Why learning new things is so hard, and why you want it to be that way

Do you know that feeling when you are starting to learn something new? The feeling of being lost in all that stuff that you can not grasp? Feeling like an idiot while thinking that you will never learn it. Good, that is the best place where you want to be. Learning new things should be challenging, it should make you cringe sometimes, and make you want to bash your head in the wall.

I was looking at my son a few days ago, and I noticed how he was learning how to climb, trying, his feet would not listen to him, and be there where they should, he was aware of it, and it made him angry, because he wanted to climb up. But he tried again, and again, and again, and then he succeeded, now he is climbing fast and precise, which for a 20 month old kid, that is very good.

So the best place you can be is this one, banging your head, because it is where you learn the most. If you remember those editor learning curves, that are so steep, they are because we learn that way, we have to learn so many things in the start, be it vim, emacs, or even Ruby on Rails. The content you have to wrap your head around is so big, that you can’t fit it in your head in one go. But because of your frustration, you learn faster, likely some animal self preservation instinct kicking in. If learning something new becomes too easy, you are not doing it correctly. There is a saying in cycling, that goes something like this: “If your legs are not hurting while climbing a hill, then you are going way too slow, and need to pick up the pace”.

When do we stop learning like this, headstrong, just pushing until you accomplish something, I can’t tell, is it the moment when we become aware of other people, who may judge us, or we fear others judging us? Basically school, i would say high school inmost instances. Ironically, the place where you must be able to learn the most, makes you unlearn learning. Makes you give up trying to do things you do not know how to do, because you believe someone will laugh at you. Yep, that happened to me, and I gave up, only to pick it up later in my life, and realize the time I have wasted. And you must not waste your time like that too.

Just pursue your dreams and ignore the bad critics. Of course you need to have some common sense, and someone you trust to tell you when you suck. But, for example, some if not most people are told they can’t sing when they are teenagers, by underpaid public teachers, and then they give up completely. Maybe they needed more practice, their performance was not, and never would be Elvis like, but they could have learned to sing, a little at least. All people are not the same, and each one of us learns differently. Because of that, you must find a way that is best for you personally, also a reason why public education sucks, by treating and grading everyone the same. When you find your way, it might be writing things down, doodling, repeating out loud, or presenting at user groups and conferences, do it, and do more of it. Your brain will be grateful to you, and regular mental stimulation does the same for your brain, as exercise does for your body(and brain). Not only will you reduce the risk from e.g. Alzheimer’s Disease, but it could indirectly improve your life style, and with that your life expectancy. Learn more, live a longer and healthier life.

There is a great quote that describes this perfectly, and it is from a great book series, that everyone must read, at least once. “My brother has his sword, King Robert has his warhammer and I have my mind…and a mind needs books as a sword needs a whetstone if it is to keep its edge. That’s why I read so much Jon Snow.” – George R.R. Martin, A Game of Thrones

Rails No-No’s: Not using background processing

Have you encountered a website with a very slow response? How do you feel when you have to wait for 5 or more seconds, for the page to even start responding? How do you feel when the app you are waiting for ages, is something you are paying for on a monthly basis? Fuck this, let’s try something different? Yep, that is the same thing your users (customers) are feeling when your app is waiting for something. And users have the tendency of voting with their wallets, which in this case, can make your much thinner than it is at the moment.

First and foremost issue is sending emails, when the user registers or when they ask for a password renewal. Those situations can even stand sending emails in sync, but you are blocking the web process thread for new requests while the email processing ends. Let’s consider a situation where you have some larger data massaging operation, that hides between a single form submit. Take for example large business reports, which have to collect data from a number of different data sources, both internal and external. Now, this process can take from a couple of seconds, to hours if your data is not well optimized. You are basically wasting that person’s time on waiting for the report to finish. Now multiply the average time that a person waits, with their hourly rate and that with the average number of reports someone waits for, and with the number of people running such reports in a given company. That is a serious waste of money right there, now if you could somehow optimize this process, you could stand to easily save $10,000+ on lost time, and more over the lifespan of the application.

Imagine if the users abandoned your shopping cart, while it was processing something that could have ran in the background. Now you are losing real sales, and your client is on their way of either firing you, or going broke.

OK, enough melodrama for now. Let us now consider a different option from doing everything in one process in sync. You have the option of threading, but that won’t get you very far, because you are still blocking the user’s flow, while they wait for the response. Enter background workers, which are best run on a separate machine, and they have queue of tasks that need to be performed. A set number of processes which take handle of those tasks, and report back(if needed) when the task is finished. Here are a few options for background jobs in Ruby on Rails:

  • Sidekiq – Multi threaded background processing built on Celluloid::IO and actor based model
  • Delayed::Job – One of the first, if not the first background workers for rails, extracted from Shopify
  • Resque – Background worker using redis instead of the database for queuing jobs

I’ve been using all of them over the years, but as of a year ago, I have been using only Sidekiq on new and migrated some old applications from other workers to it. The benefit of Sidekiq is using actor model and threads to process jobs concurrently, it has a low memory footprint and can be used to boost your application performance, and responsiveness. If you are on Heroku, this will cost you one worker instance per month, as that should be enough to process a very large number of background jobs. If you are running your own stack, one server with the minimum hardware requirements will suffice. No one cares if their email is delivered in 3 minutes instead of right now. But they do care if they can’t do anything for that 3 minutes while waiting for “something to finish”. Here is a basic example of how it’s done with Sidekiq:

class UserGeoLocator
  include Sidekiq::Worker

  def perform(user_id)
    user = User.find(user_id)
    user.perform_long_geolocation_api_call!
  end
end

You can find out all the things it can be used for in the Sidekiq Wiki on Github.

Impostor Syndrome

Are you feeling like an impostor in your team? Are you achieving things other members of your team appraise you for, but you live in fear, day in and day out, that today is the day when you will be exposed as a fraud? Maybe someone incompetent enough to do the job you are doing right now, and earn the paycheck you are receiving at the moment? Do you think that everything you have done, and are doing successfully, is just pure luck and a fluke, and that one day your luck will run out?

Unless you are working on some menial governmental job, and slacking as hell, you are most surely wrong. You have got Impostor Syndrome, a fairly common disorder that is manifesting in highly achieving people. Basically, it means you can’t internalize your accomplishments, and thinking that you are not as intelligent as other people believe you are. You accept all praise with doubt and uncertainty, and await your day of judgment.

The Impostor Syndrome, or phenomenon, as the phrase appeared in the book The Imposter Phenomenon in High Achieving Women: Dynamics and Therapeutic Intervention, is not a psychological problem, but mostly a sociological issue. It is present mostly in highly achieving women, minorities, but there is evidence it occurs in a comparable number of men also. The most effective technique of overcoming it, is to simply recognize it exists, but that can be hard, especially if you do not know about it, or even if you do, can not admit to yourself that you have it, but still leaning on to your story, even if it makes no sense to anyone else but you. Here is where great teammates and managers come in, they must be able to recognize it, because it is happening in the technology sector more that we want to admit. Especially now, that the diversity in technology is finally a topic everyone is talking, and doing something about it. So if you have a coworker, or a subordinate, who is suffering from this syndrome, please point it, and help them. It is a bad state to be in, as paranoia will eat their productivity.

One of the ways to stop the issue is writing, keeping a journal. Then the person suffering from the syndrome can organize and perceive their achievements, they can easier associate them with reality. Also, all the records are there for a later time, when they are feeling the syndrome again, to remind them of the things they have achieved.

It is healthy to want more all the time, but be sure to acknowledge what you have done already, and never let your inner doubts keep you from doing great things.