Letters from Readers

Letters: The Good, Bad and Ugly of C#

Readers respond to C# Corner columnist Patrick Steele's look into the best and worst of the C# programming language.

This is a great compilation. One thing I'll add on the try/catch block -- that I think we as Microsoft developers miss -- is the need to throw the original exception.

Or we need to stuff it in an inner exception and then throw it as a custom exception. Wrapping exceptions preserves the original downstream stack trace and gives the developer the opportunity to throw a meaningful exception -- -perhaps with more information in the form of properties -- beyond something as generic as an argument exception. This by no means is meant to advocate overuse of custom exceptions, but they also have their place and, in my experience, don't get used enough.

Tim Ellison
Richmond, Va.

Every programming language that gives you a lot of flexibility and power has bad and ugly things. That's not the fault of the language -- it's an inevitability. We learn to avoid doing things in bad and ugly ways.

Jim Perry
posted online

I'd like to thank Patrick for these timely reminders. I'm going to audit my current project for these issues today. If I may, I'd like to nominate "Bad Grammar and Spelling" to his list of "The Ugly." It's a real pain to try to understand C# code -- or any code -- that has comments and messages that are awkward, too terse or error-riddled. For instance, the first example in "The Bad" should throw a nested BadGrammarException (a recent invention of mine), as a better description for the exception would be "The amount cannot be negative" or "The amount must not be negative."

As developers, we often forget that ordinary people and late-night programmers are trying to use our software. We must make our programs easy to read, especially when errors occur.

As a final example, consider the difference a single comma makes between "Let's eat, Grandpa!" and "Let's eat Grandpa!"

David Patow
posted online

Nice list. But I think properties should have been listed under bad. They're misleading because to the user of a class they look like variables, but they're methods. Getting a variable will not change a class' state, assigning a value to it will only change that specific variable -- and in exactly the way you tell it to. With properties these guarantees are gone -- even getting one could change the state of the class. Awful. Properties should be ditched. Use methods instead.

J.C.
posted online

About the Author

This story was written or compiled based on feedback from the readers of Visual Studio Magazine.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube