Archive for category Web Developement

Object Oriented CSS – A Primer

Preface

Object Oriented CSS (OOCSS) has recently made some buzz in the web industry and for very good reasons. It’s the kind of thing I go: why didn’t I think about this? As a CSS architect, I’m very enchanted to learn something new in the subject, all thanks to Nicole Sullivan at Yahoo.

It’s a great idea, well, just maybe

A lot of Nicole’s arguments were hammering around the fact that OOCSS would be faster. While it may be efficient to code OOCSS from a programmer’s perspective, it’s far from making your page load any faster. OOP concepts require you to separate classes into individual files so that the code becomes more manageable. However brilliant that is from a manageability perspective, it’s a nightmare plan for CSS speed. You don’t want to make your browser load every single little piece of CSS like that.

If your OOCSS class files are served from a single domain, the average browser will only handle 2 concurrent connections to that given domain, making your loading of CSS classes literally 2 at a time. This isn’t beneficial at all. Having multiple pieces would require your browser to wait for the other ones to finish before even sending a request for the rest, and that also makes a lot more HTTP requests, in other words, a lot of overhead.

PHP and other applications of course don’t have that problem since they request files that are stored on the server from the server, which just results in an insignificant increase in disk I/O. But the whole OOP concept remains slower however you see it. In software engineering, everything ends up being either a 1 or 0. The computer by itself cannot understand OOP, which means you have to add additional application logic to make it work, making things slower. The only reason OOP exists is to make it easier for programmers to develop complex applications, it has nothing to do with speed.

Easier to learn?

Apparently OOCSS is easier to learn. Yes, ok, it’s true that making it possible for a junior CSS developer to only handle smaller properties from different classes at first is a way to make CSS easier to work with. But it doesn’t make it easier to comprehend.

OOP concepts won’t help in any way at understanding the CSS box model, neither will they fix the differences between the various browsers. In fact, OOCSS just makes it more difficult to comprehend CSS because on top of that, you have to learn the Object Oriented concepts.

The only real easy part is what OO brings to anything; maintainability. You don’t have to dive into a super long page of CSS rules to modify something.

Cascading, not Object Oriented

Unfortunately for Sullivan, as she mentioned in her presentation, OOCSS is but a practice. CSS has never been Object Oriented, it’s a Cascading language. Using OO practices with CSS is a good idea, but it doesn’t really work. Let’s take a look at some examples:

<style type="text/css">

.block {
   width:200px;
   height:200px;
   background-color:blue;
}

.redBlock {
   width:400px;
   background-color:red;
}

</style>

[...]

<div class="block redBlock"></div>

Ok, what do we have here. First off, you’ll notice there’s a lot of stuff missing, but just imagine there’s all the HTML headers and tags around it. So we have two classes and a div with both of those classes in its name. Yes, this is perfectly fine CSS practice.

With that, we would obtain a 400 by 200 pixels red square. The ability for the square to take one of the classes’ property and override it while still keeping its other properties intact is called Inheritance in OOP. Based on our HTML code, you would guess that redBlock inherits the properties of block. So how about we switch it like this:

<div class="redBlock block"></div>

The block would then take a blue color and a width of 200 px right? Well, this is where the problems start, it doesn’t. In fact, the order in which you specify the classes of the div has no importance whatsoever. In this scenario, we would still get the same red block with 400 px of width because the redBlock class is written after the block class in our CSS rules. This is called Cascading, which is where CSS takes its name: Cascading Style Sheets.

Most people who do CSS know the other aspect of Cascading which states that internal CSS overrides external, and in-line overrides internal. But very few know that it even extends directly into the declarations themselves, possibly because they’ve never used two classes in a single element at once, a very rare CSS practice as of now.

In real OOP however, there’s so such thing as cascading. By inventing some nice pieces of fictional CSS to define the inheritance, much in the way it’s done in real OOP languages, we can do the following:

<style type="text/css">

.block {
   width:200px;
   height:200px;
   background-color:blue;
}

.redBlock {
   extends:block;
   width:400px;
   background-color:red;
}

</style>

[...]

<div class="redBlock"></div>

In OOP, making a class “extend” another is literally telling it to inherit the properties of that class. In this scenario, we don’t even have to write the two classes inside the div because the CSS rules already define redBlock as inheriting (extending) the properties of the block class. We would obtain our famous red block. However, we didn’t say that block extended redBlock, so writing a div element with the class block would only use the block class’s properties, not the redBlock’s.

Unfortunately that CSS code is pure fiction. There’s no such construct in CSS and since it hasn’t been planned for CSS3, the whole idea remains largely a dream years away.

But do you really want this dream?

However brilliant this OO thing would be, its current implementation is nothing but a way of coding. Because of its functionality issues and overhead though, that way of coding CSS is nothing more than a gimmick.

In my opinion, it doesn’t bring in enough maintainability and functional benefits in its current state versus the overhead induced. Yes, Object Oriented practices do make it easier not to make scalability mistakes but a lot of Sullivan’s speech is just good practice, not OO. Every newbie in every field will make scalability mistakes no matter what the language.

I’m not sure if the current state of OOCSS solves anything.

PHP6 – Not out but books are

So yersterday I bought this book at Chapters called Professional PHP6. I was actually very puzzled that the PHP6 launch got under my nose like that. Oh well I thought, better learn it now when it’s new.

So, I arrive home and I check on the web. PHP6 is not out yet. So, I don’t know how this happened, but even the PHP.net website has nothing on this. Anyway, two books were already out for PHP6 and I’m already learning it.

Even more puzzling, the books reads: “Dive in right into the recent launch of PHP 6″. The book is from April 2009, but it assumes PHP6 is downloadable everywhere. It even assumes MAMP includes PHP 6, which it clearly doesn’t right now.

PHP.net’s download page only offers version 5.2.9. 5.3 isn’t even out yet! I’m following the instructions set on paper in that book, but… PHP6 doesn’t exist!

Google Wave, HTML 5 and Webkit

At this year’s Google I/O, we’ve had a taste of this very sweet thing called Google Wave. I’m totally sold, this thing really is email invented and I think once people realize how powerful it is, and when people try it of course, which is easy, Google is the most visited site on the Internet, it will explode in popularity.

And since it’s a protocol, open source above that, so not just a Google thing, I’m positive Wave is going to replace e-mail in a complete fashion very quickly.

But how far can a product go at making things more popular. If you noticed, the demo runned on Chrome 2 and Safari 4, the two biggest Webkit-based browsers on market today. Why? Because Google Wave requires HTML 5 features not yet implemented in other browsers (except Opera 10 maybe). Yup yup, even Firefox doesn’t support it and of course IE is far from that.

Google Wave is so entrenched in HTML 5 that some things weren’t even implemented in HTML 5 yet and Google is proposing those new capabilities to the WHATWG (Web Hypertext Application Technology Working Group).

So although we’ll have answers to that much later, I was wondering: Can Google push Webkit browsers to glory and forcibly integrate new features in HTML 5 because of Wave?

Post your theories in the comments.

Reducing JavaScript – Omitting Curly Braces

Edit: This “trick” is used in a lot of JavaScript compression tools or pre-made scripts, like Adobe Dreamweaver’s JavaScript roll-over images.

Note: This is actually a JavaScript trick that was, I would think, never meant to be actually executable. Do not use it in production environments, it’s unstable and clearly more confusing than useful. It’s just cool to know.

JavaScript Browser Support

JavaScript has always had its share of standardization issues. From Microsoft’s JScript to even Adobe’s ActionScript, they are all, afterall, just ECMAScript Dialects (well, since 2 years after JavaScript’s invention, in 1997).

In any cases, some very obscure less documented stuff exists in JavaScript, often akin to indivial dialects of ECMAScript. One of them is the omission of curly braces in conditional statements and functions.

Omission in conditional statements actually looks like it’s standardized since all ECMAScript dialects support it perfectly (JavaScript, JScript, ActionScript 2.0, ActionScript 3.0). However, it heavily lacks documentation and is often omitted entirely from books.

On the other hand, curly braces omission with functions will not work in anything else than Firefox. This is no surprise though, since Firefox supports a much more recent version of JavaScript than other browsers, although I doubt that functionality is actually part of ECMAScript.

As of Firefox 3.1, it supports version 1.9, compared to 1.5 which is standard on KHTML/Webkit/Chromium browsers like Konqueror, Safari and Chrome. Opera and Internet Explorer have version 1.5 and version 1.3 respectively, but they cannot really be judged on their JavaScript version since they both use custom JavaScript builds. Opera uses a combination of standard ECMAScript support plus JavaScript and JScript extensions, providing it with greater support for Internet Explorer-only scripts. On the other hand, Internet Explorer uses JScript entirely, Microsoft’s own version of JavaScript, which adheres in its own ways to the ECMAScript standard.

What’s a curly brace (or bracket)?

Note: In every example, omitting the semicolon at the end of a statement is totally possible and will not influence the functionality of the code in any way. Despite what it might look like in these examples, a semicolon is purely stylistic in JavaScript and is only used here for the sake of familiarity (because most programmers write like this and it’s a good practice to do so for languages like PHP that do require them).

In case you missed on the curly brace thing, this is a curly brace set: { }

With Conditionals

Following is traditional if conditional statement.

if (boolean == true)
{
  alert("test");
}

Next up is the same construct without the curly braces.

if (boolean == true)
  alert("test");

Both of those work exactly alike. Much in the same way, if else conditional statements can be used.

if (boolean == true)
  alert("test";
else
  alert("test2");

And so, this short-hand like syntax can also be used with conditional loops like for and while.

With Functions

Albeit not being supported in anything else than Firefox, it’s still interesting to note that functions will also work without the curly braces, like this.

function myFunction()
  //code to execute;

Particularities

However, it’s important to know the omission of curly braces does not function exactly like regular statement blocks. In fact, it is only possible with one statement, after which the other statements will be executed as if they were out of the condition of function. This is due to JavaScript’s ignorance of Line Returns and Tabulations. Don’t forget JavaScript reads everything as one line, so the indentation shown here simply illustrates how useless it is to the code’s contructs.

The following two examples produce the exact same output.

With Curly Braces

var myVar = 10;
if (myVar == 10)
{
  document.write("myVar is equal to 10.<br>");
}
document.write("This block executes even if the if condition is not met.";

Without Curly Braces

var myVar = 10;
if (myVar == 10)
  document.write("myVar is equal to 10.<br>");
  document.write("This block executes even if the if condition is not met.";

Did you know empty tags in HTML were actually closed!?

As you know, in XHTML there is what we call self-closing tags, or empty tags if you want. One notable example is <img /> or <br />. However, since XHTML really is XML, <img></img> and <br></br> is also possible.
Note: The <br></br> is only correctly interpreted in Firefox and Safari.

But, big surprise, this is not possible in HTML! Wait, huh? Why wouldn’t it be. <br> and <img> alone just look like tags that aren’t closed, but that only applies to XML. In HTML, empty tags like <img> and <br> are closed, and typing <img></img> will give you a wonderful validation error saying no opening tag for </img> could be found.

So next time somebody says this while you’re editing HTML:
- Watch it man, that’s supposed to be a non-closed tag
You can answer:
- Uh huh, empty tag! (or self-closing)

BTW: I’m positively thinking HTML’s way of doing this doesn’t make sense. Who’s the idiot? (actually we know who) And that XML does away with that closing concept in a much more logical and consistent handling, I mean, if <img> just like that is already closed, why would <p> wouldn’t be?

Some Javascript should seriously be abolished

window.innerWidth = screen.width;
window.innerHeight = screen.height;

This in particular. Because, you know, some people beleive their choice is better than yours, so they force your browser window to full width and height everytime you enter their site by entering this code right in the body under a script tag.

Now, you may be like the multiple Windows users, addicts to everything maximized, which was a very reasonable thing to do on smaller screens. It may be the only thing I’ve always critized the Mac for, the lack of true window maximization. But, it does fit well on a super-sized screen Mac. A bit less “fit” on Windows though, I still think my browser window belongs in the “equivalent to 19″ monitor” width rather than a full 1920px. The reason is simple: the background color of a browser window is often white, and, as our monitors today are simply freakishly bright, having all that white in the background is like putting a flash-light on your eyes while you read. I personally prefer seeing just the right size browser window, full height of course (considering you have a widescreen monitor in paysage format), with the nuanced background of the computer. It’s much more pleasing to the eyes, and frankly, most appealing than the bland backgrounds web pages are often given.

Maybe someone should start a standards organization about that; ban the stupid codes, kind of like what happened to blink, and what should happen to animated gifs.

IE Identification is System-wide!

Edit: On another XP system, the behavior is totally different and IE 6 does identifiy as IE 6. Additionally, forcing IE 8  to display in “compatibility mode” makes it identify as IE 7. Problem solve, on some computers only I guess. Btw, no problem in Vista either.

Huh? What does this mean?

Well, today I was testing out stuff on IE, most notably, the famous <!–[if IE]> code. No, I’m not an anti-IE guy, although I’d wish Microsoft was faster at implementing standards, but they’re getting better. This means I was looking into using <!–[if lt IE 8]>, which means, if lower than IE 8.

My whole purpose is to put up an advert on my site, only if you have something lower than IE 8 (another IE), that tells the person to upgrade its browser, or they might not benifit from a full experience on my site. This is a nice and soft way to ease the people into newer, more compliant browsers. At least, less radical than blocking access to your site entirely or telling people to upgrade to Firefox unequivocally as if it was a god…

That appart, I tested out the thing with IE 8 and IE 6, with the help of the Multiple IE thingie. Well, it didn’t work. It never worked. The only time the thing would appear was if I put <!–[if IE8]>, which, while obviously right in IE 8, shouldn’t work in IE 6. What’s more, it worked for every other browser; Opera, Firefox nor Safari or Chrome were taking themselves for IE and were totally ignoring the code.

Great, but how do I make it work? Actually, the reality is it does work! Yes, because my IE 6 really does identify as IE 8, and every other IE on my system too. Yes, on Windows, IE identifies system-wide, so even if you managed to get multiple versions of IE on your system, they’ll all identify as the one you “regularily installed”, in my case, IE 8.

While this means I’ll have to use multiple computers to test it, it’s not like I really have to worry about it. I wouldn’t test IE 5, even if I wanted it to see it. Point made, if you must really make sure everything displays as you want, just shove in <!–[if IE8]> temporarily and you’re good for testing the visual style of your thing.

Man, why is that browser so entrenched in the system… I wonder if Safari does the same on OS X.
(BTW, this is on XP, the behavior may be completely different on Vista or 7, well, I hope so cause that’s what I’m testing on at home, work is XP)

Internet Explorer 8 still rendering on as 7 in Intranet (local) editing

In IE8, Tools > Compatibility View Settings > Uncheck “Display intranet sites in Compatibility View”.

Although this may come as a surprise for you, Microsoft does has a point doing that. In large organizations like governments, intranet applications that date back to IE 6’s omnipotency are very omnipresent. Thus, keeping backward compatibility by default on all Intranet websites is the safest choice to make. There’s probably more than a ten-fold difference between the amount of corporate intranet users and home intranet users.

CSS Tables – The Limitations

Oh, CSS, you’ll never give up do you. So, think again, float is not dead. How’s that? First off, take a CSS table for layout. Secondly, realize that, in a table, there is no way to seperate table blocks, except if you put something in-between.

What used to be this with floats:

<div class="table_table">
<div class="table_row">
<div class="table_cell"></div>
<div class="table_cell"></div>
<div class="table_cell"></div>
</div>
</div>

Is now this ugly thing with CSS tables:

<div class="table_table">
<div class="table_row">
<div class="table_cell"></div>
<div class="table_cell_spacer"></div>
<div class="table_cell"></div>
<div class="table_cell_spacer"></div>
<div class="table_cell"></div>
</div>
</div>

(Sorry for the lack of code indentation, WordPress is acting stupid)

Anyways… spacers! Oh god, no. Luckily, borders can aproximate spacing and most image-based design cuts won’t need those margins in-between blocks. Actually, the fortunate news is that about 90% of the time, the spaces between blocks is what you don’t want.

I don’t think CSS tables are going to make CSS logic that much easier. If you were using floats in a good way, chances are you won’t have to change much in your HTML structure. I didn’t say you won’t have to change anything! Yes, CSS tables eliminate the need for container blocks or float clears everywhere and yes, CSS tables will fix a lot of bugs and make possible a lot of cool things; like menues that expand with content and 100% height properties that base themselves on the browser window, not parent CSS block, oh god yes.

CSS Tables – The Next Best Thing Since Ice-Cream

If your a new web developer, you’ll be amazed at how logical today’s world of the web can be. Learning CSS tables firsthand in CSS would be dream-like; I envy future students.

But for modern web developers, for the few who truly master the trickery art of CSS design, CSS table is nothing short of amazing. While reading Everything You Know About CSS is Wrong from Sitepoint, you seriously have to buy this book, my jaw dropped. I couldn’t stop reading the basics of CSS tables with a constantly growing excitement to re-code all of my sites in CSS tables.

Really, the next best thing since the invention of ice-cream, or chocolate, or whatever godly desert basis you can think of, tasting CSS tables after having been proclamed CSS guru by your friends is heavenly. I know a lot in CSS. In my short life-span of web developement carreer, nothing has ever been so cool to learn.

To give you a good idea of my history, when I entered college (we call it Cégep here in the province of Québec in Canada, and it’s before University), I knew nothing about HTML and CSS and was learning to do everything via Dreamweaver’s WYSIWYG interface. Well, so long for those wasted hours, at least I know Dreamweaver’s interface by heart. I quickly realized how coding directly was superior. By the half of my first college year, I was coding without a WYSIWYG in all of CSS’s complex floating glory, learning mind-bending tricks I have yet to see in use by anyone else than some elites on other parts of the planet. So no, I haven’t met anyone better than me in CSS in person, yet. I am now in my third year of college.

Keep in mind though that layout is just half of CSS, the other accessibility part being largely ignored and unexciting, it’s always been something I could count on when it comes to showing off CSS skills. You don’t necessarily know what’s the difference between an <i> tag and an <em> tag if you know how to use float CSS.

But enough talk. Download the free chapters of Sitepoint’s book and learn ahead. (I thought about puting the information here directly, but Sitepoint deserves too much credit for publishing that book).