views:

92

answers:

6

What is the purpose of abstraction in coding:

Programmer's efficiency or program's efficiency?

Our professor said that it is used merely for helping the programmer comprehend & modify programs faster to suit different scenarios. He also contended that it adds an extra burden on the program's performance. I am not exactly clear by what this means.

Could someone kindly elaborate?

+7  A: 

I would say he's about half right.

The biggest purpose is indeed to help the programmer. The computer couldn't care less how abstracted your program is. However, there is a related, but different, benefit - code reuse. This isn't just for readability though, abstraction is what lets us plug various components into our programs that were written by others. If everything were just mixed together in one code file, and with absolutely no abstraction, you would never be able to write anything even moderately complex, because you'd be starting with the bare metal every single time. Just writing text on the screen could be a week long project.

About performance, that's a questionable claim. I'm sure it depends on the type and depth of the abstraction, but in most cases I don't think the system will notice a hit. Especially modern compiled languages, which actually "un-abstract" the code for you (things like loop unrolling and function inlining) sometimes to make it easier on the system.

Tesserex
code reuse requires not only abstraction
Andrey
@Andrey - agreed. Necessary but not sufficient. I just didn't mention the rest in my post because we're talking about abstraction.
Tesserex
Well, if someone/-thing could write down the optimal machine code for a given problem, it would run fater than a solution built on a hundred layers on abstractions (the OS alone accounts for 40 I'd guess :D). But humans can't do this, let alone within reasonable time. A "sufficently smart compilers" could, but for most languages that provide a high degree of abstractions, the compiler not smart enough. But at least at C and C++ compilers are now able to beat most assembly programmers in 99% of the cases -> writing things in Assembly may very well actually decrese performance.
delnan
+2  A: 

Your professor is correct; abstraction in coding exists to make it easier to do the coding, and it increases the workload of the computer in running the program. The trick, though, is to make the (hopefully very tiny) increase in computer workload be dwarfed by the increase in programmer efficiency.

For example, on an extremely low-level; object-oriented code is an abstraction that helps the programmer, but adds some overhead to the program in the end in extra 'stuff' in memory, and extra function calls.

Andrew Barber
+2  A: 

Since Abstraction is really the process of pulling out common pieces of functionality into re-usable components (be it abstract classes, parent classes, interfaces, etc.) I would say that it is most definitely a Programmer's efficiency.

Saying that Abstraction comes at the cost of performance is treading on unstable ground at best though. With most modern languages, abstraction (thus enhanced flexibility) can be had a little to no cost to the performance of the application.

Justin Niessner
+2  A: 

What abstraction is is effectively outlined in the link Tesserex posted. To your professor's point about adding an additional burden on the program, this is actually fairly true. However, the burden in modern systems is negligible. Think of it in terms of what actually happens when you call a method: each additional method you call requires adding a number of additional data structures to the stack and then handling the return values also placed on the stack. So for instance, calling

c = add(a, b);

which looks something like

public int add(int a, int b){
  return a + b;
}

requires pushing two integers onto the stack for the parameters and then pushing an additional one onto the stack for the return value. However, no memory interaction is required if both values are already in registers -- it's a simple, one-instruction call. Given that memory operations are much slower than register operations, you can see where the notion of a performance hit comes from.

Ultimately, every method call you make is going to increase the overhead of your program a little bit. However as @Tesserex points out, it's minute in most modern computer systems and as @Andrew Barber points out, that compromise is usually totally dwarfed by the increase in programmer efficiency.

Chris Thompson
A method such as your add is not an abstraction, it is a brainfart. But it would be inlined anyway, resulting in absolutely zero overhead. Bad example.
delnan
@delnan, It's an illustration, not an example. It is designed to get the OP thinking about what happens when a method is called not advocate actually writing an "add" method. And yes, it would most likely be inlined, but that isn't the point.
Chris Thompson
+1  A: 

Abstraction is a tool to make it easier for the programmer. The abstraction may or may not have an effect on the runtime performance of the system.

For an example of an abstraction that doesn't alter performance consider assembly. The pneumonics like mov and add are an abstraction that makes opcodes easier to remember, as compared to remembering byte-codes and other instruction encoding details. However, given the 1 to 1 mapping I'd suggest its clear that this abstraction has 0 effect on final performance.

torak
+1  A: 

There's not a clear-cut situation that abstraction makes life easier for the programmer at the expense of more work for the computer.

Although a higher level of abstraction typically adds at least a small amount of overhead to executing a discrete unit of code, it's also what allows the programmer to think about a problem in larger "units" so he can do a better job of understanding an entire problem, and avoid executing mane (or at least some) of those discrete units of code.

Therefore, a higher level of abstraction will often lead to faster-executing programs as long as you avoid adding too much overhead. The problem, of course, is that there's no easy or simple definition of how much overhead is too much. That stems largely from the fact that the amount of overhead that's acceptable depends heavily on the problem being solved, and the degree to which working at a higher level of abstraction allows the programmer to recognize operations that are truly unnecessary, and eliminate them.

Jerry Coffin