Regions in TypeScript [closed]
Asked Answered
S

4

94

In my JavaScript (.js) files, I use a Visual Studio 2012 plugin for regions (here) like this:

//#region "My Region"

//Code here

//#endregion "My Region"

I would also like to have regions in TypeScript (.ts) files as well. Is this a possibility right now?

Semiramis answered 9/10, 2012 at 14:2 Comment(11)
Regions are bad, bad, bad. You don't need them. Seriously.Nimwegen
@Nimwegen nice link - I've just put down votes on it!Parashah
Regions are good, misuse of them is bad, same as knife, good to cut vegetables and bad to use it for murdering, that doesn't make knife bad. One blog article doesn't make it bad !!!Ardell
YOU SHOULD NEVER HIDE YOUR CODE... just create small pieces of code and use refactoring. You'll never use regions again..Dickie
@murilokunze Object-oriented programming is hard to do in Javascript in the same manner as it is done in C# or Java. Regions have been helpful for me in keeping code organized for small web projects. However, I am glad that Typescript has better file structuring, which I will definitely be doing rather than regions. In regards to Javascript, it must be hard in Visual Studio to keep separate .js, while retaining intellisense, and then compiling it into one js file for your website. Regions aren't that bad in certain contexts. You guys sound like bigots when you say "DON'T USE REGIONS --PERIOD."Semiramis
@Dickie You assume the only use of regions is to hide code. Instead we should focus on answering the question.Blankenship
To answer @Dickie - your sentiment is both right and wrong. The right part is, if you're regioning off functional code, that's a smell that you may need to refactor. The wrong part, is 'if you're using regions, you need to refactor.' This is false, because it's a common practice to region off class-level field/constant declarations, as well as implementations of interfaces, as well as class-specific methods. When used correctly, regions are another tool to organize code in modern editors.Goatee
@Steven: You just provided a link where the original question said " Whenever I do this though I do it to hide large chunks of code that could probably be refactored into other classes or methods. For example I have seen methods that contain 500 lines of code with 3 or 4 regions just to make it manageable." THAT is the problem itself, not regions. Over the years I've only CORRECT usage of regions more. If I was told I couldn't use them during an interview I would walk out. Or if I was interviewing and the programmer said they were 'bad' and gave that as a region THEY would be out the door.Smelt
@MuriloKunze: I agree, except regions shouldn't hide your code. They should be used to ensure CORRECT usage of it. You don't use them in any area that could be refactored. You refactor. If you have co-workers that use it to cover up code smells then you catch that in code review, or get rid of them.Smelt
In combination with JetBrains Rider and the Structure panel, regions are very useful. They allow you to group methods into types. So instead of seeing 20 methods signatures in the Structure list, I see 5 groups of 4 methods. I will inject my self into the flame war here. The needs of larger projects and smaller projects are very different. I really wish these "bigots" would simply make their opioion known and just answer the question, if they can.Slaby
For anyone coming here late, this is implemented in typescript. //#region RegionName //#endregionFlinch
B
33

You can download Web Essentials 2012, it lets you use regions in TypeScript.

If you use, Visual Studio 2015, here is the freshest link. Web Essentials 2015.3

Bidding answered 21/1, 2013 at 16:41 Comment(2)
Doesn't appear to be in 2017?Leucomaine
As per supported versions on the WE site it doesn't look like Regions are support in VS2017 yet :( vswebessentials.com/features/typescriptMufti
A
12

You can follow the TypeScript GitHub issue here

TypeScript Regions #11073

Still no ETA on when this will be implemented, currently awaiting more feedback.

Armilla answered 19/7, 2017 at 13:45 Comment(1)
yep, last comment at that page nicely points out that implementation is completed and can be used same as c# counterpart. I like it :=)Vocal
A
12

Visual Studio 2017 also has support for regions here.

Support via Web Essentials for older versions may be found here.

Anglice answered 28/10, 2017 at 15:15 Comment(0)
P
5

To request a new feature for TypeScript, you should start a discussion on Codeplex.

There are lots of opinions about regions being a bad thing - perhaps they are telling you that you have too much in a single file. TypeScript has a great way to load modules from many different files and it may be better to use that to separate your concerns.

Parashah answered 9/10, 2012 at 14:6 Comment(7)
I really just wanted a way to separate my classes. Thanks for the info.Semiramis
#regions mirror their counterpart in C-Sharp and and would typically be used to separate classes into categories like public methods private helper methods members etc. Like any documentation feature they can be overused if misunderstood, but they are certainly not a bad thing :)Arri
I use regions in C# to create a better visual distinction between private and public members.Sparse
How many public and private members do you have? Try avoiding regions and see if it helps guide you to better object-oriented design.Parashah
Some people (i.e. me) like to use them for readability, sorting out classes by regions (private variables, public methods, events, etc.). In TypeScript, it would be very helpful, thinking from the perspective that I'll be handing my code over one day.Sheared
It's sad that people like Fenton think that the developer defines the data structure when it's the data that does. Splitting a large flat but distinct data set into separate classes to avoid using regions just ads class bloat and a maintenance overhead.Goshorn
@Goshorn no need to be sad - I hold an opinion but you don't need to share it. My experience over several decades and lots of languages has been that regions aren't used to "better organise code", they are used to "attempt to structure total chaos". If you've seen them used in better ways than me, your opinion of them will be more positive. It's okay that we don't agree and we don't have to convince each other that there is one true way.Parashah

© 2022 - 2025 — McMap. All rights reserved.