Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative to pr/251. That method uses an enum with a function to tr… #258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

apresence
Copy link
Contributor

…anslate the names. My implementation names the enum values the actual API string values which eliminates the need for the extra ConversationTypesToQueryParam() function.

For example, instead of:
Tuple.Create("types", Conversation.ConversationTypesToQueryParam(types));

We can leave this unchanged:
parameters.Add(Tuple.Create("types", string.Join(",", types)));

This works because when string.Join translate types[] into strings, it calls ToString() which by default returns the enum element name.

This is also nice because we don't have to update the ConversationTypesToQueryParam() function whenever elements are added or removed from the enum.

If there is some kind of naming standard violation here, (e.g. The first enum element should be PublicChannel instead of public_channel), yet another alternative would be to use a class instead of an enum. For example:
public static class Type {
public static readonly string PublicChannel = "public_channel";
public static readonly string PrivateChannel = "private_channel";
...
}

There is a detailed discussion on using friendly strings with enums on Stack Overflow here:
https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings

…anslate the names. My implementation names the enum values the actual API string values which eliminates the need for the extra ConversationTypesToQueryParam() function.

For example, instead of:
	Tuple.Create("types", Conversation.ConversationTypesToQueryParam(types));

We can leave this unchanged:
	parameters.Add(Tuple.Create("types", string.Join(",", types)));

This works because when string.Join translate types[] into strings, it calls ToString() which by default returns the enum element name.

This is also nice because we don't have to update the ConversationTypesToQueryParam() function whenever elements are added or removed from the enum.

If there is some kind of naming standard violation here, (e.g. The first enum element should be PublicChannel instead of public_channel), yet another alternative would be to use a class instead of an enum. For example:
public static class Type {
	public static readonly string PublicChannel = "public_channel";
	public static readonly string PrivateChannel = "private_channel";
	...
}

There is a detailed discussion on using friendly strings with enums on Stack Overflow here:
https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant