Anonymous user
Talk:Aspect oriented programming: Difference between revisions
minor fixes, clarifications
(draft task, corrections) |
(minor fixes, clarifications) |
||
Line 20:
== Apologies, Motivation, Suggestions ==
Sorry for not following the process. I may have mis-labeled the article. I wanted to discuss ways to build up code out of optional pieces which toggle various features/forks of the core software, and I chose to call them aspects. In C this is often done with #define. In Java it can be done by extending classes and overwriting their methods, to make a new fork, or in a more generalised way using AspectJ.
It can also be done in many languages using patches from the history. Please feel free to rewrite and reuse any parts of the article or destroy it as you see fit. Perhaps it should be renamed "Build Configuration" or "Optional Code" or "Feature Tree".
Task: Write some code which provides an optional feature which may be enabled or disabled as the developer desires, at as high a level as possible in the language. The task could be to write a factorial function which logs each iteration it takes only when the LOGGING feature is enabled.▼
=== A Possible Task ===
Output of factorial(3) when LOGGING is enabled:▼
▲Task: Write some code which provides an optional feature which may be enabled or disabled as the developer desires, at as high a level as possible in the language. If possible, keep the feature code separate, and avoid polluting the original code. The task could be to write a factorial function which logs each iteration it takes only when the LOGGING feature is enabled.
3 x 2!▼
2 x 1!▼
<lang>4 x 3!
▲3 x 2!
▲2 x 1!</lang>
A runtime solution for Javascript which avoids polluting the original code:
<lang javascript>
###LOGGING.js###
// Overwrite existing global factorial function with a logging version
var oldFactorial = this.factorial;
this.factorial = function(n){
Line 41 ⟶ 45:
};
// This could be
function addLoggingTo(parent,name) {
var oldFn = parent[name];
parent[name] = function() {
console.log(name+"() is being called with "+arguments);
return oldFn.apply(this,arguments);
};
}
addLoggingTo(this,"factorial");
</lang>
|