2

I have a function that receive an Encoding object as parameter. However, I think it could be easier for other programmers to pass an enum value instead of a Encoding object (especially for programmers that are not use to deal with different encoding). I couldn't find any built-in Encoding enum in C#... did I miss something or should I create an enum of my own?

3
  • 1
    No, there isn't. How about providing a sensible default instead, and an override accepting an Encoding instance for developers who know better? Commented Nov 17, 2016 at 15:22
  • This is exactly what I have right now. The idea of using an enum was to help programmer that may not know where to get the different Encoding objects (they may still refer to the function definition to see where the default Encoding was taken from). Commented Nov 17, 2016 at 15:27
  • 3
    @The_Black_Smurf it's a terrible idea to alter code to appease "[programmers] that may not know where to get the different Encoding objects". Instead, educate your team mates on how it works and what .NET is offering. As a general lesson, don't fight your tools. Commented Nov 17, 2016 at 15:33

3 Answers 3

5

The idea of using an enum was to help programmer that may not know where to get the different Encoding objects

Go for the principle of least astonishment. Developers who want to alter the encoding, will know (or at least, will need to know) what they are doing, and so they ought to know how to obtain an Encoding instance representing the encoding they want to use.

If they don't, they can search the web for ".net get encoding for codepage XYZ", directing them to the MSDN page for Encoding.GetEncoding(). Whereas if you introduce a new enum, their Google search for ".net get TheBlackSmurf.EncodingEnum for codepage XYZ" will yield zero results.

Don't cater for people who don't know what they are doing.

If instead you're going to use an enumeration, chances are that the one developer who does want to change the encoding and knows which one to use and how to obtain it, can't utilize it because you didn't think of that one and didn't include it in your enum.

On the other hand, an enum might be a viable solution, if for whatever reason your software can only support a handful of encodings.

Sign up to request clarification or add additional context in comments.

1 Comment

Very much this, which is the message I was hoping to convey in my comment.
2

Well no, there is no enum. But there are pre-defined Encodings as static objects in the Encoding class (see https://msdn.microsoft.com/en-us/library/system.text.encoding.aspx), like ASCII and UTF8 that basically provide the same functionality as an enum would.

If you absolutely need to use an enum, you can write your own that maps to the different pre-defined encodings. It's only a couple of lines of code.

Comments

1

Agreeing with @MarioDS's comment, this is as far as I would go:

XML Docs to feed IntelliSense

///<param name="encoding"> /// Whatever it means to this function…. /// Examples: Encoding.Unicode or Encoding.GetEncoding(1252) ///</param> 

(You could get fancier with see, seealso, cref or whatever but that would clutter up the source to little advantage.)

In use:

Code showing function call with IntelliSense tool tip

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.