C++ vs Objective-C design principles of setters and getters

In C++ I was told that my classes shouldn’t have getters and setters: Doing it wrong… blog post.

On the other hand, in Objective-C, the direct use of ivar is discouraged, because the getters allow to do lazy instantiation, and setters make KVO easy.

Why is that difference?

2

I think the best takeaway from the “Doing it wrong…” post is that exposing public accessors without a good reason breaks encapsulation by giving external classes the ability to modify and inspect state that ought not be made public. It’s just expressed in an absolutist fashion (I’d argue if you have a Contact object, and want to put that contact’s name on a label for an address book application, at some point Contact will probably have to expose its name to another object). Regardless, no matter what the language, making accessors available without thought is going to break encapsulation and lead to tight coupling.

This is orthogonal to the best practice in Objective-C of using accessors for all (at least most) instance variable access. No where does Objective-C recommend making all your accessors publicly visible. Often you will have a class extension visible only to the class’s own implementation that defines most of its instance variables and then the class’s own implementation uses the accessors. And certainly, external code should always use the accessors – but they still should only be made visible to external code if absolutely necessary.

7

Setters and getters are the technology that came from visual basic properties in 1980’s. Since they’re often used together with the programming techniques that also come from same place, it’s considered bad practise. Problem with getters and setters is that the behaviour of the class gets moved to the user of the class, and class no longer handles it’s own behaviour; but supports any behaviour possible. This does not create a good class, since it’s not doing the decisions that are normally part of class’ responsibility.

0

As a C# developer, I use getters/setters all the time. Since I rarely (but still do) use C++, I might could offer a different perspective on this one.

To my eye, the bulk of difference of acceptability between C++ and C# has been due to conventions between users of a language (though there are others I’ll get to later…). Most of the time in C++, I just don’t need to use a getter/setter because it is not encouraged to do so. Unlike the blog post, I don’t even view it as a matter of ‘right way to code’ or ‘wrong way to code.’ It’s a convention that allows other humans to make heads or tails of what I’ve written/maintained.

C# does not mitigate that need, and as far as I’ve seen, there’s no evidence to say that getters/setters in C# are any more or less peformant than methods in C++ (I would assume they’re on roughly the same order, but that’s a gut feeling, there.) Yet, the general feeling in the C# community is that, while methods are preferable, properties are great for taking methods like:

public string GetName()
{
    return _lastName + ", " + _firstName + " " + _middleInitial;
}

public string SetName(string fullName)
{
    // Assumptions are made here; this is just example code!
    string[] frags = fullName.Split(' ');
    _firstName = frags[0];
    _middleName = frags[1];
    _lastName = frags[2];
}

and creating a slightly ‘shorter’, different, but reasonably readable syntax like:

public string Name
{
    get { return _lastName + ", " + _firstName + " " + _middleInitial; }
    private set 
    {
        string[] frags = value.Split(' ');
        _firstName = frags[0];
        _middleName = frags[1];
        _lastName = frags[2];
    }
}

You could say that to a C# or Java programmer, we view properties as syntactic sugar for simple methods, since that’s all a getter/setter really is: a method.

Another wrinkle in the technologies lies in various frameworks. For instance, in my current shop, our MVC projects are set up such that we’re more or less required to use getters and setters by NHibernate and the MVC framework itself. We’ve had conversations about the perceived overabundance of properties in our app, but in the end we had no technical recourse to revise those to simple methods. We have to use properties because reflection is used to retrieve properties, and thus their contents. Our code would be bigger and nastier without them, so no one really complains all that much.

To finally answer your question, though, I think that dives to the heart of the matter.

A C++ programmer expects to need the deep technical details of a given operation; instead of obscuring this machination with syntactic sugar, it’s more understandable to just bite the bullet and do the work. It’s not that C++ programmers are averse to syntactic sugar at all (in fact, I find myself yearning for better ways to express my C++ code when I use it…), but the things you turn to C++ for and the things you use properties for typically don’t mesh well on a practical level*.

A C# or Java programmer is taking advantage of the virtual machine interposed between the physical layer and our high-level code; we lose little by utilizing the syntactic sugar, and in return gain (slightly) improved readability without wholly violating encapsulation…providing the getters/setters are implemented correctly. The things you turn to C# or Java for do mesh well with properties on a practical level.

*: Note that I’m not a frequent C++ user! I beg for correction, if anyone has any to give.

1

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