Click here to Skip to main content
15,314,282 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: For how much time I spend navel gazing about the way I code Pin
MKJCP1-Dec-21 4:00
MemberMKJCP1-Dec-21 4:00 
GeneralRe: For how much time I spend navel gazing about the way I code Pin
Matt McGuire1-Dec-21 4:54
professionalMatt McGuire1-Dec-21 4:54 
GeneralRe: For how much time I spend navel gazing about the way I code Pin
MikeTheFid1-Dec-21 5:09
MemberMikeTheFid1-Dec-21 5:09 
GeneralRe: For how much time I spend navel gazing about the way I code Pin
Martin ISDN1-Dec-21 5:40
MemberMartin ISDN1-Dec-21 5:40 
GeneralCCC 30-11-2021 Pin
pkfox29-Nov-21 21:28
professionalpkfox29-Nov-21 21:28 
GeneralRe: CCC 30-11-2021 Pin
Peter_in_278029-Nov-21 22:29
professionalPeter_in_278029-Nov-21 22:29 
GeneralRe: CCC 30-11-2021 - Winner !!! Pin
pkfox29-Nov-21 22:57
professionalpkfox29-Nov-21 22:57 
Questioncode sexiness question Pin
Super Lloyd29-Nov-21 19:57
MemberSuper Lloyd29-Nov-21 19:57 
The first method below is probably 3e-9 seconds faster per call than the second method...
And a code reviewer asked that I used that syntax
C#
private IMultipleComponentHandler SelectionHandler
        {
            get
            {
                if (m_selectionHandler == null)
                {
                    var objects = SelectedObject; // <== MAIN DIFFERENCE

<pre>
                if (objects is IMultipleComponentHandler handler)
                    return m_selectionHandler = handler;

                object[] collection;
                if (objects is IEnumerable e
                    && !objects.GetAttributes<IgnoreIEnumerableAttribute>().Any())
                {
                    collection = e as object[] ?? e.Cast<object>().ToArray();
                }
                else if (objects != null)
                {
                    collection = new[] { objects };
                }
                else
                {
                    collection = Array.Empty<object>();
                }
                return m_selectionHandler = new InspectorMultipleComponentHandler(collection);
            }
            return m_selectionHandler;
        }
    }
    private IMultipleComponentHandler m_selectionHandler;</pre>

but... that extra variable annoys me (var objects = SelectedObject;), I see it as increasing code complexity for little benefit. I prefer that simpler version
C#
private IMultipleComponentHandler SelectionHandler
   {
       get
       {
           if (m_selectionHandler == null)
           {
               if (SelectedObject is IMultipleComponentHandler handler)
                   return m_selectionHandler = handler;

               object[] collection;
               if (SelectedObject is IEnumerable e
                   && !SelectedObject.GetAttributes<IgnoreIEnumerableAttribute>().Any())
               {
                   collection = e as object[] ?? e.Cast<object>().ToArray();
               }
               else if (SelectedObject != null)
               {
                   collection = new[] { SelectedObject };
               }
               else
               {
                   collection = Array.Empty<object>();
               }
               return m_selectionHandler = new InspectorMultipleComponentHandler(collection);
           }
           return m_selectionHandler;
       }
   }
   private IMultipleComponentHandler m_selectionHandler;
What says you?

For the record this is in a view model, this code is absolutely NOT performance critical.
A new .NET Serializer
All in one Menu-Ribbon Bar
Taking over the world since 1371!

AnswerRe: code sexiness question Pin
Rage29-Nov-21 20:05
professionalRage29-Nov-21 20:05 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 20:13
MemberSuper Lloyd29-Nov-21 20:13 
GeneralRe: code sexiness question Pin
Rage29-Nov-21 20:19
professionalRage29-Nov-21 20:19 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 20:29
MemberSuper Lloyd29-Nov-21 20:29 
AnswerRe: code sexiness question Pin
HobbyProggy29-Nov-21 20:30
professionalHobbyProggy29-Nov-21 20:30 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 20:34
MemberSuper Lloyd29-Nov-21 20:34 
GeneralRe: code sexiness question Pin
HobbyProggy29-Nov-21 21:06
professionalHobbyProggy29-Nov-21 21:06 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 21:17
MemberSuper Lloyd29-Nov-21 21:17 
GeneralRe: code sexiness question Pin
HobbyProggy29-Nov-21 21:53
professionalHobbyProggy29-Nov-21 21:53 
AnswerRe: code sexiness question Pin
honey the codewitch29-Nov-21 21:22
mvahoney the codewitch29-Nov-21 21:22 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:28
MemberSuper Lloyd29-Nov-21 22:28 
GeneralRe: code sexiness question Pin
honey the codewitch29-Nov-21 22:54
mvahoney the codewitch29-Nov-21 22:54 
AnswerRe: code sexiness question Pin
Daniele Rota Nodari29-Nov-21 21:52
MemberDaniele Rota Nodari29-Nov-21 21:52 
AnswerRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:01
mveRichard Deeming29-Nov-21 22:01 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:22
MemberSuper Lloyd29-Nov-21 22:22 
GeneralRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:34
mveRichard Deeming29-Nov-21 22:34 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:39
MemberSuper Lloyd29-Nov-21 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

How important is naming to you in your code?
There are 2 hard problems in Computer Science: caching, naming and off-by-one errors
  Results   811 votes