Extend class

S

shapper

Hello,

I have been creating a few classes that extend some C# classes
functionality. For example:

// StringHelper
public class StringHelper {
public static String ParseHtml(String html) {
// Method Code
}
}

I defining my methods as static so I can use:
String parsed = StringHelper.ParseHtml(myHtml);

Is this a good way to extend C# classes?

Or would you advice me other methods?

Basically, I use this for all classes: DateTimeHelper, StringHelper,
FileHelper, UriHelper, etc

Thank You,
Miguel
 
S

Stefan Hoffmann

hi,
I have been creating a few classes that extend some C# classes
functionality. For example:

// StringHelper
public class StringHelper {
public static String ParseHtml(String html) {
// Method Code
}
}

I defining my methods as static so I can use:
String parsed = StringHelper.ParseHtml(myHtml);

Is this a good way to extend C# classes?
Or would you advice me other methods?
You may use extension methods:

http://msdn.microsoft.com/en-us/library/bb383977.aspx

E.g.

public static class MyExtensions
{
public static string ParseHtml(this String str)
{
// Method Code
}
}

and

String parsed = myHtml.ParseHtml();



mfG
--> stefan <--
 
P

Pavel Minaev

I have been creating a few classes that extend some C# classes
functionality. For example:

  // StringHelper
  public class StringHelper {
    public static String ParseHtml(String html) {
    // Method Code
    }
  }

I defining my methods as static so I can use:
String parsed = StringHelper.ParseHtml(myHtml);

Is this a good way to extend C# classes?

In general, yes (but also pay attention to Stefan's suggestion above).
For your particular case, no, because a method named ParseHtml looks
very out of place in class StringHelper. The class name implies that
it contains helper methods that work on "strings in general".
ParseHtml - whatever that does - is much narrower in scope. It would
look alright in a class named "HtmlHelper", for example

By the way, there isn't an established convention on the names of such
classes, but from what I've seen they're more typically called
"...Helpers" (with "s" at the end), since it's methods that are
helpers, and there's normally more than one.

On a side note, the method name "ParseHtml" is suspect, too. "Parse"
is generally when you take some text (as string, or TextReader, or
IEnumerable<char>, or whatever) as input, and provide some structured
("parsed") output. Your method takes a string, and returns a string as
well. So what's it point? If it, say, performs some cleanup on HTML,
then call it "CleanupHtml", since that's what it does - even if it has
to parse the input, it's an implementation detail.
 
S

shapper

[...]
By the way, there isn't an established convention on the names of such
classes, but from what I've seen they're more typically called
"...Helpers" (with "s" at the end), since it's methods that are
helpers, and there's normally more than one. [...]

My two cents, 'cause I can't resist:

Whatever the common convention may be, IMHO a "...Helper" class makes more  
sense than a "...Helpers" class.  Specifically, the name of the class is a  
description of the class itself.  It tells you what the _class_ is.  The  
name "...Helpers", on the other hand, tends to describe the contents of  
the class.  As if the class isn't an entity unto itself, but rather just a  
container for stuff.  And even in that view, the name "...Helpers" is only  
accurate insofar as we tend to put the label "Cookies" on a "CookieJar",  
even though it's really a "CookieJar".  :)

Pete

Hi,

I think it sounds better as Helper instead of Helpers.
But my native language is not English so I might be wrong ...

I was looking as "this class helps String" other then "this classes
contains methods that help string" ...
Something like this.

So I would also have StringExtension. A class for string extensions.

But in this case I have a project here from Microsoft where they call
it SomeClassExtensions.

For me would make more sense to have:
StringHelper, DateTimeHelper, ... under the namespace
MyProject.Helpers
String.Extension, DateTimeExtension, ... under the namespace
MyProject.Extensions

Not?

Thanks,
Miguel
 
S

shapper

In general, yes (but also pay attention to Stefan's suggestion above).
For your particular case, no, because a method named ParseHtml looks
very out of place in class StringHelper. The class name implies that
it contains helper methods that work on "strings in general".
ParseHtml - whatever that does - is much narrower in scope. It would
look alright in a class named "HtmlHelper", for example

That makes sense ...
If it, say, performs some cleanup on HTML,
then call it "CleanupHtml", since that's what it does - even if it has
to parse the input, it's an implementation detail.

Yes, what this does is to remove all HTML tags from a String.
Yes, CleanHtml or StripHtml or CleanupHtml sounds better. You're
right.
 
S

shapper

In general, yes (but also pay attention to Stefan's suggestion above).
For your particular case, no, because a method named ParseHtml looks
very out of place in class StringHelper. The class name implies that
it contains helper methods that work on "strings in general".
ParseHtml - whatever that does - is much narrower in scope. It would
look alright in a class named "HtmlHelper", for example

By the way, there isn't an established convention on the names of such
classes, but from what I've seen they're more typically called
"...Helpers" (with "s" at the end), since it's methods that are
helpers, and there's normally more than one.

On a side note, the method name "ParseHtml" is suspect, too. "Parse"
is generally when you take some text (as string, or TextReader, or
IEnumerable<char>, or whatever) as input, and provide some structured
("parsed") output. Your method takes a string, and returns a string as
well. So what's it point? If it, say, performs some cleanup on HTML,
then call it "CleanupHtml", since that's what it does - even if it has
to parse the input, it's an implementation detail.

Just to try to clarify this. I have a few methods as follows:

1. Convert string to BaseASCII characters
public static String BaseASCII(String text)

2. Add an "and" to a string like "net, car, moon" to become "net, car
and moon"
public static String ToFriendlyCsv(String csv, String and)

3. Creates a random string using the allowed characters. And with
length defined.
public static String Random(String characters, Int32 length)

4. Trims a string to a maximum length and adds "..." or not at the end
public static String Trim(Boolean ellipsis, Int32 length, String
text)

Which ones would you implement as a string extension or helper?
Or maybe some other than string ...

I am trying to clear my ideas on when to use a string extension, a
string helper or something else.

For example for HtmlHelper I already added a method named Obfuscate
which removes all "@" from all emails in a string.
 
P

Pavel Minaev

[...]
By the way, there isn't an established convention on the names of such
classes, but from what I've seen they're more typically called
"...Helpers" (with "s" at the end), since it's methods that are
helpers, and there's normally more than one. [...]

My two cents, 'cause I can't resist:

Whatever the common convention may be, IMHO a "...Helper" class makes more  
sense than a "...Helpers" class.  Specifically, the name of the class is a  
description of the class itself.  It tells you what the _class_ is.

I would disagree here. The name of the class tells you what the
_object_ of that class is :)
 The name "...Helpers", on the other hand, tends to describe the contents of  
the class.  As if the class isn't an entity unto itself, but rather just a  
container for stuff

Here we have a static class, so there are no instances. So, indeed, a
class is in this case just a container of methods, nothing else.(in
fact, from OOD perspective, it's not even a class, since a class is by
definition something that may have instances).

An example of such naming in the FCL itself is the
"DbProviderFactories" class.
 
S

shapper

That said, if you made it a bit more general-purpose, a la String.Split(),  
I think a stronger argument could be made for the 2nd example to be an  
extension method as well.

Pete

Hi Pete,

Sorry for the delay. Thank you for your answers.
I just finished creating my helpers and extensions.

Just curious about something:
What do you mean more general-purpose?
I have the following:

public static String ToFriendlyCsv(String csv, String and) {
Int32 last = csv.LastIndexOf(",");
return last < 0 ? csv : csv.Remove(last, 1).Insert(last,
String.Concat(" ", and, " "));
} // FriendlyCsv

This is something I use very often on my web projects.
How can I make it more general-purpose?

I didn't understand very well your suggestion.

Thank You,
Miguel
 
S

shapper

Here we have a static class, so there are no instances. So, indeed, a
class is in this case just a container of methods, nothing else.(in
fact, from OOD perspective, it's not even a class, since a class is by
definition something that may have instances).

An example of such naming in the FCL itself is the
"DbProviderFactories" class.

I agree more with Peter but I think you are closer to be right ...
.... or at least the 3 Microsoft's sample MVC web projects that I have
been following always use Helpers and Extensions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top