Refactoring Between Compile Time And Run Time Operations

It is desirable to have SyntacticallyTransparentRefactorings.

The distinction between CompileTime and RunTime operations can get in the way.


CeeRefactorStringsToFunctions describes how C's compile time operations on strings, such as sizeof and adjacent string concatenation, are an impediment to "make a change in one place" refactorings.

Moral: the distinction between compile time and run time operations interferes with refactorings that convert between static and dynamic values.

Sizeof and adjacent string concatenation are compile time operators. Strlen and strcat (with memory allocation) are run-time operators that almost, but not exactly, correspond.

sizeof and strlen are not interchangeable operations! sizeof returns the length of the buffer while strlen returns the length of the string within the buffer. Unfortunately, C++ does not provide a string class for constant strings, so I think I understand the basic concern, so I usually will take the alternative to eat the time at start up to create a dynamic string that will never change rather than worrying about making the CPU work too hard.

[It does sound like he's naive about C, but then again, there is one particular special circumstance in which he's not confused:]

  #include <stdio.h>
  #include <string.h>

char foo[] = "hi there"; main() { printf("%d\n", sizeof(foo)); printf("%d\n", sizeof("hi there")); printf("%d\n", strlen("hi there")); }

[...which of course yields 9, 9, 8, and is an example of his intended point (which I don't agree with).]

(See also StringWithoutLength)

Refactoring is easier if (a) compile-time and run-time use the same language, operators, etc., or, at least (b) if they have equivalent operators.

If the operation can be done at compile time, why provide an option to do it at run time? I think the actual need is to have another recognized memory allocation type, data that is allocated and initialized at load time. This could allow templates to create special optimized operations for fixed data types, be they strings or other types of tables.


Similarly, C++ has two totally different syntaxes:


PartialEvaluation removes the distinction between CompileTime and RunTime syntaxes.


Anecdote: many years ago I posted the computer architects' RISC dictum to a notesfile (newsgroup predecessor): "Never do at run time what you can do at compile time".

I was instantly jumped upon by LispLanguage folk such as BrianMarick and ScottPreece who said "Always delay binding to run time, or as late as possible."

Both are right: the former, for performance, the latter, for extensibility and ease of programming. The latter is more important in today's world. ProgramPartialEvaluation? may allow us to get both.


CategoryRefactoring CategoryTime


EditText of this page (last edited November 9, 2005) or FindPage with title or text search