7 ms·
In OCaml (or any similar language) 95% of what you "unit test" for in Javascript just goes away. If people cared about software quality, they would use strongl
by gaius 10y ago
In OCaml (or any similar language) 95% of what you "unit test" for in Javascript just goes away.
If people cared about software quality, they would use strongly typed languages before even thinking about "unit tests". What people actually want is a) to hack it out quickly and b) to have a lot of busywork to do as a form of job security. Hence 50,000 lines of "unit tests" per 10,000 lines of application...
- mdpopescu 10y agoI have never met anyone who doesn't care about code quality yet writes unit tests...
- TickleSteve 10y agothen you have never worked in anything but a small focussed team. The majority of development is performed by people in corporate environments, 9->5. They simply know that they "have to" write unit tests. No reasoning is performed, simply churn out the code to satisfy the boss and walk out at 5pm.
- mdpopescu 10y agoSorry, you are correct; what I wrote was simply stupid. I have, in fact, encountered one method with a cyclomatic complexity of 20+, which had a single test (it verified that the result was not None). I just tried to put that code behind me and yes, those "tests" were there because they were mandatory. Blech.
- sametmax 10y agoHere is a list of basic errors in Python. Only builtin ones, I'm not talking about ones from other libs. And I'm talking of error categories, as we all know one exception can be used in a lot of different contexts: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StopAsyncIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError +-- LookupError | +-- IndexError | +-- KeyError +-- MemoryError +-- NameError | +-- UnboundLocalError +-- OSError | +-- BlockingIOError | +-- ChildProcessError | +-- ConnectionError | | +-- BrokenPipeError | | +-- ConnectionAbortedError | | +-- ConnectionRefusedError | | +-- ConnectionResetError | +-- FileExistsError | +-- FileNotFoundError | +-- InterruptedError | +-- IsADirectoryError | +-- NotADirectoryError | +-- PermissionError | +-- ProcessLookupError | +-- TimeoutError +-- ReferenceError +-- RuntimeError | +-- NotImplementedError | +-- RecursionError +-- SyntaxError | +-- IndentationError | +-- TabError +-- SystemError +-- TypeError +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError 52 exceptions. Now, on this list, how many can be linked to language typing ? +-- TypeError +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError +-- AttributeError And ALL those errors can also be triggered with something a type system would no catch : runtime class generation, unexpexted user input, corrupted database, wrong data header, etc) So basically, relying only on typing will cover about 10% of the error types, most of which are caught with linters such as flake8 and a code intelligence tools such as Jedi. You are making a very weak case.
- naasking 10y ago> And ALL those errors can also be triggered with something a type system would no catch : runtime class generation, unexpexted user input, corrupted database, wrong data header, etc) Runtime class generation and unexpected user input can actually be handled in types. Any language with Type:Type, generative modules, first-class modules, and other variations all handle various forms of runtime type generation. Something like lightweight static capabilities [1] can ensure runtime input conforms to expectations encoded in types. Furthermore, you are way underselling types with that restricted list. NameError, SyntaxError, ImportError wouldn't occur in any typed languages at runtime. LookupError, AttributeError, AssertionError, ArithmeticError wouldn't occur in some of them. Finally, your breakdown doesn't cover how frequent any of these errors occur. For example, even if typing were to solve only TypeError, if TypeError consists of 90% of runtime errors, that would be a huge win. [1] http://okmij.org/ftp/papers/lightweight-static-capabilities.pdf http://okmij.org/ftp/papers/lightweight-static-capabilities....
- sametmax 10y ago> Runtime class generation and unexpected user input can actually be handled in types. Any language with Type:Type, generative modules, first-class modules, and other variations all handle various forms of runtime type generation. Something like lightweight static capabilities [1] can ensure runtime input conforms to expectations encoded in types. How would you if you can't deternmine type in advance, check for the type it will be ? I don't understand. > Furthermore, you are way underselling types with that restricted list. NameError, SyntaxError, ImportError wouldn't occur in any typed languages at runtime. Absolutly not. Those are not linked to typing in any way, and any decent Python editor catch them. > LookupError, AttributeError, AssertionError, ArithmeticError wouldn't occur in some of them. AssertionError ? Serioulsy ? Do you even know what it does in Python ? And LookupError, unless you got constant sized containers, which has nothing to do with types, you can't check that. ArithmeticError ? Come on ! Are you numbers constants ? You need to check the inputs so that they belong to the domain of your problem, at that's it. Nothing to do with types. > Finally, your breakdown doesn't cover how frequent any of these errors occur. For example, even if typing were to solve only TypeError, if TypeError consists of 90% of runtime errors, that would be a huge win. Yes but it's not. My last week have 90% keyerror, and empty values. The input is usually the source of errors. Types are useful, and they come at a cost. Whether your want to use them or not is a technical choice to make. But selling types the way it's been done on this thread is dishonest. Or you are all here working with algo problems and very little user input. Which are the easy program to code. The hard part would be the algo, not the code. If you are dealing with a user app, a web site, a video game, typing will not save you from the 90% of the bugs, and you DO need unit tests.
- piva00 10y agoThis is very patronizing, you're not better than some other developer because you fell in love with OCaml or similar strongly-typed languages. I don't think this kind of criticism is healthy, you're trying to live on top of your ivory tower calling out everyone else who isn't like you as lazy and looking for job security, you really do think there's no other reason for people using languages that you aren't so fond of? Honest question, because this kind of message is just unnecessary aggression.
- vog 10y agoIt really depends on what kind of software you build. There are certain kinds of software for which strongly-typed languages (in the sense of ML, not C/Java) are heavily underused. For those cases, parent's rant is quite appropriate and mostly justified. For example, we really, really want to have our browsers written in a language like Rust, rather than C or C++. Same with web servers, and public-facing web applications.
- easytiger 10y agoIt isn't patronising to point out the failings of weakly typed languages
- LionessLover 10y agoOnly he doesn't - he just makes claims. You happen to agree so you are satisfied - but the test is, would you like that post if it stated something you don't already agree with?
- avip 10y agoTo some extent verbose UT is the tax I'm paying for the joy of not using statically-typed language. I'm happy to pay that, but by no means suggest it's the right trade-off for every scenario.
- pmoriarty 10y agoSorry to be pedantic, but I just have to point this out, since the mistake is made over and over again in this thread: It's not about strongly vs weakly typed languages, but statically vs dynamically typed languages. OCaml is statically typed. Javascript and Python are dynamically typed. What you are talking about are benefits of statically typed languages. For more information, please see: http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html http://blogs.perl.org/users/ovid/2010/08/what-to-know-before...
- ska 10y agoIt is good to maintain an understanding about the difference. On the other hand, many of the most egregious problems are due to weak typing and not as static advocates, well, advocate, static vs. dynamic. Having a really good type inference engine helps make statically typed languages much more productive - but lisps and the like have real advantages too.
- saosebastiao 10y agoProbably more accurate to say strong AND static typing. There are plenty of languages that are statically typed but have very weak type enforcement in the compiler and where idiomatic usage tends to use Any types and reflection and non-hygienic macros and many other type shenanigans, allowing tons of bugs to seep through. OCaml is not one of these languages.
- wtetzner 10y agoWell, if we're really going to be pedantic, then it's about typed vs untyped languages[1]. [1] http://stackoverflow.com/a/9166519 http://stackoverflow.com/a/9166519
- ivanhoe 10y agoI don't remember when was the last time I've made a type related screw up. In my experience almost all bugs in my code (and there is plenty of them of course) are either something completely stupid like a typo, or some edge-cases not covered properly by code logic (which only proper testing and QA can catch). Back in the days of working in C/C++ things were different, I'd quite often forget to do some required casting. Of course, compiler would warn me about it, but it would still kill my flow, so I honestly never regretted moving to the land of dynamically typed languages. To me it significantly reduced the number of errors since you take one layer of complexity out and thus have less of dull errors you can possibly make. Not the other way around, as you imply.
- the_af 10y ago> I don't remember when was the last time I've made a type related screw up Maybe the languages you are using aren't capable of encoding the kind of screw ups you do make as type errors? Typos are trivially covered by most static type checkers, but like you say that's not a very interesting kind of error (except, of course, with languages that silently introduce fresh variables. Those can introduce hard to find errors...). More interestingly, they can also handle other problems like dereferencing nulls, not considering all possible patterns of a value (think "case" conditions), calling a function on the wrong kind of value, etc.
- mannykannot 10y agoI am not saying you are wrong, and I think the OP of this thread overstated the benefits of static typing, but here are a couple of counterpoints: > either something completely stupid like a typo... Typos are a bigger problem in languages that silently instantiate a new variable in response. If it's on the rhs, would you prefer your IDE to warn you about using an unititialized variable, or let it go? Your coding flow is paid for by more failures (some of which will be WTFs) in testing (and testing is part of coding these days, right?) That may be the right trade-off, in which case you may be able to disable warnings in your IDE. >...or some edge-cases not covered properly by code logic (which only proper testing and QA can catch). The 'only' suggests that they are inevitable, and they are in a statistical sense, but whenever my code logic fails to cover an edge case, I ask myself why I overlooked it. Sometimes it is not something I could have anticipated even if I had thought more about what I was doing, but often it is.
- olavk 10y agoWhen you are writing unittests for JavaScript, does 95% of your asserts check that values are of the correct type?
- mamp 10y agoI've been using flow type which has made the transition from F# tolerable, so none of my assertions are type checks but still a lot of null checks because of no language support of option types.
- olavk 10y agoBut if you develop without flowtype, do 95% of your JavaScript unittests consist of type checks?
- monocasa 10y agoIn a proper type system a la Haskell, ML, Rust, etc. you can encode significantly more into the type system than you can with JavaScript's rather anemic offerings, even if it were statically typed. It feels almost like contact programming in that you must handle everything out else the code won't compile.
- agrafix 10y agoI'd agree that Haskell/OCaml/... will get rid of some unit tests that are to be written in other languages, but you still need to test if the logic and semantics of your code are correct.
- _asummers 10y agoSure, but removing entire classes of things you have to check is a really good win. Combine that with the power of fuzzers derived from legal values of the type, you can knock out a lot of bugs from your programs.
- wtetzner 10y agoSure, but just because a type system can't catch all bugs doesn't mean it isn't valuable to use one to completely eliminate certain classes of bugs.