Windows CoreAudio API in C#
hardkjarni.blogspot.com
hardkjarni.blogspot.com
Random aside: Why do so many C# coders use #region/#endregion? It really seems like a bad habit that discourages otherwise good coders from splitting their code into logical OOP silos and instead they dump too much code into a single file, and then use regions to regain some kind of order...
Regions are like goto in that they don't within their own right do anything "bad" they just encourage really bad habits, and people start to think about things in terms of regions. Plus finding things in a project which contains tons of really long code files with regions is immensely harder than finding them in a project with a lot of isolated classes and decent inheritance.
But they indeed depend on the team's total commitment on keeping them organized and up-to-date.
The region paradigm is very often misused and is one of those things that starts off nice and then degrades as time goes on (meaning new functions get added in the wrong region, refactorings end up all over the place, etc). I prefer them though over partial classes (shudder) and when properly used they can be a huge timesaver when dealing with those annoying UBER-Forms that tend to happen when dealing with legacy WinForms code :)
This is similar to my experience.
I use regions pretty often when I'm refactoring overgrown legacy code, especially older MVC apps (where the controllers get insane)
Basically what you said, but reversed. I use regions as a first step to regaining some kind of order, then continue to pull things apart into more properly factored classes, methods, etc. It helps me mentally map things out. Of course there're a bunch of ways you could do that, but Visual Studio has good support for folding regions, so I prefer it.
That folding is the second reason I'll use them sometimes. If I have a really awkward part of code that I'd like to fold for whatever reason, I'll occasionally use them if I think it'll make things more readable.
I agree with the idea, though. Every time I use a region I have to stop and think "am I just hiding bad design?"
Oh dear lord the MVC madness I've seen... thousand yard stare
I'm not sure how regions change how you create your class hierarchies and such. One of the first things I do when I inherit a project, is I add regions to it. I'm not changing the class hierarchies, but I am making it a lot easier to navigate within Visual Studio.
Plus you're now burying how long your class is and the individual methods within. So a class or method that would otherwise be inappropriately long now looks a reasonable length.
If you feel like the code is so long that it needs regions, then refactor the code, don't hide the mess under the bed.
If I end up needing to go through legacy code, and there are a lot of gnarly, long methods/blocks, I'll usually add comments and regions to it as I go along. I'll use comments if I just want to note something, and a region if I want to note something about a region of code. It's my rough map. Being able to tell Visual Studio to arbitrarily collapse an area is handy for sketching it. The long term goal is to refactor, but it's handy as a way to quickly outline things.
I find regions to be useful 1% of the time, simply pointless 10% of the time, and hiding crappy code the other 89% of the time. When I open up code and see a bunch of regions, I immediately assume that the file contains way too much unrelated functionality and that the code is poorly written. This assumption is very rarely wrong.
ca82a6d - kenjackson - 10 files changed, 34 insertions: "Added some regions"
085bb3b - dpark - 10 files changed, 34 deletions: "Removed regions"
a11bef0 - kenjackson - 10 files changed, 34 insertions: "Added some regions"
2432f8e - dpark - 10 files changed, 34 deletions: "Removed regions"
cbe6306 - kenjackson - 10 files changed, 34 insertions: "Added some regions"
e7e1bef - dpark - 10 files changed, 34 deletions: "Removed regions"
....What you fail to see is those silos can be a prison. One of the worst experiences I consistently have with heavily "OOP-person" code is you come to a new source tree and there are so many little tiny do-nothing classes and interfaces in individually tiny insignificant files that you can't come fresh to the project and tell "where the meat is" by browsing the filesystem. Putting those smaller interfaces and glue in a few mid-sized thematically oriented source files can be a breath of fresh air relative to this.
As an example, I was working on a project a while back that had StyleCop rules set up so stringently that nothing could live in the same file, no enums, structs, extension methods, nada. The amount of files you had to create when adding new functionality (even a minor one) was mind boggling...
The other annoyance that I often encounter at the same time is terribly long and redundant variable names, accompanied by an overdose of design patterns. ("Was it the FooFactoryInterfaceAdapterList or the FooFactoryInterfaceList that particular method was in?") An example from this article's code is GetMasterVolume() vs. GetMasterVolumeMute() --- GetMasterMute() is just as descriptive, especially when the class is already named AudioManager. There's no GetApplicationVolumeMute(), instead it nicely appears as the more succinct GetApplicationMute().
I know there are IDEs which will help you go to the right file, but it's still not as easy as just scrolling through a larger one and reading linearly. That said, excessive code duplication is to be avoided and best replaced with a function; the code referenced in this article shows signs of that, as I easily saw this fragment repeated many times, among others:
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
return;It made me rethink a few of the ways I've used regions previously. As a stopgap for refactoring legacy code, though, I'm still not 100% sold.
Like a lot of articles here, the comments and the back and forth are as interesting as the answer. I think right now I'd say that, used carefully, there's nothing implicitly wrong with regions, but overall it's probably better to avoid them. Honestly, I'll probably have to do a little more digging and see where it comes out.
They're reminding me of comments in general at this point, where there's nothing implicitly wrong with them, but every time you write one you should be asking yourself if you could clearly bake that intention/information into the code itself.
I also don't like it.
It gets even worse when the solutions are configured to collapse regions by default.
Also Apple is doing the same with regions for Objective-C and Swift.
Turns out a bit of a disappointment for the hacker in me. Windows has an API with the same name (https://msdn.microsoft.com/en-us/library/windows/desktop/dd3...)
https://msdn.microsoft.com/en-us/library/windows/hardware/mt...