Permalink
Browse files

Merge pull request #11 from helmus/master

Evil Eval
  • Loading branch information...
padolsey committed Apr 3, 2012
2 parents 5982466 + bb25f19 commit 6fd5e7d9b44c8d7592cd358a5ba251990bceaa5e
Showing with 53 additions and 0 deletions.
  1. +53 −0 _posts/2012-04-03-evil-eval.md
@@ -0,0 +1,53 @@
+---
+title: Evil Eval, strategy pattern to the resque
+layout: post
+author: Helmus
+authorurl: https://github.com/helmus
+---
+
+A dreadful and utterly painful attempt to create an interchangeable operator.
+
+{% highlight javascript %}
+var op = "";
+if (abc>5)
+ op = "-";
+else if(abc==5)
+ op = "+";
+else
+ op = "*";
+if (op.length > 0)
+ alert(eval("10" + op + "10"));
+{% endhighlight %}
+
+- Inconsistent white space
+- No brackets
+- eval !!
+
+My suggestion, use a strategy pattern.
+
+{% highlight javascript %}
+function multiply(a, b) {
+ return a * b;
+}
+function substract(a, b) {
+ return a - b;
+}
+function add(a, b) {
+ return a + b;
+}
+
+var sign, result, abc = 10;
+
+if( abc < 5 ) {
+ sign = substract;
+}else if( abc == 5 ) {
+ sign = add;
+}else if( abc > 5 ) {
+ sign = multiply;
+}
+
+result = sign(5, 4);
+{% endhighlight %}
+
+Read the story here
+http://stackoverflow.com/a/9989085/1173062

0 comments on commit 6fd5e7d

Please sign in to comment.