91

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?

wonea
  • 4,783
  • 17
  • 86
  • 139
Gaff
  • 5,507
  • 3
  • 38
  • 49
  • 8
    Regions are [bad, bad, bad](http://programmers.stackexchange.com/a/53114). You don't need them. Seriously. – Steven Oct 09 '12 at 14:05
  • 17
    @Steven nice link - I've just put down votes on it! – Fenton Oct 09 '12 at 14:07
  • 102
    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 !!! – Akash Kava Oct 10 '12 at 06:11
  • YOU SHOULD NEVER HIDE YOUR CODE... just create small pieces of code and use refactoring. You'll never use regions again.. – MuriloKunze Oct 10 '12 at 15:58
  • 23
    @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." – Gaff Oct 10 '12 at 18:10
  • 6
    @MuriloKunze You assume the only use of regions is to hide code. Instead we should focus on answering the question. – Ian Boyd Feb 20 '14 at 01:31
  • 2
    To answer @MuriloKunze - 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. – Andrew Gray Oct 04 '17 at 19:23
  • @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. – Brent Rittenhouse Jan 08 '18 at 19:43
  • @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. – Brent Rittenhouse Jan 08 '18 at 19:45
  • 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. – mbalsam Dec 15 '19 at 17:55
  • 6
    For anyone coming here late, this is implemented in typescript. `//#region RegionName //#endregion` – mcheah Oct 28 '20 at 19:04

4 Answers4

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

oneNiceFriend
  • 6,691
  • 7
  • 29
  • 39
unjuken
  • 1,106
  • 9
  • 9
12

Visual Studio 2017 also has support for regions here.

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

Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
Highdown
  • 646
  • 1
  • 11
  • 23
11

You can follow the TypeScript GitHub issue here

TypeScript Regions #11073

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

wonea
  • 4,783
  • 17
  • 86
  • 139
  • yep, last comment at that page nicely points out that implementation is completed and can be used same as c# counterpart. I like it :=) – mkb Dec 03 '20 at 07:30
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.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    I really just wanted a way to separate my classes. Thanks for the info. – Gaff Oct 09 '12 at 14:22
  • 35
    `#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 :) – iCollect.it Ltd May 03 '13 at 07:48
  • 3
    I use regions in C# to create a better visual distinction between private and public members. – Michael Fry Oct 20 '16 at 14:53
  • How many public and private members do you have? Try avoiding regions and see if it helps guide you to better object-oriented design. – Fenton Oct 21 '16 at 10:29
  • 4
    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. – EHorodyski Jan 26 '17 at 16:46
  • 1
    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. – m12lrpv Sep 16 '21 at 21:22
  • @m12lrpv 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_. – Fenton Oct 07 '21 at 05:53