Returning reference type passed to method

I’m asking this from .NET (C#) point-of-view, however, I’d preferire language agnostic answer.

Is it better to return the reference type when some value is changed in method or not? I’m mostly concerned about readability, but I’d like to here others pros & cons if there are any.

For Example, we have the following structure

struct PersonInformation
{
    internal string name;
    internal int year;
}

Example 1:

private void Caller()
{
    var person = new PersonInformation;
    person = GetPersonInfo(person);
}

private PersonalInformation GetPersonInfo(PersonalInformation person)
{
     person.name = "Mike";
     person.year = 19;
     return person;
}

Example 2:

private void Caller()
{
    var person = new PersonInformation;
    GetPersonName(person);
}

private void GetPersonInfo(PersonalInformation person)
{
        person.name = "Mike";
        person.year = 19;

}

2

Is it better to return the reference type when some value is changed in method or not?

To expand on Doval’s comment, it’s far better to not cause side effects at all. Side effects make your code harder to reason about. They make your code harder to reuse. They make your code harder to test. They make it way harder to use concurrently.

These examples have their own problems. If your function is named GetPersonInfo why is it not returning anything – I mean to get something, you necessarily need to return it. For the other signature, why are you calling GetPersonInfo when you already have a person info? Both of these signatures are uncommon in C# (and other languages) because they don’t follow the principle of least surprise. Programmers cannot simply look at the signature and reasonably guess what is going on. That leads to slowness writing code and it leads to bugs.

No, a better option would be to pass in some data to find the person you want and return (an ideally immutable) instance of person info.

I generally prefer to follow the Command-Query Separation Principle. The principle states that if you change information it is a Command, and then it returns nothing.

If the method call is a Query, it basically retrieves information and it should return with the correct type.

If you are retrieving the information of the person, the getPersonInfo() name is ok, but maybe you should pass the details of what person are you getting. For example

db.getPersonInfoByName(“Mike”);

Following the CQS, the signature of getPersonInfoByname should return PersonInformation.

In the case you would like to change the age of the person, you would have a method which returns void because it changes information.

void setBirthDate(date birthDate)

TL;DR

Is it better to return the reference type when some value is changed
in method or not?

When changing values no reference type should be returned according to the Command-Query Separation Principle. If a function has side-effects, return void.

This allows a programmer to immediately recognize which functions contains side-effect and which does not.

4

I think that having a void (mutator) method that starts with “Get” is a code smell on its own. Method names must be clear, descriptive and trustworthy. Your first example is better than the second one, however, I would not call it a good method.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật