Sunday, December 6, 2015

Solidworks, Aspect Oriented Programming, and Cake Pops

Today I Learned:
1) How to design a cuvette holder in Solidworks! Thanks to Erik Jue on this one!

2) Aspect Oriented Programming is a style of programming designed to address a thing called "cross-cutting concerns", which is anything that shows up in a bunch of places in code but is hard to actually make modular for some reason. For instance, lots of times you want to log a bunch of different events whenever they happen. Say, for instance, that whenever you make a call to any of an object's "set..." functions (say, setX, setY, setColor, or setVisible for some kind of shape Object), you want to print all the member variables of that object to a debug file.

You could manually wrap each call to the set function with some logging code, a la

outfile.write("Logging for setX: " + str(myObj.properties))
myObj.setX(5)
outfile.write("Logging for SetVisible: " + str(myObj.properties))
myObj.setVisible(True)

but this isn't maintainable in the least and involves a ton of redundant code

A slightly better thing to do would be to put the logging code inside the set functions for the class, a la

class ExampleShape:
   ...
   def setX(self, x):
       outfile.write("Logging for setX: " + str(myObj.properties))
       self.x = x
...
myObj.setX(5)

which is better but still a pain if you have many set functions in many different kinds of objects, and if you ever decide to log in a different way (say, by using some third-party logging package), then you have to go back and potentially rework a ton of code.

The Aspect Oriented (TM)* solution is to define an aspect with advice and cutpoints that define where to apply the... oh to heck with the official language -- basically you can write a rule that adds some wrapper code around STUFF, and a second rule that defines what STUFF is, and the compiler (assuming you're working in a language with AOP support) will find STUFF with the second rule and add whatever you need to STUFF using the first rule.

Using the logging example, the first rule would say "add a logging statement with the name of the function and all of the object's information", and the second rule would say "any method of the ExampleShape class starting with 'set'".

All this is kind of nifty, but potentially adds a lot of non-locality to code, and frankly I'm not convinced you can't do all of this with some carefully thoguht-out object-oriented programming.

*That's a joke, aspect oriented programming is not, in fact, trademarked.

3) There's a thing called a cake pop, which is a cake, crumbled into little bits, mixed with icing to form a pasty substance, and reformed into a delicious, starchy lolipop. Why didn't anybody tell me about this *before* I was vegan?

No comments:

Post a Comment