Preserve Console Log in Chrome

Like a huge number of other people, my co-workers and I are fans of Google Chrome.  Of course, as web developers, there has been one feature that has been driving us nuts — the inability to persist the developer tool’s console log messages across page refreshes.  Whenever I find myself in a situation where I absolutely must have this functionality, I have to abandon Chrome, launch Firefox, and do my work in Firebug.

Granted, Firebug is an excellent tool and one with which every web developer should be familiar.  But having to switch browsers mid work flow is such an annoyance.

Well not any more!  I’m not sure which version it was released in — I’m currently running version 14.0.835.202 of Chromium — but it’s here.

Preserve Console Log Messages Across Page Navigation
Preserve log upon navigation

Fire up the developer tool window, click the gear icon on the lower right hand corner, and check the box we’ve all been waiting for. Alternatively, you can right click in the console window and enable the option from the associated context window.

Thanks to Google and all the excellent Chrome developers for this much awaited feature. Now back to my work!

It’s time to setup your Gravatar

If you have already setup your Gravatar, this post is not for you. If on the other hand you have no idea what a Gravatar is, welcome.

Let’s get this meeting started. My name is Jason: It’s been one week since I set up my Gravatar. I will admit that for years I have seen the anonymous outlined man’s head next to my blog posts and never really thought much about it. Over time as I started to receive more comments among the various blogs I write on, every now and then I would see a comment where instead of the anonymous man was a fun/sad/creative/dumb little icon image. I wondered how they changed the image — there are no settings in the wordpress admin — but after about 30 seconds of intense investigation (!google) I would become uninterested and move on to something more exciting.

To get to the point, while doing the initial setup of …com, I finally got interested enough to investigate for 30 seconds (!!google) and quickly found out that the images are created at gravatar.com.

The Gravatar service as stated on its website is “A Globally Recognized Avatar”. So when you post a comment, publish an article or any number of other things, if the website you are posting to supports Gravatars then your image will show up. To find the correct image, the hosting website will send a request to the Gravatar service asking for the corresponding image associated to the email address you provided while making the comment/post/etc. If none is found then it will use the default image anonymous man’s head; otherwise, it will get the associated Gravatar for the account. If you update the image at a later time, you can do it in one place and be updated every where your Gravatar is used.

The sign up process is about as easy as it gets and within a couple minutes you will be sporting your very own Gravatar. There are two things to note when setting one up. First, if you have a wordpress.com account, there is a good chance you already have an account with gravatar.com. You can use the same password that you used to setup your wordpress.com account or you can reset it if you forgot. Second, while the image upload takes a couple seconds, it may take 5 minutes or more before your image starts to appear on your own blog and in the comment sections of other blogs.

One of the things I really like about the service is that it allows you to associate multiple email addresses with your account and manage all the images in one easy to use location.

Negation and De Morgan’s Law

Undoubtedly, your grade school grammar teacher has scolded you at one point or another for your inadvertent use of a double negative.  Statements such as “I don’t know nothing about physics”, while grammatically incorrect, are often semantically inaccurate as well.  While the previous statement would commonly be understood to mean the person posses no knowledge of physics, the actual statement implies the opposite, that the preposition “I know nothing about physics” is incorrect.  Certainly your teacher offered sound advice and one would be wise to heed such instruction.

Unfortunately, when programming such double negation is quite common and often leads to disastrous ends.  For example, who would qualify for the below condition?

if( !($student->age != 18 && !$student->enrolled('Physics')) ) { ... }

We want someone who is not both not 18 and not enrolled in physics. Simple, right?

Granted, at the time this snippet was written, it might have been well understood by the author.  But 6 months later when this line needs altered by a different developer, precious time is wasted deciphering this twisted logic.

This is where De Morgan’s Laws come in handy. From Wikipedia:

The negation of a conjunction is the disjunction of the negations.” and
The negation of a disjunction is the conjunction of the negations.

Great, so what does that mean? Basically, it means that if you have two conditions — p and q — and you apply the negation to the statement p and q, this is logically equivalent to !p or !q. Similarly, if we have the statement p or q to which we apply the negation operation, this is logically equivalent to !p and !q.

The easiest way to think about this mentally is that when we apply a negation across a statement, we prepend each preposition ( either p or q ) with the not operator, and every time we encounter the logical operators and or or, we toggle them

Thus, ! ( ( p or q ) and ( r or s ) ) implies ( (!p and !q) or (!r and !s) ).

Using De Morgan’s law, let’s simplify the logic of our initial if condition.

Let us assume the preposition p represents the condition where the student’s age is 18 and q represents the condition where the student is enrolled in physics. Given this, our initial condition can be written in a more compact form as

if( !(!p && !q) ) { ... }

After applying De Morgan’s Law, we have

if( !!p || !!q ) { ... }

The negation of the negation of p is p, so the above reduces to

if( p || q ) { ... }

Substituting p and q for the original conditions, we arrive at the greatly simplified

if ( $student->age == 18 || $student->enrolled('Physics') ) { ... }

which is much more readily understood.

In order to convince you that the two statements are logically equivalent, a truth table is provided below. Note that the second to last column contains the results of the original condition, and the last column is the reduced condition.

p q !p !q !p && !q !(!p && !q) p || q
1 1 0 0 0 1 1
1 0 0 1 0 1 1
0 1 1 0 0 1 1
0 0 1 1 1 0 0

As you can see, De Morgan’s Law is a powerful tool for managing conditional complexity in code. I hope this hasn’t not helped.