考虑这个婴儿简单的代码
pg.connect( {
user: 'hhope',
password: '...',
port: 5432,
host: '/var/run/postgresql',
database: 'phiddler' },
function( err, client ) {
client.query(
"select count(1) as ct from pg_prepared_statements",
function( err, results ) {
console.log( 'prepared statement count: '+results.rows[0].ct );
client.query( {
name: 'test2' ,
text: "insert into t1( c2 ) values( 'q1')" },
function( err, result ) {
console.log( 'inserted t1' );
client.query(
"select count(1) as ct from pg_prepared_statements",
function( err, results ) {
console.log( 'prepared statement count: '+results.rows[0].ct );
} );
}
);
}
);
}
);
如果按程序写成
pg = new PG(...);
client = new pg.client();
console.log( client.col( "select ..." ) );
client.prepare( 'test2', "insert into t1( c2 ) values( 'q1')" );
console.log( client.col( "select ..." ) );
在我看来,后者更具可读性和可维护性。
是否有某种折衷方法可以使标准缩进回调样式更具可读性?