1
sottwell
Why sprintf?
  • 2004/7/2 22:06

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


I have a question. Why do you use sprintf when defining the sql query?

$sql = sprintf("...

2
Dave_L
Re: Why sprintf?
  • 2004/7/3 1:30

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


It's just a convenient way of inserting expressions into a string.

3
sottwell
Re: Why sprintf?
  • 2004/7/3 3:37

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23



4
Dave_L
Re: Why sprintf?
  • 2004/7/3 12:05

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


That's correct. sprintf is slower than concatenation.

5
Brad
Re: Why sprintf?
  • 2004/7/3 12:45

  • Brad

  • Not too shy to talk

  • Posts: 150

  • Since: 2003/12/4


Quote:
Dave_L wrote:
That's correct. sprintf is slower than concatenation.

To add to that... Slower isn't always a bad thing. Code maintainability is just as important as absolute speed.

If it was slower by, say, more than a second, then it could be an issue. If we're talking a difference of less than a second... Not an issue.

6
sottwell
Re: Why sprintf?
  • 2004/7/3 13:28

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


Just wanted to know if there was some reason why I should use it, if I don't want to. Thanks.

7
Dave_L
Re: Why sprintf?
  • 2004/7/3 13:35

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I mostly agree with Brad. Maintainability is very important.

But a second can be a significant delay for a web page, especially in combination with delays caused by other factors. Howver, I'm sure that a single sprintf takes much less than a second.

Personally, I try to avoid using sprintf simply to achieve string concatenation.

Here's my preferred way of doing queries:

Example 1:

$challenges_table $xoopsDB->prefix('chess_challenges');

$result $xoopsDB->query("SELECT player1_uid FROM $challenges_table WHERE challenge_id = '$challenge_id'");


If the query is long, I break it up into separate lines to make it more readable.

Example 2:

$challenges_table $xoopsDB->prefix('chess_challenges');

$result $xoopsDB->query(trim("
   SELECT game_type, fen, color_option, player1_uid, player2_uid, UNIX_TIMESTAMP(create_date) as create_date
   FROM   
$challenges_table
   WHERE  challenge_id = '
$challenge_id'
"
));


(In XOOPS 2.0.7, the trim() isn't needed, since XoopsMySQLDatabaseProxy::query was modified to strip leading whitespace.)

8
Brad
Re: Why sprintf?
  • 2004/7/3 17:50

  • Brad

  • Not too shy to talk

  • Posts: 150

  • Since: 2003/12/4




To be clear... I wasn't saying my preference was to use sprintf(). At this time I don't have much of a preference although your two examples do look circus seal enticing.

I was just saying that in general, maintainability is very important and shouldn't be overriden by speed issues alone.

I think you *fully* agree with me, Dave.

Login

Who's Online

47 user(s) are online (32 user(s) are browsing Support Forums)


Members: 0


Guests: 47


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Aug 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits