PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 28, 2020 3:06 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Super Truncate
PostPosted: Wed Aug 09, 2006 3:02 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 3:18 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Aug 31, 2005 5:58 pm
Posts: 396
Location: Arkansas, USA
Wowee. 53 passes for one function. Hard to find fault with that. :)

You might try turning it into a class. Interface ideas:
$truncator =& new Truncator('a message!', 'a message to append to the truncated string');
$truncator->truncate(7);

Interface-wise, it's not all that great; but you could . This would make the code a lot clearer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 3:51 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 4:08 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Aug 31, 2005 5:58 pm
Posts: 396
Location: Arkansas, USA
I agree, the interface is fine.

The code innards, however, could be improved by extracting methods. The code would do the same thing, but be easier to maintain. It is easiest to do this with classes.

If you want to keep your current interface, then I would recommend turning it into a class, and then making a function called truncate that behaves exactly as the one you have now. Instead of figuring the result itself, however, it would pass the work on to your class. The function would be a .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 4:18 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
ideas for additional tests:
Syntax: [ Download ] [ Hide ]
truncate('                   '); // truncation of string of spaces

truncate('ads           kjhdf'); // truncation does not touch the spaces inside the string (or does, since you didn't test for that, it's not clear from your test suit)

truncate('asd                '); // does it leave the trailing spaces

truncate('                asd'); // does it leave the leading spaces

truncate(''); // truncation of empty string

truncate(new stdClass); // truncation with invalid arguments


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 4:54 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK
Weirdan, all of thoes would cause a parse error because you haven't specified the 2nd parameter but I've added them in and I can tell you exactly what it would do
Syntax: [ Download ] [ Hide ]
truncate('                   ', 100) == '';

truncate('                   ', 10) == '';

truncate('ads           kjhdf', 100) == 'ads           kjhdf';

truncate('ads           kjhdf', 10) == 'ads';

truncate('asd                ', 10) == 'asd';

truncate('                asd', 4) == 'asd';

truncate('') == '';

truncate(new stdClass, 20) == 'object id 1'; // or something like that


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:02 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:09 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group