Suppose I've mostly used untyped programming languages and don't have a great appreciation for types.
Let's do some convincing!
Suppose I've mostly used untyped programming languages and don't have a great appreciation for types.
Let's do some convincing!
For further actions, you may consider blocking this person and/or reporting abuse
Michael Tharrington -
Best Codes -
dev.to staff -
Cecelia Martinez -
Latest comments (60)
You can catch malicious attacks on your input forms of your web apps. Debugging is lot easier and code is easier to read.
Depends on the project imo. Types are useful when building out enterprise applications with lots of inherent complexity and are meant to be read by many different developers. This is because it is easier to quickly deduce what a function expects, and what kinds of data you are working with. Additionally many editors like typescript can use the types to provide intellisense, and static type checking.
However types can get in the way and be overbearing in small projects maintained by one person where the complexity is small. It's just faster to code without types.
This is gonna be a really crappy persuasive argument, but I wanted to give pros and cons of types, and you can decide for yourself 😄
Pros:
myarray[0]
will never throwCons:
That's all I can think of for now. I think typed languages pay off after you get to a medium sized codebase. I usually reach for them on day 1, because I've been burned a lot by errors in dynamic languages, that typed languages don't have.
Hope this helps!
I just like that it decreases mental load for me.
I do not have to think about what the inputs to my functions are, what the outputs are, whether I have to think about null checks, etc.
It just helps me write safer code faster.
Also it's very self-documenting. I can always ask my IDE what other functions I can call on the output of another.
Oh so you're not only a grafikart follower lol
I can see why people like them, but personally I find they slow down development and restrict creativity somewhat. All depends what you're building of course.
I always find my ideas flow into working code much, much faster using untyped vars. Doing it this way also allows for more flexible code, and encourages a better mental understanding of the code. Admittedly this works better for solo coders than for sharing code with others.
Something that bit me, recently: ended up having code-squirreliness because I wasn't paying attention and kept re-typing a structure. The resulting (unintentional) transformations to the stored data made for Good Times™ debugging. Wasn't till I found an excessively-pedantic linter that I figured out where the mangling was happening.
I should have explained better. Clojure is a dynamic language with optional typing. Also it's not an additional tool, it's baked into the language, and only possible because it's dynamic. The program is literally made from interpreting each statement instead of statically compiling everything at once.
There is also some power in not having types, or only optional types.
I really think it's a spectrum. Like in Java I sometimes miss the feature to make type aliases to make strings especially more safe. But 'cheating' with var in Java in selected places feels like a releave. And with Rust you are repeating some types a lot in some cases (but then you could use an alias). And typescript can be tricky when coming from Java, since it only checks compile time.
When I first started programming I thought dynamic languages were faster to program in. That was until I had a decently sized project and had no idea what the hell was going on.
User
object'sname
property tofullName
is as easy.Make the code work for you, don't work for the code!
But what if can directly verify if the code is good, like with a Clojure Repl. It's Scenario 3, as you write is function it's detectly evaluated and you get the result back.
One great example I heard just today on the corecursive podcast is that some typos in your JS are only caught at runtime if at all:
y
isundefined
here, you only notice this once you get a strange result somewhere or, if you're lucky, get aTypeError: y is undefined
. You basically need full test coverage to be sure this isn't happening before deploying.In a typed language like C you don't have this problem:
This will immediately give you a compiler error:
Static typing allows for stronger tooling, catching errors and (with static analyzers) code smells earlier. It becomes more and more important as project size and teams grow larger.
According to Eric Elliott, a veteran programmer and Now a JS advocate,types helps for better productivity and lower bugs in his blog
The TypeScript Tax A Cost vs Benefit Analysis
Eric's thoughts on Dynamic types
I just thought you'll be convinced when some one with more experience offer their wisdom
His thoughts on types
For some time, I saw types as a crutch for people who could not read code well enough to know what to expect in a weak-typed language like JavaScript.
That was when I still did the maintenance for most of the code I handled myself. Working in a bigger team now, I see the advantages of types: inherent documentation, catching hard-to-spot errors during compile time, easier refactoring and integration of typed external dependencies (also the tooling is rather brilliant).
Do not ask this question to an Ada programmer: we are the hard-core of strong typing :-) :-)
Seriously, as many said, correctness is a big plus of strong typing. I say strong typing and not just typing since C, for example, is typed but not strongly (it converts silently between integer/pointers/chars/float/...). In a strongly typed instead no conversions are done by default and mixing different types by mistake is impossible.
Few years ago I wrote a program that made a .dot graph starting from Ada code, showing the dependence among packages. The code uses both package names and the names of the files that contain the package. Initially I used
String
for both of them, but I discovered that I was keeping mixing them (passing a filename when a package name was expected).The solution was very simple: I just defined
and the compiler prevented me from mixing them. A good portion of potential bugs never made beyond the compilation stage.
Of course, strong typing reduces a bit the flexibility in your code. In languages like Matlab or Rudy variable can change value type dynamically and you can call procedures with any type of parameter you want. Indeed, because of this, I find this kind of languages a good choice for short or "short lived" fast-and-dirty software: you write it in little time and it does the work you wanted. If it is short lived, you do not care about maintainability, if it is short, it is quite easy to master it anyway.