views:

237

answers:

8

Why can't the compiler just compile my code as I type it?

From the user's point of view, it could work as smoothly as syntax colouring does today. If you stop typing for long enough (maybe a couple of seconds) the compilation (not linking) would finish, and code errors would be identified using something like syntax colouring.

It's not like my 3GHz quad core monster computer was really busy doing something else. Why not let it compile all the time?

+3  A: 

Some IDEs do compile (or at least check syntax and some semantics) code as it is typed. For example, I think Eclipse does it. I think Visual Basic 6 (and maybe earlier versions) did this.

Kristopher Johnson
+5  A: 

It can. Or, to be more useful, the answer to this question depends on

  1. What language
  2. What degree of optimization you require
  3. How annoyed you will be if you temporarily type something dumb, and the compiler compiles and injects the result into the binary your are debugging before you can fix it.

Some really strong optimizations would be very messy to mess with on the fly. On the other hand, a basic compilation, if there's no need to worry about assigning offsets for X86 instructions? Sure.

bmargulies
+3  A: 

Note sure what IDE you're using, but that's how VB.NET works.

egrunin
+6  A: 

That's exactly what the VB.NET code editor in Visual Studio does.

The advantage is much more accurate IntelliSense than C#. The disadvantage is that it wastes truly vast amounts of processor time and memory. :-(

Christian Hayter
The same is true for F#.
gnucom
It's only a waste if it _isn't_ being used.
Rocketmagnet
egrunin
@Rocketmagnet: Unfortunately it is used up. There is a very obvious upper limit on the size and complexity of a VB.NET solution. By contrast, I have worked on C# solutions 20 times the size and have yet to reach a practical limit.
Christian Hayter
+1  A: 

I'm not well-versed in compilers or the methods by which code is converted to IL and machine language, etc. But even so I can see how altering my program by one flow control statement can completely invalidate the work a compiler has done up to that point. By adding or changing a single line of code, entire portions of a program may become obsolete, unused, or in some other way require re-evaluation.

I think I'd rather save those CPU cycles for distributed.net or SETI @ Home instead of constantly recompiling my code as I alter it.

JYelton
I don't run SETI@Home, and my CPU won't cry if I throw away its work.
Rocketmagnet
I'm not suggesting that idle CPU cycles are bad, just that they could be used for better things.
JYelton
A: 

That totally depend on the language.

Languages that have context-independent syntaxes "could" pre-compile expressions once typed. However, compilation of such languages project is always fast, so why use the cpu when you can batch quickly the work when the code is ready?

Other languages, like infamously C++, are context-dependent. In most cases, the compiler can't understand an expression without having already read the whole code before the expression. It's really really hard to parse and that's why we have error checking before compilation only now (in VS2010 and other recent ide). In this case it looks like impossible to implement the feature you're asking for.

That said, I'm not a specialist at all. That's all I know about it.

Klaim
A: 

Resharper Plug-in from JetBrains does a minimal syntax check with your code while you are typing. You should try it.

http://www.jetbrains.com/resharper/

Lelis718
Thanks. I will.
Rocketmagnet
A: 

Even interpreted languages like PHP have support for this in the Komodo editor. I'm sure there's many more editors out there that support this for almost any language.

selfsimilar