Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Wednesday, October 16, 2013

Reflections


I have been recently been reading over some of Steve Yegge's old posts and they reminded me of a theme I wanted to cover.  There is a idea we call meta-cognition, that testers often use to defocus and focus, to occasionally come back for air and look for what we might have missed.  It is a important part of our awareness.  We try to literally figure out what we don't know and transfer that into a coherent question(s) or comment(s).  Part of what we do is reflect on the past, using a learned sub-conscious routine and attempt to gather data.

In the same way, programming too has ways of doing this, in some cases, in some frames of reference.  This is the subject I wish to visit upon and consider a few different ways.  In some languages this is called reflections, which uses a reification of typing to introspect on the code.  Other languages allow other styles of the same concept and they call them 'eval' statements.  No matter the name, the basic idea is brilliant.  Some computer languages literally can consider things in a meta sense intelligently.

Reflections

So lets consider an example. Here is the class under consideration:
class HighScore {
 String playerName;
 int score;
 Date created;
 int placement;
 String gameName;
 String levelName;
 //...Who knows what else might belong here.
}

First done poorly in pseudo code, here is a way to inject test variables for HighScore:
function testVariableSetup(HighScore highScore) {
highScore.playerName = RandomString();
highScore.gameName = RandomString();
highScore.levelName = RandomString();
highScore.score = RandomNumber();
highScore.create = RandomDate();
//... I got tired.
}

Now here is a more ideal version:
function testVariableSetup(Object class) {
for each variable in class.Variables {
 if(variable.type == String) then variable.value = RandomString();
 if(variable.type == Number) then variable.value = RandomNumber();
 if(variable.type == Date) then variable.value = RandomDate();
}

Now what happens when you add a new variable to your class?  For that matter, what happens when you have 2 or more classes you need to do this in?  The first version can be applied to anything that has Strings, Dates and Numbers.  Perhaps we are missing some types, like Booleans, but that doesn't take too much effort to get the majority of the simple types.  Once you have that, you only have to pass in a generic object and it will magically set all fields.  Perhaps you want filtering, but that too is just another feature in the method.

The cool thing is, this can also be used to get all the fields without knowing what the fields are. In fact, this one is so simple, I am going to show a real life example, done in Java:

//import java.lang.reflect.Field;
//import java.util.*;

 public static List<String> getFieldNames(Object object) {
  List<String> names = new ArrayList<String>();
  for(Field f : object.getClass().getFields()) {
   f.setAccessible(true);
   names.add(f.getName());
  }
  return names;
 }

 public static Object getFieldValue(String fieldName, Object object) {
  try{
   Field f = object.getClass().getDeclaredField(fieldName);
   f.setAccessible(true);
   return f.get(object);
  }catch (Throwable t) {
   throw new Error(t);
  }
 }

 public static Map<String, Object> getFields(Object object) {
  HashMap<String, Object> map = new HashMap<String, Object>();
  for(String item : getFieldNames(object)) {
   map.put(item, getFieldValue(item, object));
  }
  return map;
 }

Lets first define the term "Field."  This is a Java term for a variable, be it public or private.  In this case, there is code to get all the field names, get any field value and get a map of field names to values. This allows you to write really quick debug strings by simply automatically reflecting any object and spitting out name/value pairs. Furthermore, you can make it so that it would filter out private variables, variables with a name like X or rather than getting fields, use it to get properties rather than variables.  Obviously this can be rather powerful.


Eval

Let me give one other consideration of how reflective like properties can work.  Consider the eval statement, a method of loading in code dynamically.  First, starting with a very simple JavaScript function, let me show you what eval can do:

  var x = 10;
  alert( eval('(x + 2) * 5'));

This would display an alert with the value 60. In fact, an eval can execute any amount of code, including very complex logic. This means you can generate logic using strings rather than hard code it.

While I believe the eval statement is rather slow (in some cases), it can be useful for generating dynamically generated code.  I'm not going to write out an exact example for this, but I want to give you an idea of a problem:

for(int a = 0; a!=10; a++) {
  for(int b = 0; b!=10; b++) {
    //for ... {
      test(X[a][b][...]);
    //}
  }
}

First of all, I do know you could use recursion to deal with this problem. That's actually a hard problem to solve, hard to follow and hard to debug. If you were in a production environment, maybe that would be the way to go for performance reasons, but for testing, performance is often not as critical. Now imagine if you had something that generated dynamic strings? I will again attempt to pseudo code an example:

CreateOpeningsFor(nVariables, startValue, endValue) {
  String opening = "for(a{0} = {1}; a{0}!={2}; a{0}++) {";  
  String open = "";
  for(int i = 0; i!=nVariables; i++) {
    open = open + "\n" + String.Format(opening, i, startValue, endValue);
  }
  return open;
}


eval(CreateOpenFor(5, 0, 10) + CreateFunctionFor("test", "X", 5) + CreateCloseFor(5));
//TODO write CreateFunctionFor, CreateCloseFor...  
//Should look like this: String function = "{0}({1}{2});" String closing = "}";

While I skipped some details, it should be obvious to someone who programs that this can be completed. Is this a great method? Well, it does the same thing as the hard coded method, yet it is dynamically built, thus is easily changed. You can log the created function even and place it in code if you get worried about performance. Then if you need to change it, change the generator instead of the code. I don't believe this solves all problems, but it is a useful way of thinking about code. Another tool in the tool belt.

The next question you might ask is, do I in fact use these techniques? Yes, I find these tools to be invaluable for some problem sets. I have testing code that automatically reflects and generates lots of different values and applies them to various fields or variables. I have used it in debugging. I have used it to create easily filterable dictionaries so I could get all variables with names like X. It makes the inflexibly type system into a helpful systems. I have even used it in creating a simple report database system which used reflections to create insert/update statements where the code field names are the column names in the database. Is it right for every environment? No, of course not, but be warned, as a tool it can feel a little like the proverbial hammer which makes everything look like nails. You just have to keep in mind that it is often a good ad-hoc tool, but not always a production worthy tool without significant design consideration. That being said, after a few uses and misuses, most good automators can learn when to use it and when not to.

Another reasonable question is what other techniques are useful and similar to this? One closely related tool would be regular expressions, a tool I don't wish to go in depth on as I feel like there is a ton of data on it. The other useful technique is known as annotations or attributes. These are used to define meta data about a field, method or class. As I think there is a lot more details to go over, I will try to write a second post on this topic in combinations with reflections as they are powerful together.

Friday, September 13, 2013

Online Books I

I'm being somewhat lazy with this, but I thought I'd list out a few online books I have read or at least partly read in the last few months.

The first is a interesting look into the world of Word Perfect.  While there are a large number of issues I have with the author's philosophy, I did appreciate his candor and it gave me some insight into management.

In part of the book, the author was speaking of his training efforts he was giving to managers who were both internal to the company and external.  From the picture the author paints, and from his point of view, he was the sole responsible 'owner' of the three running the company.  Often he seems to indicate he felt like he was not teaching but rather allowing things to go all ways without enough order.  His goal and one of his ideals or guiding principles was to teach people how to act and they would then do the right thing.  From my personal feelings, he seemed very "Type A" personality.

At the time in the book that this excerpt was taken, the company was explosively growing and Word Perfect was quickly becoming number one in the field.  Here is what he told his managers:
WordPerfect Corporation was not a platform for personal achievement, a career ladder to other opportunities, or a challenging opportunity for personal improvement. The company did not put the needs of the individual ahead of its own. The company was not concerned about an employee's personal feelings, except as they related to the company's well-being.

WordPerfect Corporation was not intended to be a social club for the unproductive. While other companies might condone many personal or social activities at the office, ours did not. Things like celebrating birthdays, throwing baby showers, collecting for gifts, selling Tupperware or Avon, managing sports tournaments, running betting pools, calling home to keep a romance alive or hand out chores to the children, gossiping or flirting with co-workers, getting a haircut, going to a medical or dental appointment, running to the cafeteria for a snack, coming in a little late or leaving a little early, taking Friday afternoon off, and griping about working conditions were all inappropriate when done on company time. Even though these activities were condoned by many businesses across the country, we felt there was no time for them at WordPerfect Corporation.

WordPerfect Corporation was also not an arena for political games. A good old boy network method of trading favors inside the company to get things done was frowned upon. Kissing up, back stabbing, and seeking for power and position were inappropriate. Making decisions by compromise, the politician's favorite tool, was not acceptable.

Finally, WordPerfect Corporation was not a "New Age" company. We were neither employee-owned nor a democracy. We were not primarily interested in focusing all our attention on either the employee or the customer. We did not feel it appropriate to check an employee's body fat or prescribe a diet or exercise program. We were not trying to stay in step with current business philosophies.
Now to be fair (in pulling this quote out of the air), he had never run a particularly big company before and he did get advice from the only place he knew to get advice from, his father.  It is a really interesting account and worth a glance at the very least.  This is not because I feel his advice to be particularly good, but because often people develop their management ideas on their own rather than gaining the knowledge by working their way up.

Since most companies grow from a small number of people, by a owner/manager who might know how to run a small company or might have a great product, but have not necessarily studied management, I felt I gained some insight on where their ideas of management come from.  In this case, religion, a large heap of work ethic and a father who had a great deal of influence seem to be largest factors.  I'm not saying there isn't value in those things, just that when you say, "What were they thinking?" sometimes the answer is not all that complicated.  This also means that sometimes when you argue about things using logic, sometimes you will fail to convince someone to change as their ideas come from fundamental bed-rock concepts in their life.  In those cases, you probably can't get them to change and the answers are:

A. Accept it.
B. Attempt to get that person removed.
C. Change companies.

In Word Perfect it sounded like the other two owners elected B.  For better or worse, Word Perfect declined, was sold and is now barely surviving.  I'm not so sure it is related to that choice, but who knows, maybe that sort of culture shock was too much at a delicate time.

On a very different topic, I thought I would talk about a technological 'ebook' I read (parts of) recently.  While I do 'know' JavaScript, I'm not an expert.  I write automation that is usually in Java or C#.  So when I have to go into the 'lower levels' of a browser and use JavaScript, I do find it handy to be able to look up the particular details of how it works.

Sometimes Google or Stack Overflow work well, but other times I need a more general look, and for those cases I have found Eloquent JavaScript to be of value to me.  I don't really have any useful quotes to pull from this book as I think each person will find different parts useful based upon their own level of knowledge in both JavaScript and programming in general.  If you need to study up on JavaScript and don't want to wait (or pay) for a book, this is a decent choice.  If you are interested in a paper variety then this isn't for you.