AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 19.07.2018, 14:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
crmtipoftheday: Tip #1137: Learn language and study API to simplify your code
Источник: https://crmtipoftheday.com/1137/lear...ify-your-code/
==============

I already wrote about the benefits of learning new language features. Equally important is to understand what assemblies are available as part of Dynamics 365 SDK, how they work, what classes and interfaces are available, and how the edge cases work (basically, “what-if” scenarios, like “what if this is null”).

This week I had “privilege” to sink my teeth into some old code written by a developer with a particularly severe case of spießrutenlaufen. My eyes are still bleeding. Consider this fragment:

DateTime? DateAccepted = null;if(entity != null) { DateAccepted = (entity.Contains("new_dateacc") && entity["new_dateacc"] != null && entity["new_dateacc"] != string.Empty) ? entity.GetValue("new_dateacc") : (DateTime?)null;}I did some line wrapping for your viewing pleasure, original code was a one-liner. At this point in time you should be able to hear my eyes rolling all the way to the back of my head. How about this instead?

var DateAccepted = entity?.GetAttributeValue("new_dateacc");There is no magic here, I simply used:
If you are unsure how things would work out if, for example, you used GetAttributeValue, why don’t you whip out some quick code to test the behavior:

As you can see, return seems to be default(T) for non-nullable T types. Yep, I’m using the goodness of LINQPad, a must have for every developer.

(Social media cover photo by Easton Oliver on Unsplash)



Источник: https://crmtipoftheday.com/1137/lear...ify-your-code/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 12:28.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.