Use abstraction layers to access configuration settings rather than accessing environment variables, feature flags, or other configuration sources directly. This improves maintainability by centralizing configuration logic, enabling validation, and simplifying testing.
Use abstraction layers to access configuration settings rather than accessing environment variables, feature flags, or other configuration sources directly. This improves maintainability by centralizing configuration logic, enabling validation, and simplifying testing.
Why it matters:
Implementation:
Instead of:
var organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION");
Prefer:
var organization = Helper.Organization; // Abstracted access to environment variable
For environment-specific code, use consistent preprocessor directives and centralize format strings:
var format =
#if !HAS_ENVIRONMENT
"{0} ({1}; {2}; {3}; Octokit {4})";
#else
"{0} ({1} {2}; {3}; {4}; Octokit {5})";
#endif
// Use format variable instead of duplicating the format string
When creating clients that require specific configuration constraints:
public EnterpriseSpecificClient(IApiConnection apiConnection)
{
// Validate configuration constraints early
if (!IsEnterpriseUrl(apiConnection.Connection.BaseUrl))
{
throw new InvalidOperationException(
"This client only works with GitHub Enterprise URLs.");
}
// Rest of constructor...
}
Enter the URL of a public GitHub repository