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,015

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
  • 15,223