9
$\begingroup$

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?

$\endgroup$
4
  • 8
    $\begingroup$ If this question should be downvoted & closed, then most questions on this website should be downvoted/closed. $\endgroup$
    – ice1000
    Commented May 18, 2023 at 21:51
  • 3
    $\begingroup$ "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. $\endgroup$
    – kaya3
    Commented May 19, 2023 at 2:45
  • 3
    $\begingroup$ I agree, most questions on this site should be downvoted and closed. The site has a very serious problem with Wadler's law. $\endgroup$ Commented May 19, 2023 at 6:27
  • 1
    $\begingroup$ If most questions on thi site are downvoted and closed then we will have no site@AndrejBauer $\endgroup$ Commented May 21, 2023 at 9:53

4 Answers 4

9
$\begingroup$

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:

  1. Simplicity. It makes it harder to learn.

  2. Code duplication. It can lead to code duplication with similar functionality on different types, especially when the language lacks polymorphism or generics.

$\endgroup$
7
  • $\begingroup$ Did you use AI to write this? $\endgroup$
    – mousetail
    Commented May 20, 2023 at 16:55
  • $\begingroup$ No, why do you think I did? @mousetail $\endgroup$ Commented May 21, 2023 at 9:30
  • 1
    $\begingroup$ 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%) $\endgroup$
    – mousetail
    Commented May 21, 2023 at 10:36
  • 1
    $\begingroup$ stackapps.com/questions/9611/openai-detector $\endgroup$
    – mousetail
    Commented May 21, 2023 at 10:41
  • 1
    $\begingroup$ It doesn't sound like AI to me. The detectors are extremely inaccurate in my experience $\endgroup$
    – naffetS
    Commented May 21, 2023 at 14:25
5
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ Considering the popularity of the is-number Node package (npmjs.com/package/is-number), this is a pretty valid pro for static typing. $\endgroup$ Commented May 19, 2023 at 6:09
4
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ C has no runtime type checking at all, all type information is removed when the program is compiled $\endgroup$
    – mousetail
    Commented May 19, 2023 at 15:45
  • 1
    $\begingroup$ Indeed, that's a popular choice for languages who prioritize performance. $\endgroup$
    – khinsen
    Commented May 21, 2023 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .