On Wed, Nov 7, 2012 at 3:20 PM, Luke Mauldin <
lukem...@gmail.com> wrote:
> The two examples I gave are unrelated. The violation of the DRY principle
> occurs within each of the files. For example, in regexp.go, the FindAll and
> FindAllString are very similar and likewise the FindAllIndex and
> FindAllStringIndex have alot of duplication. The duplication in the sort
> package occurs mainly because of the different types (string vs int vs
> float64). However, both regexp and sort demonstrate areas where Go has
> duplication of almost identical code in the standard library.
OK, so then:
- In the regexp package case, those similar parts of the functions are
quite short (4 lines prologue and 4 lines epilogue). Factoring out 4
lines into a shared function saves 3 lines per instance at a cost of
setting up the parameters/results stack frames and risking a possible,
avoidable, stack split. I would write the function in the same way as
the are written now.
- In the sort package case, that must be IMO an misunderstanding. The
methods for different types do not apply for the DRY principle Every
method implementation can be attached to only one named type (so the
function "header" is mandatory and the bodies are one-liners in this
case). Then there are the functions (not methods), which are "type
specialized". As long as Go has no generics, there's no other
reasonable way to how to do that. Changing it (DRY-ishly) to a single
implementation w/ `interface{}` parameters leads to lost static type
checking and performance penalties.
In summary, the examples provided by you are IMHO not violating the
DRY principle, as that doesn't demand sacrificing type
safety/performance just to not repeat oneself, and in the methods case
it is simply out of the specs.
-j