<p>Static typing means the type of a variable is known at compile time. This means that either you have to specify the type, or the compiler has to infer it (usually both). You also can't change the type of a variable. Many languages, such as Java, C, C#, Go, and Swift use this.</p>
<p>What are the pros and cons of a statically typed language?</p>

<static-typing>
What are the pros and cons of static typing?
CC BY-SA 4.0
2023-05-18T21:51:51.633Z
If this question should be downvoted & closed, then most questions on this website should be downvoted/closed.
ice1000
CC BY-SA 4.0
2023-05-19T02:45:46.633Z
*"Static typing means the type of a variable is known at compile time."* ─ I think this is a very incomplete summary. Static typing means all expressions have a type known at compile-time, not just variables. Also, there is no *a priori* reason that a static type system can't allow the same variable to have different types in different places, and in fact there are static type systems like Typescript where this does happen, via control-flow type narrowing.
kaya3
CC BY-SA 4.0
2023-05-19T06:27:43.383Z
I agree, most questions on this site should be downvoted and closed. The site has a very serious problem with Wadler's law.
Andrej Bauer
CC BY-SA 4.0
2023-05-21T09:53:12.057Z
If most questions on thi site are downvoted and closed then we will have no site@AndrejBauer
Starship
<h1>No need for manual type-checking</h1>
<p>Often while writing scripts in dynamically-typed languages such as JavaScript or Lua I find myself type-checking code manually:</p>
<pre><code>function add(a, b) {
    assert(typeof(a)==&quot;number&quot;&amp;&amp;typeof(b)==&quot;number&quot;)
    return a+b
}
</code></pre>
<p>In statically-typed languages none of this is necessary making some code simpler.</p>

CC BY-SA 4.0
2023-05-19T01:14:22.810Z
2023-05-19T01:14:22.810Z
CPlus
CC BY-SA 4.0
2023-05-19T06:09:39.243Z
Considering the popularity of the is-number Node package (https://www.npmjs.com/package/is-number), this is a pretty valid pro for static typing.
FireTheLost
<h2>Pros</h2>
<p>The Zen of python says &quot;Explicit is better than implicit&quot;. Every function or container has some expectation about what types you can put in and which ones will not.</p>
<p>It makes sense to just write that down explicitly, either as checked static types, unchecked gradual types, or as something like a doc comment.</p>
<h2>Cons</h2>
<p>A function may work just fine on a wide variety of different types. For example, a function that adds two things may work just as well on integers, floats, strings, and arrays. You can define interfaces or traits and implement them for every type you want to support but it's a lot of work just to prove that a function works that you know works anyways.</p>
<p>Even worse, sometimes a function works just fine on many types but it wasn't considered when the function was written so there is no easy way to make it accept a interface instead, especially if the function is from some third party package that can't be modified. Just allowing any type that supports the required operators and methods solves this issue.</p>

CC BY-SA 4.0
2023-05-19T05:46:13.817Z
2023-05-19T05:46:13.817Z
mousetail 'he-him'
<p>First of all, there is no such thing as &quot;static typing&quot;. There are lots of type systems and type checkers, which in general use some combination of code inspection (static) and runtime checks (dynamic). It's not a binary choice. More importantly, type systems have other attributes that may well be more important than static vs. dynamic. Both C and Haskell can be qualified as statically typed, but they are very different!</p>
<p>That said, I find explicit type specification (usually with static checking) useful when I write code in a problem domain that I understand well enough to know what types to choose for my data. On the other hand, I find the obligation to fix types a real nuisance when I do exploratory coding, i.e. coding to help me understand a new problem domain.</p>

CC BY-SA 4.0
2023-05-19T07:21:13.653Z
2023-05-19T07:21:13.653Z
khinsen
CC BY-SA 4.0
2023-05-19T15:45:22.200Z
C has no runtime type checking at all, all type information is removed when the program is compiled
mousetail 'he-him'
CC BY-SA 4.0
2023-05-21T08:06:58.447Z
Indeed, that's a popular choice for languages who prioritize performance.
khinsen
<p>Some pros:</p>
<ol>
<li>Early error detection. Many common errors can be detected at compile time.</li>
<li>Improved performance. The compiler can make optimizations based on known types. It can also make assumptions about memory layouts, function calls, and other things.</li>
</ol>
<p>Some cons:</p>
<ol start="3">
<li><p>Simplicity. It makes it harder to learn.</p>
</li>
<li><p>Code duplication. It can lead to code duplication with similar functionality on different types, especially when the language lacks polymorphism or generics.</p>
</li>
</ol>

CC BY-SA 4.0
2023-05-18T22:32:20.547Z
2023-05-18T22:35:26.200Z
2023-05-18T22:35:26.200Z
Starship
CC BY-SA 4.0
2023-05-20T16:55:41.067Z
Did you use AI to write this?
mousetail 'he-him'
CC BY-SA 4.0
2023-05-21T09:30:27.293Z
No, why do you think I did? @mousetail
Starship
CC BY-SA 4.0
2023-05-21T10:36:06.790Z
It felt a bit like the style of writing AI uses, but based on surveying more of your posts many seem to use the same style but don't seem AI written so maybe the AI detector is just off. (it said 95%)
mousetail 'he-him'
CC BY-SA 4.0
2023-05-21T10:40:26.037Z
What’s the AI detector? @mousetail
Starship
CC BY-SA 4.0
2023-05-21T10:41:27.980Z
https://stackapps.com/questions/9611/openai-detector
mousetail 'he-him'
CC BY-SA 4.0
2023-05-21T14:25:25.610Z
It doesn't sound like AI to me. The detectors are extremely inaccurate in my experience
naffetS
CC BY-SA 4.0
2023-05-21T14:30:26.477Z
I tried entering nonsense and it said 85% fake… @naffetS
Starship
CC BY-SA 4.0
2024-07-07T13:25:20.347Z
@Starship-OnStrike AI detectors tend to flag nonsense as well as poor English.
Rei Miyasaka
# What are the pros and cons of static typing?
Posted by: username_0 on 2023-05-18
Static typing means the type of a variable is known at compile time. This means that either you have to specify the type, or the compiler has to infer it (usually both). You also can't change the type of a variable. Many languages, such as Java, C, C#, Go, and Swift use this.

What are the pros and cons of a statically typed language?
**Tags:** static-typing
> **Comments:**
> **username_1**: If this question should be downvoted & closed, then most questions on this website should be downvoted/closed.  
> **username_2**: *"Static typing means the type of a variable is known at compile time."* ─ I think this is a very incomplete summary. Static typing means all expressions have a type known at compile-time, not just variables. Also, there is no *a priori* reason that a static type system can't allow the same variable to have different types in different places, and in fact there are static type systems like Typescript where this does happen, via control-flow type narrowing.  
> **username_3**: I agree, most questions on this site should be downvoted and closed. The site has a very serious problem with Wadler's law.  
> **username_4**: If most questions on thi site are downvoted and closed then we will have no site@AndrejBauer  
---
## 4 answers
### Answer by username_4 on 2023-05-18. Score: 9
Some pros:

1. Early error detection. Many common errors can be detected at compile time.
2. Improved performance. The compiler can make optimizations based on known types. It can also make assumptions about memory layouts, function calls, and other things.

Some cons:

3. Simplicity. It makes it harder to learn.
4. Code duplication. It can lead to code duplication with similar functionality on different types, especially when the language lacks polymorphism or generics.
> **Comments:**
> **username_7** (2023-05-20): Did you use AI to write this?  
> **username_4** (2023-05-21): No, why do you think I did? @mousetail  
> **username_7** (2023-05-21): It felt a bit like the style of writing AI uses, but based on surveying more of your posts many seem to use the same style but don't seem AI written so maybe the AI detector is just off. (it said 95%)  
> **username_4** (2023-05-21): What’s the AI detector? @mousetail  
> **username_7** (2023-05-21): https://stackapps.com/questions/9611/openai-detector  
> **username_0** (2023-05-21): It doesn't sound like AI to me. The detectors are extremely inaccurate in my experience  
> **username_4** (2023-05-21): I tried entering nonsense and it said 85% fake… @naffetS  
> **username_9** (2024-07-07): @Starship-OnStrike AI detectors tend to flag nonsense as well as poor English.  
---
### Answer by username_5 on 2023-05-19. Score: 6
No need for manual type-checking
Often while writing scripts in dynamically-typed languages such as JavaScript or Lua I find myself type-checking code manually:
function add(a, b) {
    assert(typeof(a)=="number"&&typeof(b)=="number")
    return a+b
}

In statically-typed languages none of this is necessary making some code simpler.
> **Comments:**
> **username_6** (2023-05-19): Considering the popularity of the is-number Node package (https://www.npmjs.com/package/is-number), this is a pretty valid pro for static typing.  
---
### Answer by username_7 on 2023-05-19. Score: 5
## Pros

The Zen of python says "Explicit is better than implicit". Every function or container has some expectation about what types you can put in and which ones will not.

It makes sense to just write that down explicitly, either as checked static types, unchecked gradual types, or as something like a doc comment.

## Cons

A function may work just fine on a wide variety of different types. For example, a function that adds two things may work just as well on integers, floats, strings, and arrays. You can define interfaces or traits and implement them for every type you want to support but it's a lot of work just to prove that a function works that you know works anyways.

Even worse, sometimes a function works just fine on many types but it wasn't considered when the function was written so there is no easy way to make it accept a interface instead, especially if the function is from some third party package that can't be modified. Just allowing any type that supports the required operators and methods solves this issue.
---
### Answer by username_8 on 2023-05-19. Score: 5
First of all, there is no such thing as "static typing". There are lots of type systems and type checkers, which in general use some combination of code inspection (static) and runtime checks (dynamic). It's not a binary choice. More importantly, type systems have other attributes that may well be more important than static vs. dynamic. Both C and Haskell can be qualified as statically typed, but they are very different!

That said, I find explicit type specification (usually with static checking) useful when I write code in a problem domain that I understand well enough to know what types to choose for my data. On the other hand, I find the obligation to fix types a real nuisance when I do exploratory coding, i.e. coding to help me understand a new problem domain.
> **Comments:**
> **username_7** (2023-05-19): C has no runtime type checking at all, all type information is removed when the program is compiled  
> **username_8** (2023-05-21): Indeed, that's a popular choice for languages who prioritize performance.  
---
