What's your most reused class?

2019-03-23 18:40发布

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in java:

 class Separator {

        private String separator;
        private boolean called;

        public Separator(String aSeparator) {
            separator = aSeparator;
            called = false;
        }

        @Override
        public String toString() {
            if (!called) {
                called = true;
                return "";
            } else {
                return separator;
            }
        }
    }

and:

public class JoinHelper {

    public static <T> String join(T... elements) {
        return joinArray(" ", elements);
    }

    public static <T> String join(String separator, T... elements) {
        return joinArray(separator, elements);
    }

    private static <T> String joinArray(String sep, T[] elements) {
        StringBuilder stringBuilder = new StringBuilder();
        Separator separator = new Separator(sep);

        for (T element : elements) {
           stringBuilder.append(separator).append(element);
        }

        return stringBuilder.toString();
    }
}

What is your most reused class?

9条回答
不美不萌又怎样
2楼-- · 2019-03-23 19:11

Globals

Just a simple class with static DBConnString, and a few other app wide settings.

Have reused the simple file in about 2 dozen projects since working with .Net

查看更多
ら.Afraid
3楼-- · 2019-03-23 19:12

A utility class that has logging and email functionality. An extensions class that contains extension methods. A reporting class that basically harness the reporting services web service and makes it easy to stream reports as excel, pdf, etc.

Examples...
1.) Utility Class (static)

   public static void LogError(Exception ex)
    {
        EventLog log = new EventLog();
        if (ex != null)
        {
            log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
            StringBuilder sErrorMessage = new StringBuilder();
            if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
            {
                sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
            }
            sErrorMessage.Append(ex.ToString());
            log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
        }
    }

2.) Extensions Class

   public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
    {
        if (condition)
            return source.Where(predicate);
        else
            return source;
    }
查看更多
做个烂人
4楼-- · 2019-03-23 19:12

System.Object - almost all my types extend it.

查看更多
Root(大扎)
5楼-- · 2019-03-23 19:12

Configuration Reader/Setter: which reads the configuration from ini/xml file and sets the environment of the application

查看更多
地球回转人心会变
6楼-- · 2019-03-23 19:12

Most reused? Hmmm...

boost::shared_ptr<> with boost::weak_ptr<>

probably most reused (also probably most bang-for-buck ratio)

查看更多
迷人小祖宗
7楼-- · 2019-03-23 19:14

A ConcurrentDictionary I wrote, which I now seem to use everywhere (I write lots of parallel programs)

查看更多
登录 后发表回答