{"id":1324,"date":"2023-08-16T05:00:00","date_gmt":"2023-08-16T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1324"},"modified":"2023-08-15T16:44:52","modified_gmt":"2023-08-15T23:44:52","slug":"best-practices-for-using-visibility-modifiers","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/best-practices-for-using-visibility-modifiers\/","title":{"rendered":"Best practices for using visibility modifiers"},"content":{"rendered":"\n<p>Yesterday, we talked about <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/typescript-visibility-modifiers\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Typescript visibility modifiers<\/a> in the context of Angular classes. Today, I want to focus on best practices and common scenarios that involve such visibility modifiers. <\/p>\n\n\n\n<p>First, it&#8217;s very common to have Angular services that use <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/subject-behaviorsubject-and-replaysubject\/\" target=\"_blank\" rel=\"noopener\" title=\"\">RxJs Subjects<\/a>. Subjects are somewhat dangerous because they can be used to emit data to all subscribers. As a result, it makes sense to &#8220;hide&#8221; a subject by making it private and then expose an Observable out of it as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"269\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-19.png\" alt=\"\" class=\"wp-image-1325\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-19.png 703w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-19-300x115.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note that we don&#8217;t need to make <code>data$<\/code> public, because any field that isn&#8217;t <code>private<\/code> or <code>protected<\/code> is <code>public<\/code> by default. Using <code>readonly<\/code> enforces that other components\/services cannot assign a new value to <code>data$<\/code>. As a result, the above code is safe: The business logic that decides when and how to emit data is &#8220;hidden&#8221; in our service (where it belongs), and yet the rest of the application can be notified when the data changes without being able to break that process by accident.<\/p>\n\n\n\n<p>Of course, the same idea applies to Signals (see my <a href=\"https:\/\/blog.angulartraining.com\/angular-signals-best-practices-around-exposing-signals-5385452150a1?sk=d832d8e3d6a13b2bf640d76ef6ea8579\" target=\"_blank\" rel=\"noopener\" title=\"\">best practices for Signals<\/a>)<\/p>\n\n\n\n<p>Another option suggested in that post is to define a getter method that returns the read-only object:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"421\" height=\"136\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-20.png\" alt=\"\" class=\"wp-image-1333\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-20.png 421w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-20-300x97.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This is equivalent to using <code>readonly<\/code>. The value can&#8217;t be changed because we define a getter and no setter.<\/p>\n\n\n\n<p>Let&#8217;s complete our best practices list from yesterday:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make every member&nbsp;<code>private<\/code>&nbsp;by default<\/li>\n\n\n\n<li>If the member is needed in the template of a component, make it&nbsp;<code>protected<\/code><\/li>\n\n\n\n<li>If the member is meant to be fully public, go with&nbsp;<code>public<\/code><\/li>\n\n\n\n<li>If the member should be accessible but not modifiable, use <code>readonly<\/code> or a single getter with no setter<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we talked about Typescript visibility modifiers in the context of Angular classes. Today, I want to focus on best practices and common scenarios that involve such visibility modifiers. First, it&#8217;s very common to have Angular services that use RxJs Subjects. Subjects are somewhat dangerous because they can be used to emit data to all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,31,3],"tags":[],"class_list":["post-1324","post","type-post","status-publish","format-standard","hentry","category-angular","category-best-practices","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":8,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1324\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}