Is it necessary for you to write the return null?

Source: Internet
Author: User

The last article "you wrote Try...catch really necessary" aroused a lot of friends of the discussion. This time I found another problem in code review, that is, someone intentionally or unintentionally wrote the return null such code, for example:

       Public User GetUser (string userName, string password)        {            if (/*for some reason*/)                return null;                        return new User ();        

Is there a problem with wood?

In my opinion, there is no good reason why null should not be returned, because the user of the method does not know under what conditions it will get null, leading to the infamous NullReferenceException exception.

If a user cannot be obtained, we should throw a definite exception for the upper service to decide what to do with it:

       Public User GetUser (string userName, string password)        {            if (/*for some reason*/)               return new Specificexceptio N ("can ' t get this user because ....");                        return new User ();        

In the open source project I have read, I have hardly seen the notation of return null. The two LINQ methods that I can think of for the moment FirstOrDefault () and LastOrDefault () are also named by methods to remind the consumer that the method returns NULL.

Speaking of the FirstOrDefault () method reminds me of another mistake many people are prone to make:

       Public User Getuserbyid (Guid userId)        {            return list. FirstOrDefault (X=>x.userid==userid);        }

The FirstOrDefault () method is used in the database where the UserID only corresponds to one user, and I would recommend that you use the single () method decisively. You should use FirstOrDefault () to hide the bug for this purpose. you expect to have only one user, if the single () method throws an exception, it means that a bug appears and lets you find the bug in the first time.

F # introduces the option type in order to reduce the use of the null type, and when option is used as the return type of the function, the compiler will make an error if the undefined type is not processed by the consumer.

If the pattern match here forgets to write the None-> branch, the compiler will give an error, reminding you that you must handle the logic when the Invalidint value is none. But in C # The return null type, the compiler does not give us the help, so we should avoid return null such code, do you think?

Is it necessary for you to write the return null?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.