6 ms·
Write a code generator. That's the best solution at this moment.
by xiaq 11y ago
Write a code generator. That's the best solution at this moment.
- enneff 11y agoOr, y'know, just copy and paste the trivial code. It should take all of 30 seconds. Not saying that it's pretty, but it's quick and easy.
- junke 11y agoAnd that's exactly why I am not convinced that Go is as maintainable as often claimed. 30 seconds for you, but how many hours for the poor souls who will come after you and ask: should I change this copy too or is it a separate case? It reminds me of this: http://typicalprogrammer.com/abject-oriented/ http://typicalprogrammer.com/abject-oriented/
- enneff 11y agoThis thread is about sort interface implementations. Their general form will never change, and the only specifics unique to any given copy are those specific to their type. It is obvious to any Go programmer what can and cannot be changed in this situation.
- junke 11y agoThe thread was more about mattlondon's problem with sorting according to the same key in different structs. I used of modified version of this example[0] to illustrate what I think is his problem[1]: you can sort circles and planet by radius, and probably many other things in the original case, but you are required to copy-paste the definitions. I can understand that instantiating templates by hand is not so bad. But once it is written, maintaining the code is not so obvious for someone not acquainted with it. Did I introduce a bug or not in the code? Not easy to tell without context. [0] http://stackoverflow.com/a/28999886/124319 http://stackoverflow.com/a/28999886/124319 [1] https://play.golang.org/p/Dq0AJjkhhr https://play.golang.org/p/Dq0AJjkhhr
- jhalderman 11y agoWhy not write an interface for getting the radius and a radius sorter for sorting on that interface? I made a modified version of your code to illustrate. https://play.golang.org/p/Ya7tUhDnO2 https://play.golang.org/p/Ya7tUhDnO2 *edited a typo
- junke 11y agoThanks a lot for your example. I would certainly take this approach and I am happy that you took the time to write it. I feel a little sorry for you because I put a trap for the parent poster in the original code. I wrote "a[j] < a[i]". You "fixed" it while refactoring, which would be the good thing to do if I made a typo. If however I really wanted to sort circles differently from planets, then you made an error while refactoring what looked like copy-pasta but wasn't. My original point was that when you "have" to copy-paste, you cannot clearly see what is or isn't part of the copy and what is new. You found a way to rewrite the code without much copy- pasting, and I am happy to see that. Still, in other places where code duplication arise, there could be similar problems from a maintenance point of view.
- NateDad 11y agoIt's 3 lines of code the most newbie of newbies could understand... if this is the biggest problem I have in my day to day programming, I'll be happy. Is it mildly annoying to type out those three lines? Sure. You know what's a lot more annoying? Basically everything else in programming.
- junke 11y agoThe problem with code duplication is not about being lazy, or having to type. I love typing. When you dive into a codebase full of vaguely similar yet different blocks, it starts being more than mildly annoying to understand the intent of the code and make the correct change.
- NateDad 11y ago
- iopq 11y agocommit c270f19d456e862aeb559098fa32d36fbc5329a0 Date: Wed Apr 13 04:57:46 2016 -0700 fixing level selection again commit dae018e4f76db0e9da810b4560d6c8e1c7029048 Date: Mon Apr 11 07:06:47 2016 -0700 fixing level selection for hint case commit a754bcd0d09ab1e357ae28c8f06baf65d879878f Date: Sat Apr 9 02:33:14 2016 -0700 forgot to fix the level selection in the move case
- golergka 11y agoMaintenance.
- parenthephobia 11y agoWhat if I'd done that 20 times around my code-base and then later I discover there's an off-by-one error in the original code that's maybe been inherited into all 20 copies? If I could actually reuse code properly I'd only have to fix it once. Without effective code reuse, I have to hunt down the copies, each of which may have slight modifications to make them better fit their use case (and might be hard to grep for as a result), figure out whether or not the bug exists in that copy (and whether it can actually be triggered), and fix it there. The description of inheritance in http://typicalprogrammer.com/abject-oriented/ http://typicalprogrammer.com/abject-oriented/ seems relevant here. (Edit: Derp, junke got there way before me!)
- takeda 11y agoThen one day you find a bug in your code and now will have to find all copies of it and to make things worse someone else modified some of the copies so now not all of them are identical.
- junke 11y agoYou might be right, this is the best solution in Go at this moment, but that's a local optimum. You don't have to solve this kind of problems in many other languages. The generator given as an example in the official blog [0] is called "stringer". It is made of 639 lines [1] mixing ast parsing/traversing and printf statements. If this is what I am supposed to write then of course copy-pasting becomes a pragmatic alternative. [0] https://blog.golang.org/generate https://blog.golang.org/generate [1] https://raw.githubusercontent.com/golang/tools/master/cmd/stringer/stringer.go https://raw.githubusercontent.com/golang/tools/master/cmd/st...
- scotty79 11y agoOr go the JS route and write a transpiler. Language is lacking? Make a new one.
- louthy 11y agoOr just use a language that supports generics instead?
- sievebrain 11y agoThe first company to make a fork of Go that runs on the JVM and has compiler-specific extensions will make a ton of money, IMO.
- zzzcpan 11y agoThis will only target a very small group of developers, who are comfortable with JVM, but not with Java or Scala. I don't see any money there. JVM is a major drawback for any language. Many people don't even look at JVM languages.
- sievebrain 11y agoNo, it's the opposite. It would appeal to people who built large Go codebases and eventually realised that they were tied to a toolchain that was years behind the state of the art. A Go for the JVM would immediately give Go developers much better optimising compilers, high quality cross platform IDE-integrated debugging and profiling, much stronger garbage collectors, ability to access the large quantity of Java libraries that are out there, ability to mix in other non-Java languages too (javascript, ruby, R ...), code hotswap, better monitoring tools, a vastly larger standard library, better code synthesis tools and so on. The JVM is hardly a liability. It adds features and carries a cost, but Go is substantially similar to Java in many ways and getting closer with time. I do not see any credible plan from the Go developers to beat the JVM developers in terms of GC or compiler quality, for instance.
- 11y ago
- ball_of_lint 11y agoWouldn't that end up being essentially equivalent to adding generics to go?