-
Notifications
You must be signed in to change notification settings - Fork 26
Refactor: Reduce Cognitive Complexity in LoadEmbeddedLocalizations() #588
Copy link
Copy link
Closed
Labels
C#C# related codeC# related codebugSomething isn't workingSomething isn't workingcode cleanupcleaning-up codecleaning-up codeenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededrefactoringRefactoring codeRefactoring codereliability
Description
What version of FlowSynx?
1.2.2
Describe the bug
The method LoadEmbeddedLocalizations() currently has a Cognitive Complexity of 20, exceeding the allowed threshold of 15. This makes it harder to maintain, test, and understand.
File: src/FlowSynx.Infrastructure/Localizations/JsonLocalization.cs
Method: LoadEmbeddedLocalizations()
Proposed Solution
private Dictionary<string, Dictionary<string, string>> LoadEmbeddedLocalizations()
{
var result = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
LoadAssemblyLocalizations(assembly, result);
}
return result;
}
private void LoadAssemblyLocalizations(Assembly assembly, Dictionary<string, Dictionary<string, string>> result)
{
var resourceNames = assembly
.GetManifestResourceNames()
.Where(r => r.EndsWith(".json", StringComparison.OrdinalIgnoreCase));
foreach (var resourceName in resourceNames)
{
var culture = GetCultureFromResourceName(resourceName);
if (culture == null) continue;
var parsed = ReadLocalizationResource(assembly, resourceName);
if (parsed == null) continue;
MergeLocalizationEntries(result, culture, parsed);
}
}
private static string? GetCultureFromResourceName(string resourceName)
{
var parts = Path.GetFileNameWithoutExtension(resourceName)?.Split('.');
return parts?.LastOrDefault()?.ToLowerInvariant();
}
private static Dictionary<string, string>? ReadLocalizationResource(Assembly assembly, string resourceName)
{
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null) return null;
using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
return JsonSerializer.Deserialize<Dictionary<string, string>>(json);
}
private static void MergeLocalizationEntries(
Dictionary<string, Dictionary<string, string>> result,
string culture,
Dictionary<string, string> parsed)
{
if (!result.TryGetValue(culture, out var dict))
{
dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
result[culture] = dict;
}
foreach (var kv in parsed)
{
if (!dict.ContainsKey(kv.Key))
{
dict[kv.Key] = kv.Value;
}
// else: optionally log duplicate keys here
}
}Acceptance Criteria
- Cognitive Complexity ≤ 15 (as per static analysis)
- Maintains the same functional behavior
- Code remains readable and testable
- Unit tests (if applicable) pass successfully
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C#C# related codeC# related codebugSomething isn't workingSomething isn't workingcode cleanupcleaning-up codecleaning-up codeenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededrefactoringRefactoring codeRefactoring codereliability