Live data from Hacker News

Windows CoreAudio API in C#

hardkjarni.blogspot.com

21–30 of 30 posts

Re: Windows CoreAudio API in C#

#21

The code is well structured and easy to read. Thanks for the example. 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…

> discourages ... from splitting their code into logical OOP silos 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 brows…

My experience with C# and Java agrees with you completely. As someone who didn't start with, and never quite understood, the OOP craze, the urge to make tiny pieces that have almost nothing in them is baffling. Arguments that it's "more readable" are true only locally, and understanding a single-line method does nothing for understanding the system as a whole; a significantly larger amount of cross-file jumping (and often keeping track of a very deep the call stack) is a hindrance. I suppose they do this because it gives the feeling and appearance of being highly productive, when they're really just making things more difficult for themselves and others by bloating the complexity.

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;

Re: Windows CoreAudio API in C#

#22
Holy shit, thank you. I wrote a tool to mute a game automatically when I backgrounded the window and was extremely annoyed when I found out that I had to work with COM in C++. I knew absolutely 0 COM and had to clobber together example code until I got something that barely worked and crashed randomly. I figured C# had good COM integration and could do it but just couldn't quite figure out how.

Re: Windows CoreAudio API in C#

#23
post #16

Earlier quoted context omitted.

I think regions are super useful. Even within the context of a class, I create regions for various types of code, e.g., constructors, private fields, etc... 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…

Ugh. Every time I inherit code that someone has peppered with regions, I delete then. It's a bunch of extra noise if you expand all regions, and if they're collapsed it's harder to see the overall structure of the code, and some functionality (specifically undo/redo, but others also) gets gimpy when collapsed regions are involved, making it hard to see what is changing. I find regions to be useful 1% of the time, sim…

In fairness though that assumption is almost always true regardless.

Re: Windows CoreAudio API in C#

#24

Earlier quoted context omitted.

I think regions are super useful. Even within the context of a class, I create regions for various types of code, e.g., constructors, private fields, etc... 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…

I don't see how hiding large chunks of code behind a drop-down is making it "easier to navigate in Visual Studio." If anything it is now much harder. 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 h…

Point me to some of your code and I bet it would benefit from well placed regions.

Re: Windows CoreAudio API in C#

#25

Holy shit, thank you. I wrote a tool to mute a game automatically when I backgrounded the window and was extremely annoyed when I found out that I had to work with COM in C++. I knew absolutely 0 COM and had to clobber together example code until I got something that barely worked and crashed randomly. I figured C# had good COM integration and could do it but just couldn't quite figure out how.

You're more than welcome, glad that you might find this useful. Hope this HELPS "stabilise" things ;)

Re: Windows CoreAudio API in C#

#26
post #16

Earlier quoted context omitted.

Ugh. Every time I inherit code that someone has peppered with regions, I delete then. It's a bunch of extra noise if you expand all regions, and if they're collapsed it's harder to see the overall structure of the code, and some functionality (specifically undo/redo, but others also) gets gimpy when collapsed regions are involved, making it hard to see what is changing. I find regions to be useful 1% of the time, sim…

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: "Re…

gold, pure gold :D

Re: Windows CoreAudio API in C#

#27

The code is well structured and easy to read. Thanks for the example. 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…

> Why do so many C# coders use #region/#endregion?

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.

Re: Windows CoreAudio API in C#

#28

The code is well structured and easy to read. Thanks for the example. 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…

> discourages ... from splitting their code into logical OOP silos 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 brows…

You don't need to browse the filesystem when you have an IDE.

Re: Windows CoreAudio API in C#

#30

The code is well structured and easy to read. Thanks for the example. 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…

I only use regions to categorize old/legacy code that either is going to be pulled into its own class or outright replaced with a much better implementation. I don't like to use regions since VS already makes it relatively easy to navigate to a method.
Post reply on HN