Click to Play

Search + Social = Better ROI
Are you utilizing search and social media together? According to Lee Odden, CEO of TopRank Online Marketing, many people keep their search and social...

Recent Articles

Perl - The Most Popular Language No One Is Talking...
Even though PHP and ASP are the two new languages that have all the buzz currently, Perl is still a major player. It allows for faster loading websites and little overhead. It shouldn't be surprising, but Perl runs some of...

Perl 5.11.1 Enters The Scene
The march of progress continued last week with the release of Perl 5.11.1. This release - which, it should stressed, is a development release - incorporates some significant changes, and Perl programmers have...

Perl 5.11.0 Released
Perl 5.11.0 - which has been labeled "the result of over two years of development by a global community of developers" - is now available to everyone. It was released not long ago, and a plan regarding...

Perl Packaging Discussed, Altered
A new argument relating to the definition of Perl has arisen, and, perhaps surprisingly, the involved parties didn't arrive at some sort of impasse (even one of...

Three Cheers For Two-Day Perl Conference In Italy
Italy is one of those places that almost everyone wants to visit. It's hard to just drop everything and fly to another country for the heck of it, however, so if you're in...

Perl On The Command Line
One of the beautiful uses of Perl is to do quick and dirty string manipulations on the command line. In a Unix environment this is especially nice as the output...


11.24.09

Formatting Your Perl Code

By Rodney Sellers

I have worked with many programmers working with many different languages, so I am used to seeing code formatted different ways. The problem I run into and more often than I should, is when someone has a long program written to the left margin or improper formatting.

Once in college I ran into a program that started out left margin, then switched to single space indent then to tab indent at the end. I am still not sure it was all written by different people or if the guy just wrote it that way. My personal preference is tab indenting. It allows you to see what is in a loop and what is not very easily. Here is a very simple version of how I indent.

While ($number != 4)
{
print "inside loop\n";
$number++;
}

There are many variations on this format from double space indents to putting the { on the same line with the while loop.

While ($number != 4){
print "inside loop\n";
$number++;
}

To me, the first is easier to read if you have a large number of nested loops but it can get very large very quickly.

In college, I had a database professor that had a format style that I still have a lot of trouble reading. It is when you indent only the fist line of a loop.

While ($number != 4) {
print "inside loop\n";
$number++;
}


When you have a large number of loops nested together it can get very confusing, but by the end of the semester, I was able to read it... kind of.

My least favorite format style is when you indent the second line of the loop and nothing else.

While ($number != 4) {
print "inside loop\n";
$number++;
}

This format was the favorite of many of my professors in electoral engineering classes. I asked one of my professors once and he said it was easier to look at FORTRAN code that way.

Here are 4 different types of formatting your code. To make it easier for yourself or others to maintain, the code is to pick a format style and stick with it. If you are modifying someone else's code, try to stick with their style. It makes the code flow better and easier to understand.

About the Author:
Rodney Is A Staff Writter for iEntry.
About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals





PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com






-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2009 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde