views:

1953

answers:

8

I'm adding a field to a member table for twitter names for members on a site. From what I can work out the maximum twitter name length is 20 so it seems obvious that I should set the field size to varchar(20) (SQL Server).

Is this a good idea?

What if Twitter starts allowing multi-byte characters in the user names? Should I make this field nvarchar?

What if Twitter decides to increase the size of a username? Should I make it 50 instead and then warn a user if they enter a name longer than 20?

I'm trying to code defensively so that I can reduce the chances of modifying the code around this input field and the DB schema changes that might be needed.

A: 

[opinion only]

  • Twitter works on SMS and the limit there is something like 256 characters, so the name has to be small to avoid hitting into the message.
  • nvarchar would be a good idea for all twitter text
  • If the real ID of a Twitterer is a cell-phone then the longest phone number is your max - 20 should easily cover it!
  • Defensive programming is always good :) !

[/opinion only]

dcpking
<for info>SMS text limit is 168 chars.</for info>
Hooloovoo
+3  A: 

Nowadays, space is usually not a concern, so I'd use a mostly generic approach: use nvarchar(200).

When designing DB schemas you must think 2 steps ahead, even more than when programming. Or get yourself a good schema update strategy, then you'll be fine also with varchar(20).

MicSim
+1  A: 

There's only so much you can code defensively, I'd suggest looking at the twitter API documentation and following anything specified there. That said, from a cursory look through nowhere seems to specify the length of the username, annoyingly :/

Rob
+1  A: 

Personally I wouldn't worry. Use something like 200 (or a nice round number like 256) and you won't have this problem. The limit then is on their API, so you might be best to do some verification that it is a real username anyway. That verification implicitly includes the length checking.

Neil Barnwell
I love this response, "a nice round number like 256," Didn't even strike me as odd until I read it a second time.
Rob
A: 

I'm guessing you are managing the data entry on the Twitter name field in your application somewhere other than just in the database. If you open the field to 200 characters, you only have to change the code in one place or if you allow users to enter Twitters names with more than 20 characters, you don't have to worry about a change at all.

Jeff O
+2  A: 

Twitter allows for 140 characters to be typed in as the message payload for transmission, and includes "[username]:" at the beginning of the SMS message. With an upper limit of 140 characters for the message combined with the messaging system being based on SMS, I think they would have to decrease the allowable message size to increase the username. I think it is a pretty safe bet that 20 characters would be the max username length. I'd use nvarchar just in case someone uses 16-bit characters, and maybe pad it a little. nvarchar(24) should work; I wouldn't go any higher than nvarchar(32).

If you're going to develop an app for their service, you should probably watch the messages on Twitter's API Announcements mailing list.

Jeremy Seghi
A: 

One thing to keep in mind here is that a field using nvarchar needs twice as much space, since it needs 2 bytes to store each potential unicode character. So, a twitter status would need a size of 280 using nvarchar, PLUS some more for possible retweets, as those aren't inlcuded in the 140 char limit. I discovered this just today in fact!

For example:

RT @chatrbyte: here's some great tweet that I'm retweeting.

The RT @chatrbyte: is not included in the 140 character limit.

So, assuming that a Twitter username has a 20 character limit, and wanting to also capture a ReTweet, a field to hold a full tweet would need to be a nvarchar of size 280 + 40 (for the username) + 8 (for the initial RT @ before a retweet) +4 (for the :+space after a Retweet username) = 330.

I would say go for nvarchar(350) to give yourself a little room. That's what I am trying right now. If I'm wrong I'll update here.

Michael A
+1  A: 

while looking for the same info i found the following in a sort of weird place in the twitter help section (why not in the API docs? who knows?):

"Your user name can contain up to 15 characters. Why no more? Because we append your user name to your 140 characters on outgoing SMS updates and IM messages. If your name is longer than 15 characters, your message would be too long to send in a single text message."

http://help.twitter.com/entries/14609-how-to-change-your-username

so perhaps one could even get away with varchar(16)

jsh