Why use relics in C#?

Asked

Viewed 3,841 times

10

What are the advantages and disadvantages of using #regions in the C#?

Its use really facilitates code organization?

#region

/* Código */

#endregion

3 answers

13


It is irrefutable that the religions facilitate the organization, but one should take some care with what really you intend to organize.

I recommend using them when considering the following purposes:

  • Group generated code;
  • Separate large code snippets;
  • Other legibility purposes.

I believe the use of religions to separate members of a class according to levels of protection (private, public, etc.) is justifiable, but I define its use as preferential because, while sometimes the best thing to do is to group them according to their functionality, in others it becomes more feasible to organize them based on the own needs of its application.

Now, use religions to hide "ugly" code is reprehensible; when the intention is to hide, why not avoid future confusions and spend a small part of the time in refactoring certain functionality?

  • 2

    +1 for condemning relics to hide ugly parts of the code. = D There are people who do just that, hehe!

  • 1

    +1 for the same reason as @Miguelangelo

  • There is way to do this in Javascript ?

4

His idea is to decrease the amount of code displayed on the screen by grouping common functions. The advantage is to keep the code more succinct for reading.

The downside is that the religions hide the signatures of the methods. When it is important to see the signatures of each method, the shortcut Ctrl + M, Ctrl + The Is more useful.

  • I usually use #Region and Ctrl+M and Ctrl+O in sets. Create for example a sea of Fields and Properties, then I use #Region Fields to isolate my Fields and #Region Properties to isolate the properties... #Region Constructor and so on... then I use Ctrl+M to reduce all methods tbm... with this I consider that I have a gain of organization, because then I know clearly where to go... for example if I have to touch a Property, I go right there in Gion and Property, when I can’t remember her name to use the navigation shortcuts

  • Also use, but #Region use less.

3

This is just a way to organize the code, and is usually used by generated code.

In codes not generated I recommend a certain care when using Codes, because when used to separate codes according to some sort of classification, they tend to become outdated.

  • For example, if you use relins to group private members together, and some day some of them need to become public, I really bet that the member will remain within the religion.

The use I recommend, is to group members that have some real connection. For example:

  • a static field, initialized only once, which serves a single method, could be placed within a Region together with the method... obviously I am in favour of a nomenclature that also indicates that this field is part of that method, so that other programmers know this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.