Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred naming convention for Func<TResult> method parameters?

Tags:

I admit that this question is subjective but I am interested in the view of the community. I have a cache class that takes a cache loader function of type Func<TResult>, which it uses to retrieve a value from the database and store it in cache.

public static class Cache {     public TResult Get<TResult>(string cacheKey, Func<TResult> cacheLoader)      {         // Implementation     } } 

My question is: How should I name the function parameter?

  • Should I name it like an object, e.g. cacheLoader?
  • Should I name it like a method, e.g. loadResult?
  • Should I explicitly refer to it as a function, e.g. cacheLoadFunction? (I don't like this.)

I'm less interested in what I should name this particular function parameter and more interested in how you name function parameters in general. What say ye, Stack Overflow community?

like image 973
John Bledsoe Avatar asked May 19 '11 16:05

John Bledsoe


People also ask

What is the naming convention used for parameters to methods?

The naming convention for these group items is: methodname--method-parameters, where methodname is the desired name of the method, followed by two hyphens.

How do you name a parameter?

Parameter names identify the local or global parameters that are defined by you or that are specified by the SRC software. The parameter name is a string of alphanumeric characters starting with a letter that does not contain spaces or special characters.

What is name convention in C#?

C# naming conventions are an important part of C# coding standards and best practice when you are developing a . NET applications. . NET naming conventions are standards how the naming of variables, methods, classes, and other code elements should be defined.

Is C# Camelcase or Pascalcase?

They are used to lower camel casing for everything from table names in SQL databases to property naming in C# code but I like Pascal casing better, lower camel casing for variables and Pascal casing for properties: string firstName; public string FirstName { ... }


2 Answers

There are precedents for using a noun in the Framework, e.g.

Enumerable.Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)  Enumerable.Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)  Enumerable.GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)  ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey key,              Func<TKey, TValue> valueFactory); 

The noun is often an appropriate verb with an agentive suffix.

In your example I would use something like loader or possibly valueFactory. I personally don't like cacheLoader because presumably it's the caller rather than the delegate that does the work of inserting in the cache.

like image 73
Joe Avatar answered Oct 24 '22 23:10

Joe


I like to name it like a method so that when you invoke it, like this:

loadResult(result); 

it looks like an ordinary method call but the casing indicates that it is a variable, so both pieces of information are conveyed.

You can append a suffix like Method or Delegate or Lambda but those often just make it verbose without adding clarity. It can depend on the situation and your coding standards, and of course your preferences.

like image 35
Rick Sladkey Avatar answered Oct 25 '22 00:10

Rick Sladkey