Superbowl Saturday?

Short rant. When will the NFL wise up and move the Superbowl to Saturday afternoon? The Superbowl is now an event as big as any other and undoubtedly the biggest sporting event of the year. People like to drink and stuff their faces (is this news to anyone) so why not allow for some time to recover? Who wants to party hard on Sunday night when most of us have to go to work the next day. I for one, don’t like having to stagger into work bleary-eyed, or take a day off. Start the Superbowl no later than 4pm EST on Saturday afternoon and turn it into an even bigger party.

Bracket Style and Indenting Code

I’ll try to keep this short. Having been coding for years I’ve seen a lot of examples of how other people write code. Now while there are certain requirements in coding, there are areas where you can inject your own person style or preference. One area that has always bugged me is how code is indented and how brackets are used to separate blocks of code. Here’s a very simple example of how I normally code a simple if/else block:

if (hours < 24 && minutes < 60 && seconds < 60)
{
 return true;
}
else
{
 return false;
}

This is known as Allman style (named after Eric Allman, the developer of sendmail). The most common variant of this would be K&R style (from Kernighan and Ritchie’s book The C Programming Language) and the same code looks like this:

if (hours < 24 && minutes < 60 && seconds < 60) {
 return true;
} else {
 return false;
}

Equivalent in every way except readability. Some people call this “The One True Brace Style” because it’s been around so long. The major difference is that the opening bracket is placed at the end of the line that the control statement is on where in Allman style the brackets are each on their own line.

To me it makes sense to make code as readable as possible by lining up brackets to match the block they go with. It becomes apparent what blocks of code belong to what conditions.

Allman style
 
K&R style

If I was to have another condition nested within the example, it would look like this:

if (hours < 24 && minutes < 60 && seconds < 60)
{
 if(hours % 2 == 0)
 {
 	return true;
 }
 else
 {
 	return false;
 }
}

Again, very readable.

The funny thing about K&R style is that its roots seems to be based in the fact that programmers used to have to deal with limited screen space and by squishing the brackets together with the blocks that they belonged to, it saved precious screen real estate. Most programmers either picked up the style they use most often from learning coding in a class or by following the examples set by others, while other programmers code for readability, especially now that space isn’t the issue it once was.

While Allman and K&R are just two of the most popular styles (see
http://en.wikipedia.org/wiki/Indent_style
and
http://en.wikipedia.org/wiki/Programming_style
for some others and a detailed explanation about the history of each)

(please pardon the less than perfect wordpress code formatting)

The Non-Breaking Space –  

This is one of those posts that you never thought you’d be reading. Spaces, how basic a topic is that? It’s almost ridiculous. A no-brainer. The &nbsp; is a "HTML character entity" which is just a fancy way of saying it’s a HTML way of displaying characters that may not appear on your keyboard. The two common ways to access these special creatures is to use special HTML. For example, if you wanted to show the cent sign (¢) you can enter &#162; or &cent; in your HTML. All character entities start with a pound sign (#) and end with a semicolon (;). There are plenty of cool or unusual HTML character entities available out there like the club ♣ (&#9827;), the quarter fraction ¼ (&#188), the infinity symbol ∞ (&#8734;) and many more. Note that not all HTML character entities are available in every font and every OS, so sometimes you might get unexpected results, especially with the more obscure characters.

But the space? Why does the space need a special code (&nbsp; or &#160;)? Well technically &nbsp; and &#160; aren’t just spaces, they’re non-breaking spaces. What’s so special about that you ask? The difference between "just an average space" and the "non-breaking space" is that while a typical space between text will allow the text to be broken up on multiple lines, the non-breaking space does just what it’s name implies and prevents the text from breaking. Here’s a quick example. Let’s say you have some text that is being squeezed due to other elements on your page. So the text "non breaking text" ends up like this:

Example:

non breaking space

You can see that the text has been broken up on multiple lines. What happens if we absolutely need to have the text all on one line and want to prevent the browser from automatically breaking it? This is where the non-breaking space comes in handy. By substituting regular spaces with non-breaking ones you get:

Example:

non breaking space

This obviously breaks the 50 pixel width we specified, however it does keep the text from breaking. Now if you’re like me, you’ve known about the &nbsp; tag forever and never given it a second thought. You might also have run into it when looking at code from other folks or from some HTML editors, especially when people try to indent things manually like: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Section 1. Older versions of FrontPage were especially guilty of trying to be overly helpful by using &nbsp; in places that it didn’t belong, or when content was pasted into it. Older versions of Netscape also used to pitch a fit if a table cell was encountered with no content in it and the easy workaround was to throw a &nbsp; tag in the cell to appease the browser. Thankfully most of today’s better HTML editors know when to properly apply the &nbsp; tag and now so do you.

Note: Also check out the <nobr> tag (which was never a formal part of any HTML standard) and CSS nowrap attribute.

Simple Pagination Class Revisited

I wanted to make a small addition to the pagination class talked about at http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/. I added another example which uses a basic form at http://www.catchmyfame.com/paginator/example-form.php. This is very similar to the first example (http://www.catchmyfame.com/paginator/example.php) where the query is hard coded. This new example is meant to show how the pagination class can be used with forms where the data determines the query. In this case, the pull down menu for a continent selection determines the query. Note that the pagination class works fine with both GET and POST requests.

A web developer's journal