1

Currently reading Steve McConnell "Code complete". "General issues in using variables" chapter, "Binding time" section. He says about variable's binding time in such languages as C++ and Java, that have phases like compiling.

  1. Coding time (use of magic numbers)
  2. Compile time (use of a named constant)
  3. Load time (reading a value from an external source such as the Windows registry file or a Java properties file)
  4. Object instantiation time (such as reading the value each time a window is created)
  5. Just in time (such as reading the value each time the window is drawn)

But is there an alternative for interpreted languages at 2 point? I mean, they also have constants, but they are not compiled. What is their binding time in case of using constants? (JavaScript, Python?)

CoderDesu
  • 1,005
  • 5
  • 10
  • 1
    What do you mean by "interpreted language"? Interpretation is not a property of the language, but of the language *implementation*, i.e. the *interpreter* (duh). You give JavaScript and Python as examples, but literally *every single mainstream implementation* of Python (CPython, IronPython, Jython, GraalPython) and JavaScript (V8, SpiderMonkey, SquirrelFish Extreme, Nashorn, dyn.js, GraalJs, Rhino) has a compiler, and there are interpreters for C++ (Cint, Cling, Ch), so how do you define "interpreted language" in a way that excludes C++? – Jörg W Mittag Nov 18 '21 at 09:17
  • Jorg, thank you for your explanation. Could you advice some literature for me to understand that terms more in detail? – CoderDesu Nov 18 '21 at 15:24
  • @JörgWMittag, most used approach (and I can sprinkle it with "by order of magnitude"). – astrowalker Nov 19 '21 at 06:05
  • 1
    @astrowalker: Your comment appears to have been cut off and does not seem to be a grammatically complete sentence, nor am I able to divine its semantics. That may very well be a problem on my end, since I am not a native speaker of English. Would you care to elucidate? – Jörg W Mittag Nov 19 '21 at 07:09
  • @JörgWMittag "so how do you define "interpreted language" in a way that excludes C++? ", me: by using the following criterion -- most used approach... Btw. I don't argue with your point per se, you are correct in your explanation, but it is not terribly useful in non-academic discussion. – astrowalker Nov 20 '21 at 14:01
  • @astrowalker: So, you would agree that, since all mainstream Python, JavaScript, and Ruby implementations have compilers, those are compiled languages? After all, if compilation is the *only* used approach, then it is most certainly the most used approach. And if I were to write an amazing C++ interpreter that everybody starts using, C++ suddenly stops being a compiled language and becomes an interpreted language, despite the fact that absolutely nothing whatsoever about the language has changed? – Jörg W Mittag Nov 20 '21 at 15:34
  • The JavaScript that I use commonly has one interpreter and _three_ compiler that take increasingly longer to create faster and faster code. – gnasher729 Nov 30 '21 at 08:20

1 Answers1

2

Firstly, Jorg is right but pedantic that compilation and interpretation isn't a property of the language unless it's explicitly specified that way; but nonetheless most languages have a canonical or reference implementation that is either compiled or interpreted, and some languages have features that are difficult to compile unless you ship a copy of the compiler with the runtime (C doesn't have "eval", for example).

Perhaps a more useful way to look at it parse time. For the "magic number" case, the number is encountered directly by the parser and treated as a literal. Whereas the named constant will usually be treated as either a symbol (that is, it's treated as an immutable variable), or a macro (something that is expanded at parse time and converted into a literal).

From that we can make a couple of observations from examples. Python has no true "named constant": it just has variables which by convention the programmer does not change. Original K&R C has no immutable variables either: it has macro expansions which are interpolated into the text of the program before parsing. ("const" was added in C90, but by then the style of the language was already set in the minds of programmers and a lot of old code)

There is an additional wrinkle if you consider that C has three phases, "preprocess", "compile", and "link". There can be variables which are defined as "const" but whose value is not known at compile time, only link time. https://stackoverflow.com/questions/2151831/non-integral-constants/

Certain other languages have pitfalls in which "constants" can be modified, e.g. FORTRAN https://www.ibiblio.org/pub/languages/fortran/ch1-8.html#01

pjc50
  • 10,595
  • 1
  • 26
  • 29
  • 1
    Does this answer the question? "What is their binding time?" Is it actually an alternative binding time compared to compiled implementations? Or do you mean to repace 'compile time' with 'parse time' across the board? What difference does this make? Is binding at parse time any 'earlier' or 'later' than compile time in the sense Code Complete discusses? Answering those would make this a great answer. – joshp Nov 20 '21 at 17:52