Tuesday, December 27, 2011
« Make the Ugly Scrollbars on your Faceboo... | Main | The Finnish Education System »

I'm a big fan of good naming in code, here's a recent example:

Suppose you have a unique index in a database table and you're trusting that index to enforce no more than one record with the key.

So you're using an insert ignore into...on duplicate key update statement.

So you end up calling something like DataAccess.InsertRecord(data) or DataAccess.AddRecord(data). Looking at such code it's very unclear that what really happens is an insert/update and you're only left with one record.

You can go the way of making your code explicit my moving the logic into your app and doing something like

var record=DataAccess.GetRecord(key);
if(record == null)
  DataAccess.InsertRecord(data);

But then you'll be losing the power of using the database do that for you.

So what I'm suggesting is just making your naming better, for example: DataAccess.ReplaceRecord(data).

Want to join me as partner in a cool new startup?
Get in touch: pasha at cohai dot co

Bookmark and Share Tuesday, December 27, 2011 9:34:39 PM (Jerusalem Standard Time, UTC+02:00)  #    Comments [7]  
Tuesday, December 27, 2011 10:30:55 PM (Jerusalem Standard Time, UTC+02:00)
Upsert() is the correct convention to use
Eyal
Wednesday, December 28, 2011 1:11:53 AM (Jerusalem Standard Time, UTC+02:00)
Correct, I was just about to comment that Replace assumes a record exists when I saw @Eyal's comment.
Good names are often difficult to find.

See also: http://stackoverflow.com/questions/1866794/naming-classes-how-to-avoid-calling-everything-a-whatevermanager
Thursday, December 29, 2011 7:33:04 PM (Jerusalem Standard Time, UTC+02:00)
"Upsert" is indeed correct but is very 1995 ;)

Who talks like that?

But yeah, it will def work too
Pasha
Saturday, January 14, 2012 5:49:22 AM (Jerusalem Standard Time, UTC+02:00)
I don't even know what to say, this made things so much eiaser!
Saturday, January 14, 2012 5:07:19 PM (Jerusalem Standard Time, UTC+02:00)
p2l9Dl <a href="http://ijcnymbqmlqh.com/">ijcnymbqmlqh</a>
Sunday, January 15, 2012 7:04:16 PM (Jerusalem Standard Time, UTC+02:00)
BQE5u1 , [url=http://ufdqnzcwdmmn.com/]ufdqnzcwdmmn[/url], [link=http://mnyuwmygwshw.com/]mnyuwmygwshw[/link], http://qwfrhkqjidbf.com/
Monday, January 16, 2012 6:58:14 PM (Jerusalem Standard Time, UTC+02:00)
e52AJy <a href="http://vyxmgkcacmfz.com/">vyxmgkcacmfz</a>
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):