{"action":"created","issue":{"url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/142","labels_url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/142/labels{/name}","comments_url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/142/comments","events_url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/142/events","html_url":"https://github.com/ffMathy/Shapeshifter/issues/142","id":128096823,"number":142,"title":"Challenge: Reduce complexity of GetPathSegmentsInCommonCount","user":{"login":"ffMathy","id":2824010,"avatar_url":"https://avatars.githubusercontent.com/u/2824010?v=3","gravatar_id":"","url":"https://api.github.com/users/ffMathy","html_url":"https://github.com/ffMathy","followers_url":"https://api.github.com/users/ffMathy/followers","following_url":"https://api.github.com/users/ffMathy/following{/other_user}","gists_url":"https://api.github.com/users/ffMathy/gists{/gist_id}","starred_url":"https://api.github.com/users/ffMathy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ffMathy/subscriptions","organizations_url":"https://api.github.com/users/ffMathy/orgs","repos_url":"https://api.github.com/users/ffMathy/repos","events_url":"https://api.github.com/users/ffMathy/events{/privacy}","received_events_url":"https://api.github.com/users/ffMathy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ffMathy/Shapeshifter/labels/help%20wanted","name":"help wanted","color":"159818"},{"url":"https://api.github.com/repos/ffMathy/Shapeshifter/labels/technical%20debt","name":"technical debt","color":"fbca04"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/ffMathy/Shapeshifter/milestones/3","html_url":"https://github.com/ffMathy/Shapeshifter/milestones/Shapeshifter%206.2.0","labels_url":"https://api.github.com/repos/ffMathy/Shapeshifter/milestones/3/labels","id":1242485,"number":3,"title":"Shapeshifter 6.2.0","description":"The first release of Shapeshifter","creator":{"login":"ffMathy","id":2824010,"avatar_url":"https://avatars.githubusercontent.com/u/2824010?v=3","gravatar_id":"","url":"https://api.github.com/users/ffMathy","html_url":"https://github.com/ffMathy","followers_url":"https://api.github.com/users/ffMathy/followers","following_url":"https://api.github.com/users/ffMathy/following{/other_user}","gists_url":"https://api.github.com/users/ffMathy/gists{/gist_id}","starred_url":"https://api.github.com/users/ffMathy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ffMathy/subscriptions","organizations_url":"https://api.github.com/users/ffMathy/orgs","repos_url":"https://api.github.com/users/ffMathy/repos","events_url":"https://api.github.com/users/ffMathy/events{/privacy}","received_events_url":"https://api.github.com/users/ffMathy/received_events","type":"User","site_admin":false},"open_issues":26,"closed_issues":17,"state":"open","created_at":"2015-08-05T17:31:16Z","updated_at":"2016-01-27T16:58:14Z","due_on":null,"closed_at":null},"comments":5,"created_at":"2016-01-22T06:56:19Z","updated_at":"2016-01-27T16:59:50Z","closed_at":"2016-01-27T16:58:13Z","body":"Hey everyone interested (I'm looking at you @dvitonis, @Zargess, @Eraknelo and @KoalaBear84).\r\n\r\nThe code of Shapeshifter is starting to get pretty damn clean. Uncle Bob would have been proud. However, there is still one single function in the code that is bothering me. It's called `GetPathSegmentsInCommonCount` and is located in the `FileManager`.\r\n\r\nThe code (and its dependent functions) are shown below.\r\n\r\n```csharp\r\nstatic int GetPathSegmentsInCommonCount(IReadOnlyCollection<string> paths)\r\n{\r\n    var commonIndex = 0;\r\n    foreach (var originPath in paths)\r\n    {\r\n        var originSegments = GetPathSegments(originPath);\r\n        for (var index = 0; index < originSegments.Length; index++)\r\n        {\r\n            var originSegment = originSegments[index];\r\n            foreach (var referencePath in paths)\r\n            {\r\n                var referenceSegments = GetPathSegments(referencePath);\r\n                if (index >= referenceSegments.Length)\r\n                {\r\n                    return commonIndex;\r\n                }\r\n\r\n                var referenceSegment = referenceSegments[index];\r\n                if (originSegment != referenceSegment)\r\n                {\r\n                    return commonIndex;\r\n                }\r\n            }\r\n            commonIndex++;\r\n        }\r\n    }\r\n\r\n    return commonIndex;\r\n}\r\n\r\nstatic string[] GetPathSegments(string originPath)\r\n{\r\n    return originPath.Split('\\\\', '/');\r\n}\r\n```\r\n\r\nNormally I'm pretty good at making code clean and easy to read (by reducing the amount of lines per function, reducing nesting, and naming methods clearly after their intent), but *this monster is so hard to clean up*.\r\n\r\nAny suggestions are welcome. I have given up on that method.\r\n\r\nFor those curious about what it's for, take a look at one of the tests for it:\r\n\r\n```csharp\r\n[TestMethod]\r\npublic void CanGetCommonPathWithBiggestPathInMiddle()\r\n{\r\n    var container = CreateContainer();\r\n    var fileManager = container.Resolve<IFileManager>();\r\n\r\n    var commonPath = fileManager.FindCommonFolderFromPaths(\r\n        new[]\r\n        {\r\n            \"foo\\\\bar\",\r\n            \"foo\\\\bar\\\\baz\\\\lol\",\r\n            \"foo\\\\bar\\\\baz\"\r\n        });\r\n\r\n    Assert.AreEqual(\"foo\\\\bar\", commonPath);\r\n}\r\n```\r\n\r\nSo `FindCommonFolderFromPaths` finds the shortest path that all of the paths provided have in common. It uses `GetPathSegmentsInCommonCount` for this, which fetches the amount of path segments (in this case `\"foo\"` and `\"bar\"`, with a count of `2`) that the paths have in common.\r\n\r\nLet the challenge begin! I hope you'll add some proposals."},"comment":{"url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/comments/175745237","html_url":"https://github.com/ffMathy/Shapeshifter/issues/142#issuecomment-175745237","issue_url":"https://api.github.com/repos/ffMathy/Shapeshifter/issues/142","id":175745237,"user":{"login":"ffMathy","id":2824010,"avatar_url":"https://avatars.githubusercontent.com/u/2824010?v=3","gravatar_id":"","url":"https://api.github.com/users/ffMathy","html_url":"https://github.com/ffMathy","followers_url":"https://api.github.com/users/ffMathy/followers","following_url":"https://api.github.com/users/ffMathy/following{/other_user}","gists_url":"https://api.github.com/users/ffMathy/gists{/gist_id}","starred_url":"https://api.github.com/users/ffMathy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ffMathy/subscriptions","organizations_url":"https://api.github.com/users/ffMathy/orgs","repos_url":"https://api.github.com/users/ffMathy/repos","events_url":"https://api.github.com/users/ffMathy/events{/privacy}","received_events_url":"https://api.github.com/users/ffMathy/received_events","type":"User","site_admin":false},"created_at":"2016-01-27T16:59:50Z","updated_at":"2016-01-27T16:59:50Z","body":"Thanks for that! And cool that you added additional tests."}}