11 ms·
> It’s hard to describe the experience of doing a major refactoring, having a really helpful and friendly compiler guide you along step by step and when finally
by kazinator 10y ago
> It’s hard to describe the experience of doing a major refactoring, having a really helpful and friendly compiler guide you along step by step and when finally everything compiles it just works.
A truly large refactoring is one that you want to try long before getting everything to compile.
"Hmm, I have 75 operations involving data structure A. I want it to be B. Let's try it with five operations; I know that such and such features of the program only use those. Let's build the inconsistent program and try it out with with the B structure to the extent that it is possible."
The dynamic world lets us have inconsistent code that partially works, rather than forcing us to follow up on all the places reached by the ripple effect of a change. Maybe the change turns out to be a bad idea in the end; then it was a waste of effort.
Psychologically, having to deal with a big ripple effect is discouraging; it creates an "activation potential barrier" which opposes change.
- tikhonj 10y agoThis is why Haskell recently got a flag to turn type errors into warnings: now you came make a change, get a bunch of type errors (as warnings!) and fix them one at a time, loading and testing each part as you change it. You don't have to update everything to play around with your module. This isn't dynamic typing though: if you tried to use an expression that did not typecheck, you will get a runtime value immediately. It's the moral equivalent of automatically replacing the broken expression with an `undefined` exception.
- talles 10y agoThat's very, very nice. Is this exclusive to Haskell?
- jerf 10y agoWell, Haskell got it from Agda [1], but Agda is even farther in the direction of Haskell than Haskell relative to mainstream programming languages, and beyond the "currently practical" language horizon. [1]: https://wiki.haskell.org/GHC/Typed_holes https://wiki.haskell.org/GHC/Typed_holes
- jdmichal 10y agoEclipse does a similar thing with Java. It will replace methods that don't compile with a stub that throws an exception with the compilation error.
- kornish 10y agoFor anyone reading and interested in checking out the docs, the flag is -fdefer-type-errors: https://downloads.haskell.org/~ghc/7.8.2/docs/html/users_guide/defer-type-errors.html https://downloads.haskell.org/~ghc/7.8.2/docs/html/users_gui...
- kazinator 10y agoWhy don't they just add the dynamic typing when that happens. There could then be a class of Haskell programs that are statically ill-formed, yet actually free of errors. Maybe adding dynamic typing to a thoroughbred static language could turn out more fruitful than efforts to go the other way.
- dllthomas 10y ago> Why don't they just add the dynamic typing when that happens. Haskell (at least, GHC Haskell) has support for dynamic typing, in three(ish) parts. "Dynamic typing" in the sense of being able to ask the types at runtime is available in the Typeable typeclass. This is provided by the compiler automatically for known types; it must be visibly asked for for polymorphic types - partly for visibility and to preserve reasoning (https://www.reddit.com/r/haskell/comments/4mu30y/what_is_the_point_of_typeable/d3yakg6 https://www.reddit.com/r/haskell/comments/4mu30y/what_is_the...) and partly to clarify what dictionaries need to be passed where. "Dynamic typing" in the sense of foregoing static checks in favor of runtime checks can, of course, usually be done in a constrained way simply by choosing looser types. That said, depending on language, that can often mean substantial boilerplate. GHC does provide Dynamic, which will wrap any value up with its type annotations and let you apply (possibly wrapped) functions to these wrapped values in a dynamically checked way. Lastly, I'd kind of put "existential quantification" in play here. It's sometimes the case that you only need to show that some pieces of "the same type" are internally consistent, without knowing their precise shape. As for why it's not added, I'm not 100% sure what you mean. If you mean on every build, making that the only option, then I would say "because many of us who choose Haskell find static checks a huge help (particularly when refactoring) and wouldn't want to give that up". If you mean in place of the current behavior of -fdefer-type-errors, I expect it's sometimes quite useful to know earlier when you're exercising magically untyped code (as compared to deliberately demarcated untyped code). If you mean, "why don't they add -fdynamic-type-errors", an additional flag that subs in use of Dynamic in place of inconsistently typed code, that's certainly an interesting idea; given that terms are sometimes derived from types (typeclass polymorphism) it may not always be possible, and in any case may be a large engineering effort - I suspect Simon and company simply haven't gotten around to it.
- hacker_9 10y ago> The dynamic world lets us have inconsistent code that partially works This is precisely why I have nightmares about refactoring dynamic codebases.
- Touche 10y agoIf it's tested, you shouldn't.
- willtim 10y agoTesting shows the presence, not the absence of bugs. Dijkstra (1969).
- lucisferre 10y agoSame can be said of static type checking so what is your point? Are you suggesting that those working in static languages don't need to write tests? Static type checking generally represents an even lower form of error detection than functional and unit testing does. Sure, it can be convenient to have it, but lately I see a lot of static language novices around here talking about their recent and limited experience with type checking as if they are suddenly experts in the tradeoffs between static and dynamic languages.
- pka 10y agoBut, as you surely know, types prove something about a piece of code. This means they can very well prove the absence of (certain) bugs. So generally, the more invariants you manage to capture in your types, the more categories of bugs you can statically prove will never happen.
- jw- 10y agoThis is all true, but is it not a problem that most static languages don't let you (easily) specify the kind of invariants people include in unit-tests.
- vertex-four 10y ago> A truly large refactoring is one that you want to try long before getting everything to compile. So I thought this for a good long while - and then I started coding in Rust on a major project. Turns out that once I'd written my serialization/deserialization functions on the boundaries of my program, it's remarkably difficult to get the rest of my code to compile and not do what I was intending it to do. It's not magic - the compiler doesn't guess at what I want - but it's close enough that I'm comfortable making larger changes than with Python.
- Buttons840 10y agoI've had the same feeling. I have often though to myself "I do not know how to break this code. Even if I purposely tried to get this code to throw a run time error, I could not."
- pka 10y agoLeaving Haskell's deferred type errors aside for a moment; after you are done with your partially working code, what then? Relying on (the surely preexisting) 100% test coverage?
- kazinator 10y agoSay you have 65% test coverage over the refactored code. You have that before the refactoring and you have it after. What does it mean? It means 35% of the code is being refactored without coverage. Its status is "unknown" before refactoring and "unknown" after. Oh, but it compiles!
- pka 10y agoBut if it compiles its status is not unknown. It means it is well typed, proving the absence of a whole lot of bugs. Generally, refactoring in dynamic languages without tests or something like Clojure's schema is madness. Usually not a problem in strongly typed languages, even without much test coverage.
- kazinator 10y agoWell typed means nothing. Complete garbage can be well typed. Well typed proves the absence of a tiny, tiny subset of the possible bugs. A subset which has nothing to do with, for instance, the corretness of loops. Because type checking doesn't verify those. Well typed doesn't even tell you whether a program terminates or not and on what inputs. A program that is supposed to calculate pi could calculate e instead, yet be well typed (everything is a double-precision float, or a function from several of them to one, check!)
- pka 10y agoAs I said above, you can verify/prove the invariants you capture in the types. I don't know what you mean by "correctness of loops", but if you capture whatever it means in a particular type, yes you can. If you really wanted, you could derive a proof that pi is equivalent to some sort of corecursive series, which you can then evaluate in a dependently typed language, I guess. A proof is a type. On the other hand, how would you write a unit test that checks whether something calculates pi without comparing its result to pi itself?
- ionforce 10y agoIf lots of things change though... Couldn't that be a smell of bad boundaries? Like the change would not have been so drastic with the right abstractions?
- kazinator 10y agoIt could be the smell of bad boundaries. But sometimes a data type can have many functions that work with its internals. The 75 functions might not sounds so bad if there is a good justification for their existence, and there are 7500 other functions which don't have to change which rely those functions. At some level we need to actually implement things; we can't have abstraction all the way down! I'm also reminded of Alan Perlis Epigram #9: "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures."
- kazinator 10y agoIt could be the smell of bad boundaries. But sometimes a data type can have many functions that work with its internals. The 75 functions might not sounds so bad if there is a good justification for their existence, and there are 7500 other functions which don't have to change which rely those functions. At some level we need to actually implement things; we can't have abstraction all the way down! I'm also reminded of Alan Perlis Epigram #9: "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures."
- skybrian 10y agoYou can still do experiments with some adjustment to your work habits: create the new data structure with a different name, try it it out in some places via renames, and if necessary comment things out. Delete the old one and rename back again when you're done. The one that still annoys me, though, is having to comment out unused imports in Go. I agree they should always be removed before submit, but in the meantime it's annoying. (Tools that automatic remove unused imports are also annoying in their own way.)
- moosingin3space 10y ago`goimports` both adds and removes imports. It has been excellent to run on save in emacs for me.
- naasking 10y ago> The dynamic world lets us have inconsistent code that partially works, rather than forcing us to follow up on all the places reached by the ripple effect of a change. Maybe the change turns out to be a bad idea in the end; then it was a waste of effort. Except you can mostly do this reasoning just using the types. So you're rarely updating everything, you're just updating a few type definitions to see if your idea meshes well, and then you have to do the large refactoring.
- dllthomas 10y agoI find, when I'm refactoring, that well designed types often lets the compiler help me think ahead of my code, and spot subtle contradiction before I've "reached" them in terms.
- MaulingMonkey 10y ago> A truly large refactoring is one that you want to try long before getting everything to compile. For me, from experience, this is a sign that I or my coworker is creating an unreviewable tarball of a diff that will break something, can't be incrementally rolled back, and may not be easily fixed. A truly large refactoring is one that I want to break up into individual smaller refactorings, each with a more reviewable diff, aided by automatic refactoring tools that help offload my workload. While it's possible to perform enough static analysis of dynamically typed source code for such tools to exist and aid the programmer, they're less likely to be as rock solid as statically typed source code refactoring tools, for something as simple as a method rename.
- igouy 10y ago> A truly large refactoring is one that you want to try long before getting everything to compile. That's a luxury that may not be available :-) Here's a case study of industrial use of a refactoring tool provided with a dynamically typed language: … "A very large Smalltalk application was developed at Cargill to support the operation of grain elevators and the associated commodity trading activities. The Smalltalk client application has 385 windows and over 5,000 classes. About 2,000 classes in this application interacted with an early (circa 1993) data access framework. The framework dynamically performed a mapping of object attributes to data table columns. … Analysis showed that although dynamic look up consumed 40% of the client execution time, it was unnecessary. … A new data layer interface was developed that required the business class to provide the object attribute to column mapping in an explicitly coded method. Testing showed that this interface was orders of magnitude faster. The issue was how to change the 2,100 business class users of the data layer. … A large application under development cannot freeze code while a transformation of an interface is constructed and tested. We had to construct and test the transformations in a parallel branch of the code repository from the main development stream. When the transformation was fully tested, then it was applied to the main code stream in a single operation. … Less than 35 bugs were found in the 17,100 changes. All of the bugs were quickly resolved in a three-week period. … If the changes were done manually we estimate that it would have taken 8,500 hours, compared with 235 hours to develop the transformation rules. … The task was completed in 3% of the expected time by using Rewrite Rules. This is an improvement by a factor of 36." … from “Transformation of an application data layer” Will Loew-Blosser OOPSLA 2002