Archive for the ‘Programming’ Category

Best Programming Practices #1 – multiple files

Tuesday, December 29th, 2009

When I first started out as a programmer, I didn’t have much of a concept of best programming practices. If anything, I saw them as barriers in the way of doing what I really wanted to do. And although there are plenty of people on the web willing to yell at you when you don’t follow good programming practices, there aren’t so many good explanations for beginners on what makes those practices good. So this is the start of a series of quick tips on good programming practices and what makes them good.

Tip #1: Divide your program up into multiple files.

This used to drive me crazy when I first started as a programmer. I would go in to work on a program and find five hundred separate little files, which would force me to hunt laboriously through them in order to find what I wanted. I disliked this practice so much, I would often rewrite other people’s programs to put all the little files into one big one. But guess what? There were good reasons behind it all:

  • Debugging:  If something goes wrong with your program –and it will– it’s much easier to disable one included file at a time then to search through a single massive program.
  • Reusability: Some portions of your code will be used over and over again, either within the same program or in a wholly separate project.  Save that code as a separate file, and you can use it anywhere, no cutting and pasting involved.
  • Structure:  A properly named and organized system of subdirectories can make the organization of your program clear at a glance.

Of course, the key to making this tip work for you is organization.  It isn’t helpful at all to look through five hundred mysteriously named and ordered subroutines.  But if each separate file has a name that clearly identifies what it does, and is placed in a folder with other similar files, it eliminates a lot of the fruitless searching.

PHP MySQL Dump or Export

Tuesday, November 3rd, 2009

I searched all over for this information, and never found it in a easy-to-use format:

Here’s the situation.  You have php access to a MySQL –or other SQL variant– server.  You don’t have phpMyAdmin, you don’t have command line access.  You would like to export some of the data, but not necessarily all of it, because perhaps the full database is too large.

The SQL command that makes this simple is: “SHOW CREATE TABLE table_name

Use that as your query, and the output will be the complete SQL command for creating a table.  The only other thing you need is to turn the data output into insert statements, which is trivial.

Here’s a complete script –notice how short it is:

mysql_connect($sql_host, $sql_user, $sql_password) or exit(mysql_error());
mysql_select_db($sql_db) or exit(mysql_error());
$result=mysql_query("SHOW TABLES");
echo mysql_error();
while ($row=mysql_fetch_array($result)){
$tableName=$row[0];
$query2="SHOW CREATE TABLE $tableName";
$result2 = mysql_query($query2);
$row2=mysql_fetch_array($result2);
echo "$row2[1];\n";
$query3      = "SELECT * FROM $tableName";
$result3 = mysql_query($query3);
while ($row3=mysql_fetch_array($result3)){
$header_array=array();
$line_array=array();
foreach ($row3 as $key=>$value){
if (!is_numeric($key)){
$header_array[]="$key";
$line_array[]="'$value'";
}
}
echo "INSERT INTO $tableName (".implode(", ", $header_array).") VALUES (".implode(", ",$line_array).");\n";
}
}
}
mysql_close();

Links For Web Design

Tuesday, September 15th, 2009

Here’s a list of some good links. A lot of these are great for beginners –they are places I relied on many years ago when I was first starting in Web Design.

http://www.cdilearn.com/cmat/html/crossref.html - HTML tags. Anyone who works in web design should know basic HTML –there’s no excuse not to, no matter how shiny and full featured your web page editor might be.

http://www.ascii.cl/htmlcodes.htm – ASCII codes. You’ll need these to go along with your HTML.

http://www.tizag.com/ – They have some excellent beginner’s tutorials for php, asp, mysql and other web essentials.

Those three are pages I’ve used for years –now here’s one I just found today: a clear concise cheatsheet for .htaccess regex (regular expressions redirects).

http://forums.doctornuke.com/apache-web-http-server/423-tips-regex-code-htaccess.html

Object Oriented Programming

Wednesday, September 9th, 2009

For years I resisted object oriented programming. If you’re used to linear programming it can be quite a mental adjustment. But there are some very good reasons that the object-oriented approach has revolutionized programming.

Perhaps the easiest way to understand the “objects” of object oriented programming are as specialized stand-alone programs. There are ways to get data into them, ways to get data out of them, and functions that do things with the data.

The important part of this idea is “stand-alone”. The nice thing about the objects is that nothing that happens inside of them directly affects the outside world. This makes it easy for a programmer to use many objects together, even if some of the objects were programmed by someone else. There’s no worry that you might accidentally use the same name for two different variables, or that your programming might conflict with the other person’s methods.

In addition, since each object has a limited, specialized function, if there’s a programming error, it’s easy to find the source, isolate the problem, and fix it without breaking other parts of your program.

The final value of object oriented programming is that it makes it easier to read programming code (at least assuming you understand the OOP programming conventions and syntax!). This is because your main program now contains only the large overall structure of the program. When you get to an object, assuming it is well-named, you can tell what it does at a glance.

Although objects can be quite complex on the inside, if you do ever have to get inside an object and tinker with it, the fact that you know it is designed for specific limited tasks will help you understand what you are looking at. In addition, the fact that objects can contain other objects means that even the inside of an object can benefit from the same simplifying effect as does the main program.

In summary: programmers, don’t be frightened by OOP. An object is nothing but a self-contained program with input, output and internal transformations.