Tip #209: Who’s your sister?

When it comes to the coding, not always but often, less means better, faster and more manageable code. I’ve recently come across of someone else’s (could it have been me 4 years ago?) piece of work updating bulk email setting for the contact:

Contact contact = 
    context.Retrieve(
        "contact", contactId, 
        new ColumnSet(true))
    .ToEntity<Contact>();
            
contact.DoNotBulkEMail = true;
context.Update(contact);

Sigh. The “all columns” sin aside, the retrieve is absolutely unnecessary. You know what to update, why wouldn’t you just send that information over and let the server do its job?

context.Update(
   new Contact()
   {
      Id = contactId,
      DoNotBulkEMail = true
   });

And no, “new” operator will not create a new contact record. Always remember, brevity is the sister of the talent.

Share on FacebookTweet about this on TwitterShare on Google+

Leave a Reply

Your email address will not be published. Required fields are marked *