Posts Tagged ‘language’

SQL is Not C

Thursday, July 24th, 2008

C is the canonical procedural programming language and the greatest pitfall for a programmer looking to show off his database-fu is that he fails to realize that SQL is not procedural (nor is it functional or object-oriented, for that matter). Rather than thinking in terms of data and operations on data one must think of sets of data and relationships among those sets. This usually crops up with the improper use of a subquery:
SELECT a.id,
(SELECT MAX(created)
FROM posts
WHERE author_id = a.id)
AS latest_post
FROM authors a

Since this subquery is correlated, i.e., references a table in the outer query, one should convert the subquery to a join.
SELECT a.id, MAX(p.created) AS latest_post
FROM authors a
INNER JOIN posts p
ON (a.id = p.author_id)
GROUP BY a.id

The JavaScript language

Wednesday, July 16th, 2008

JavaScript is not a programming language in strict sense. Instead, it is a scripting language because it uses the browser to do the dirty work. If you command an image to be replaced by another one, JavaScript tells the browser to go do it. Because the browser actually does the work, you only need to pull some strings by writing some relatively easy lines of code. That’s what makes JavaScript an easy language to start with.

But don’t be fooled by some beginner’s luck: JavaScript can be pretty difficult, too. First of all, despite its simple appearance it is a full fledged programming language: it is possible to write quite complex programs in JavaScript. This is rarely necessary when dealing with web pages, but it is possible. This means that there are some complex programming structures that you’ll only understand after protracted studies.

Secondly, and more importantly, there are the browser differences. Though modern web browsers all support JavaScript, there is no sacred law that says they should support exactly the same JavaScript. A large part of this site is devoted to exploring and explaining these browser differences and finding ways to cope with them.

So basic JavaScript is easy to learn, but when you start writing advanced scripts browser differences (and occasionally syntactic problems) will creep up.

JavaScript vs Java

Wednesday, July 16th, 2008

Although the names are much alike, JavaScript is primarily a scripting language for use within HTML pages, while Java is a real programming language that does quite different things from JavaScript. In addition Java is much harder to learn. It was developed by Sun for use in pretty much anything that needs some computing power.

JavaScript was developed by Brendan Eich, then working at Netscape, as a client side scripting language (even though there’s no fundamental reason why it can’t be used in a server side environment).

Originally the language was called Live Script, but when it was about to be released Java had become immensely popular (and slightly hypey). At the last possible moment Netscape changed the name of its scripting language to “JavaScript”. This was done purely for marketing reasons. Worse, Eich was ordered to “make it look like Java”. This has given rise to the idea that JavaScript is a “dumbed-down” version of Java. Unfortunately there’s not the slightest shred of truth in this story.

Java and JavaScript both descend from C and C++, but the languages (or rather, their ancestors) have gone in quite different directions. You can see them as distantly related cousins. Both are object oriented (though this is less important in JavaScript than in many other languages) and they share some syntax, but the differences are more important than the similarities.

If you are a C++ or Java programmer you will be surprised by some of JavaScript’s features. Since I don’t have any previous programming experience, the differences are not described on this site. The best you can do is buy David Flanagan, “JavaScript, the Definitive Guide”, 5th edition, O’Reilly, 2006. In this book the differences between C++/Java and JavaScript are clearly explained. I co–edited a few chapters of this book.

Creating a First Visit Web Page Pop Up Window

Monday, July 14th, 2008

It’s a proven fact that the use of popup windows is an effective marketing technique that produces great results. However, they can be very irritating to your visitors. How can you use this powerful marketing technique without offending your visitors? Compromise and use a popup window that only displays the first time your visitor enters your site.

Place the following code within the <BODY> of your web page.

<SCRIPT LANGUAGE=”JavaScript”>
<!–
function GetCookie(name) {
var arg=name+”=”;
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return “here”;
i=document.cookie.indexOf(” “,i)+1;
if (i==0) break;
}
return null;
}
var visit=GetCookie(”COOKIE1″);
if (visit==null){
var expire=new Date();
window.name = “thiswin”;
newwin=open(”yourpage.htm”, “dispwin”,
“width=450,height=455,scrollbars=yes,
menubar=no”);
expire=new Date(expire.getTime()+7776000000);
document.cookie=”COOKIE1=here; expires=”+expire;
}
// –>
</SCRIPT>

Change the text where indicated in red.

The “yourpage.htm” text specifies the popup window url.

Change the height and width to your preferred window size.

PHP:A PHP Guy’s Look At Python

Monday, June 30th, 2008

Against all odds, I found myself with a little spare time this week. Rather than do something sensible like clean the garage or get some exercise, I took the opportunity to learn a new programming language: Python.

Like may SitePoint readers, I cut my teeth on PHP. I’ve become very comfortable with it over the years, warts and all. PHP continues to be a dependable choice, but PHP hasn’t changed a whole lot lately. Meanwhile, the kinds of applications I’ve been working on have been growing dramatically in both size and complexity.

Python has a lot in common with PHP: it’s a dynamically typed, open source scripting language with excellent documentation and a thriving community around it. Both languages are also a little quirky when it comes to their handling of Unicode text.

Unlike PHP, Python wasn’t originally designed as a language for Web development—it’s a general programming language that just happens to have some excellent libraries and frameworks for building web sites, like Django. This may sound like an argument against Python, but it turns out that when you start writing bigger web applications, most of your code has nothing to do with HTML, and PHP’s HTML-friendly features just seem to …

Top 5 Search Engine Optimization Tips

Saturday, June 28th, 2008

You may have heard many times about the ways you can optimize your site for search engines. Optimizing your site can allow the search engines to extract the important information from your page, making your site appear for more relevant search queries. Search engines may also better rank your page, so it appears at higher positions within results pages.

I have compiled a list of the best 5 ways (in my opinion) to optimize your site for the search engines:

1. Put keywords in your title. Try to place keywords relevant to your site in your page title. This will give the search engines a better idea of what your site is about, and also allows searchers to quickly determine whether your site is relevant to them.
2. Use Headings. Using headings in your webpage gives some structure to your page, and makes searching the page for relevant information quick. Try to place some relevant keywords in the headings, which give a clue to what the paragraphs below it are about.
3. Use alt=”" in images. As images can’t be understood by search engines, you should provide an alternative textual description of the image. You can do this using the alt attribute of the HTML image tag. Here’s an example:

<img src=”dog.jpg” alt=”Dog jumping into the air” />

4. Use META tags. You should use META tags within your page, to describe what the document is about, as well as provide relevant keywords and define a language.
5. Use a sitemap. Create a sitemap to show all the pages of your site. I recommend you add your sitemap to Google’s Sitemap service, or Yahoo’s service.

SEO : Use Headings

Thursday, June 26th, 2008

In college and some high schools, essays are written using a standard guideline created by the Modern Language Association (MLA). These guidelines included how to write you cover page, title, paragraphs, how to cite references, etc. On the Web, we follow the W3C’s guidelines as well as commonly accepted “best practices” for organizing a web page.

Headings play an important role in organizing information, so be sure to include at least H1-H3 when assembling your page. Using cascading style Sheets (CSS), I was able to make my h1 at the top of this page more appealing. Here’s a piece of code you can pop into your heading:

<style type=”text/css”>

h1 font-size: 18px;

h2 font-size: 16px;

h3 font-size: 14px;

</style>

Since a page full of headings would look just plain silly, my SEO tip would be to fill in the blank space with paragraphs, ordered and unordered lists, images, and other content. Try to get at least 400+ words on each page.

Unicode support in PHP 6

Monday, June 16th, 2008

But to my surprise they had implemented much more. Not only will PHP 6 talk unicode and understand it, it will understand locals and will even understand full language definitions and convert between them. All wrapped in an object oriented way of working.