6 Replies - 284 Views - Last Post: 14 June 2019 - 09:29 AM

#1 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

No more null reference for lists in C#

Posted 08 June 2019 - 10:04 PM

Personally, I hate this code found in this blog post on Dev.to:
public static class ListExtension
{
    public static void Add<T>(this List<T> list, T value, out List<T> newInstance)
    {
        if (list == null) list = new List<T>();
        list?.Add(value);
        newInstance = list;
    }
}



When I first saw it, I felt like I was trying to keep from throwing up.

I hate it because it just completely breaks the expected style for using and writing C# code. Like do you really write code like:
list.Add("test",out list);


C# has followed the object oriented tradition of calling methods by doing object.verb(). Even with the coming of the fluent style, it is still effectively object.verb().verb().verb()...

I just consider it completely foreign to do object.verb(out object). I do understand that usage in some other programming languages, but why introduce it into C#. To me, that is like doing the play Les Miserables as in a Kabuki style.

Is This A Good Question/Topic? 0
  • +

Replies To: No more null reference for lists in C#

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,102
  • Joined: 12-December 12

Re: No more null reference for lists in C#

Posted 09 June 2019 - 02:40 AM

There has been, in my opinion, a lot of unnecessary, cosmetic, stuff added to c#. It might be 'cool' to use but will likely confuse the next developer for little gain.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,478
  • Joined: 12-June 08

Re: No more null reference for lists in C#

Posted 09 June 2019 - 08:48 AM

Yeah I agree. Half the time getting a new library or something off github will inevitably lead me down a hole which ends up being odd "helper" functionality built in that folk weren't expecting.

*le sigh*
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

Re: No more null reference for lists in C#

Posted 09 June 2019 - 12:03 PM

When I was working at Microsoft and the .NET Framework was still in it's early stages, one of the guiding principles everyone was reminded of was the "least surprise". The idea was that by designing for least surprise, adoption outside the company would be faster, as well as, after adoption, code reviews would be a lot easier because again "it just makes sense" and people could focus on the intent of the code, instead of thinking about any gotchas inherent in the library/API.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

Re: No more null reference for lists in C#

Posted 09 June 2019 - 12:08 PM

Alas, some gotchas still crept in. For example, the way bitmap backing files get locked by the WinForms bitmap control.
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,989
  • Joined: 24-August 11

Re: No more null reference for lists in C#

Posted 14 June 2019 - 09:21 AM

I think there's some value in kludging out solutions like that to problems as you hack at them, but there's an ethical obligation to delete them afterwards. Sure, writing boilerplate null check code can be a bit tedious, especially when you're passing the object many levels down the chain... but that hardly warrants breaking something as ubiquitous as List.Add. It can sometimes be hard to distinguish the exact line between clever and "too far", but I think this example specifically is clearly on the side of the latter.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 DarenR   User is online

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,113
  • Joined: 12-January 10

Re: No more null reference for lists in C#

Posted 14 June 2019 - 09:29 AM

as you hate that i cant stand lambda expressions-- i just find them to hard to debug and deal with
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1