Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: dbi style questions (code, discussion)

by talexb (Chancellor)
on Dec 29, 2001 at 08:02 UTC ( [id://135059]=note: print w/replies, xml ) Need Help??


in reply to dbi style questions (code, discussion)

All I can suggest is comment, comment, comment.

But let's look at some code. Your first snippet is

my $sth = $dbh -> prepare("insert into table1 (col1, col2, col2) value +s (?, ?, ?)");
This wraps a little ugly; I'd change it to
my $sth = $dbh->prepare( "insert into table1 (col1, col2, col2) values (?, ?, ?)");
That way, the entire SQL statement is on a line by itself. A little lower you have
my $updater = $dbh -> prepare("update table1 set col1 = ?, col2 = ?, c +ol3 = ? where col1 = ? and col2 = ? and col3 = ?");
I'd change that to
my $updater = $dbh -> prepare( "update table1 set col1 = ?, col2 = ?, col3 = ? \ where col1 = ? and col2 = ? and col3 = ?");
That makes the variables all line up (I know, it's not to everyone's taste.) Although I ponder at the logic of that statement .. if the row values are already set to those values, what good is setting them to those same values? Usually when I do an update, I get an id value for the row that I'm going to change, and do something like
my $updater = $dbh->prepare( "update table1 set col1 = ?, col2 = ?, col3 = ? where id = $ID");
Generally I believe you want to use placeholders like ? when you are going to be inserting or updating many rows. For one-offs it isn't really necessary.

In general style terms, I try to stick with an 80 character limit on line lengths. I may have a big monitor now, but maybe later I'll have to look at my code on my client's 14" screen in character mode. Then I be lookin' stoopid.

Make your code as readable as possible -- if you can't read your own code in three months time, what chance does anyone else have?

--t. alex

"Excellent. Release the hounds." -- Monty Burns.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://135059]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found