I came cross a post today by James Bennett on “magic” in general but more specifically in Python, Django, and Rails.
I tend to think that most critiques which invoke the term “magic” are simply expressing part of the Python philosophy which says “explicit is better than implicit”. Just because something is better though, doesn’t mean it is always the right answer.
James gets to an important point:
“To put it concisely, I think that’s what a lot of critiques of “magic” really boil down to: the notion that just because you can do something doesn’t mean you should.”
He goes one to say that this realization is an important part of becoming a good programmer. I have to wholeheartedly agree. This concept was first crystallized for me when I was reading Inside COM by Dale Rogerson and I read the phrase:
“Just because you can put peas up your nose doesn’t mean that you should”
Both because the phrase communicated a important idea and because it has excellent imagery, I have used as my email signature for the last few years. Truly they are words to live by if you are developing software or not.
Tags: programming
Consider the following Object Oriented code:
class Foo
{
private int bar;
public int getBar()
{
return this.bar;
}
public bool setBar(int i)
{
this.bar = i;
return true;
}
}
When learning the art of writing code, especially if you start in a language like Java, you are told to keep your data private and always use accessor methods to get or set it. But why is that? I was never really told why (maybe I just didn’t ask enough). Consider the alternative in Python:
class Foo:
def __init__(self, i=0):
self.bar = 0
Python being dynamic whatever attributes you set on the instance in the initializer (__init__) dictate what is available in the instance. In this case the data is public and can be accessed though foo.bar if foo is an instance of Foo.
When I started coding in Python suddenly everything was public and all the getter and setter methods were gone replaced with methods which actually did a logical unit of work. Part of the reason for this openess was a C philosophy of trusting the programmer. If someone uses your class and messes with the private data in a bad way, the program will crash, and it will be their fault. The best way to fight back against that would be to write a simple well documented API. Practically, for my uses thus far this have worked well.
Back to the accessors in Java, C++, and C#. Why are they such a must? The first step in the progression tends to be that by keeping your data private, you protect your interface from abuse. Specifically your clients will be less likely to write incorrect code. Having public data is also leaky: clients can write code based on implementation details like the int bar in Foo which an implementer may want to change. Using private data the compiler can enforce this encapsulation.
But really you want the client to have access to the data, that is why you write accessors in the first place. Writing the accessors allow you to control access to this private data. The value of a member variable can be restricted valid ones, and values being read can be validated before being returned. This excellent when it is needed, but most often the trivial accessors are all that is needed. Simply setting the value and returning is all you need. In that case wouldn’t it just be easier to use a public value? It would, but this would define a difficult to change interface. An interface you might want to change when you discover in the future you really want to restrict values for this data.
The interface is the key. The is why writing accessor in the first place is encouraged and the difference between Java and Python. The reason Python doesn’t need accessors.
If you use public data in Java, the client would access the data with foo.bar, and that defines the interface If the implementer were to change it to suddenly require an accessor all of the client code would have to be recompiled with something like foo.getBar() in place of foo.bar. By always using accessors, whenever it is decide that accessing private data needs to be controlled the implementer can add it, no client changes needed.
Great right? Except you have to write all of the accessors (most of the time). Really if the data is so important as to be publicly accessible what is the harm in writing accessors? None really. But in Python you don’t have to.
This is because in Python foo.bar is actually a flexible interface. In the case plan public data, it gives you access to it. But if the need arises and you need to protect the data you can using properties:
class Foo:
_private_bar = 0 # accessible but understood as don't touch.
def _get_bar(self):
return self._private_bar
def _set_bar(self, i):
#Do something interesting like ensure i is an int
self._private_bar = i
bar = property(_get_bar, _set_bar)
Now foo.bar implicitly calls _get_bar and foo.bar = 5 implicitly calls _set_bar. In Python implicit is bad, but here it preserves the interface which is good. If clients are used to accessing bar, they still can while you make the logic behind the access arbitrarily complex, only when needed. I think that is a good thing.
Beyond language features I thought this was interesting because it show how important syntax is when designing a language. How any construct is an interface, and the semantics of that interface can effect how easy or how difficult it is to do something in the confines of that language. A flexible interface increases power, but unclear semantics decreases readability.
Tags: design · programming
It has been just over a year since the trip started and I am finally finished writing about it. It took much longer than I thought to write about the trip then I had assumed. Part of that is writing, even as poorly as I do, takes a lot of time and effort.
It is difficult to reflect on the trip now, it seems a world away as I sit in a cubical. But I am certain this record of the trip will be an personal treasure for me in to the future. As time pasted my ability to recall the events of the trip lessened, and this probably effects the quality of the last entries. On the other hand you can’t spend you time in the moment writing everything down. You will miss a lot. Especially when traveling with a lovely companion. It is a balance, and I am glad to have these records of my trip. Every last picture, these blog entries, and my old warn out shoes from walking around the world.
I can say looking back, like everything hard I have done, it seemed easy. All of the logistics, and troubles worked themselves out and I am left with wonderful memories. These days I feel as if I do less because I am more fearful of things being hard to do. In the past though be it running events great or small, or even traveling the world, all you really need is the unshakable belief that something is possible and before you know it the struggle will be a memory.
Tags: travel
Our plane may have landed in Glasgow but we immediately took the train to Edinburgh, with a plan to return to Glasgow before we made our final trips home.
Edinburgh is an amazing city. It’s summer also features non-stop festivals much like Glasgow, except there is an enormous castle on top of an enormous hill in the middle of the city. When visiting Edinburgh as a backpacker, try and get into the Castle Rock hostel. It is in an old manor right across from the castle. The rooms are spacious and beautiful even when there is 8 people to a room. The staff is really nice and very accommodating. Our original booking only had us there one day, but we were more than willing to pay a cancellation fee to stay there another night. The castle was just outside our window.
Edinburgh is the other city we took advantage of the for-tips tours that we started in Amsterdam. The guide had really interesting things to say such as the crag that the castle was built on was really hard stone which created a weathering shadow when the ice age glaciers last came to shape the land. The whole core of the city was build on this huge rock and the gentle ramp in the glacier shadow of it.
Beyond enjoying the general ambiance of the living city of Edinburgh we made our way to the Scotland Museum of Science and Technology where Dolly’s stuffed body is on display. There was even an exhibit on Canada. Well Northern Canada at least.
With our trip winding to the inevitable end, we took the train to Glasgow, which is far more practical and less dramatic city. We stayed in a University Residence together which was actually a lot of fun. We did some shopping and saw Angus, Thongs, and Perfect Snogging, which seemed like a good thing to do because I don’t think the move has been release in North America at all. It is from the same director as Bend it Like Beckham.
Check out all the Scottish pictures including my shoes after traveling the world and what might be one of the finest shots from the trip in the last album:
Tags: travel
Our week long tour of Ireland started in Dublin. After that we spent a night in Galway, a night in Sligo, and a night in Belfast before flying the worlds shortest flight to Glasgow.
We spent a whole day in Dublin seeing the sites like the Spire (from far too many locations), the Post Office where the Easter Rising started, and a sculpture of Oscar Wilde. We ended up doing the Jameson’s tour over the Guinness tour due to various people’s recommendations. All I can say is the Jameson’s tour was a lot of fun and quite tasty. We stumbled across the Smurfit Institute of Genetics in Trinity College Dublin which was an unexpected treat for Jess. We even made it to the so called Castle which British counter IRA intelligence operated out of.
We took a bus (probably the best way to get around Ireland) from Dublin to Galway. Anyone I talked to about visiting Ireland, especially the Irish, said get out of Dublin, it isn’t Ireland. I have to agree. There is much charm a traveler would miss if they didn’t leave Dublin.
Galway is strange. It has a summer long festival that draws travelers and performers alike making it an intense experience. There is a beautiful green in the middle of the city, it various pubs are full of music every night, and its streets downtown filled with performers and vendors. I wish all city squares were cover in grass like Galway’s is. The city is probably not a typical Irish experience, but an experience that can only happen in Ireland. We enjoyed a traditional session in a pub complete with some fabulous singing by an older man who must have been a regular. Our evening was complete with a singing of Danny boy.
In Sligo we mostly spent time recovering. It was the middle of week so the core was quiet when I ventured down to get some food. Still despite some signs of tough times, the downtown area was beautiful with a stream running through it. It makes me wonder way water isn’t feature more in our cities and town in Canada. There is a stream flowing through up town Waterloo for example, but it is easy to miss it. You have to look passed the concrete barricades.
Belfast was a way to get from point A to point B. It is probably a shame we didn’t see more of the city, but we did see Wall-E which would will make any place special. On the second half of the trip we ended up seeing 4 movies which goes to show how much we need a bit of an escape from our European adventures. Ireland and Scotland were a welcome linguistic cool down after at least a month and a half of ever changing tongues.
Check out the rest of the photographic highlights:
Tags: travel
Brussels has a few sites to see as it mostly a political power house without much for the tourist to do. The central square is the highlight and it is beautiful, but I understand there are more interesting cities to visit is Belgium. Of course there is also the statue of a boy peeing know as the Manneke Pis.
Being such a hot spot for internal business and political traveler there are a lot of hotels some with some relatively decent rates. We spent the time relaxing in a relatively nice hotel recovering for the next leg of our trip.
During the trip we saw Segway tours in Salzburg but we were unable to take it so I was looking for another option. Tragically it wasn’t to be. The one day we could take the tour in Brussels, the operator had to go to a funeral. We picked up some Dali’s at an art show to go with our excellent crossing sign man poster from Berlin. We were about to take a plane from Paris to Dublin with very resistrict lugague requirement so our quest was to find a post office. Before I get into that though a word on the fries: AMAZING. With ketchup or mayo, or even both, the double fried wedges of potato are done right in Brussels.
We took a high speed train from Brussels to Paris. It only took an hour or two. We ran around the streets of Paris with our heavy bags desperately looking for a post office because it was a holiday Monday in Belgium and we had some stuff which we couldn’t take on our flight to Dublin. We packaged up things as best as we could and hoped they would arrive home safely in Canada. Once everything got sorted we got on a bus which took us a few hours outside of Paris to a tiny airport where we boarded the first of two Ryan Air flights. It is a special airline. I would say taking the train is far more rewarding, but using a discount airline to connect the dots is practical.
Tags: travel
We found an odd little caravan based hostel to stay at just outside of Amsterdam. It was a group of trailers parked permanently in a field by a lake. Throughout the park were a variety of interesting paper-mache sculptures of animals. It was relaxing and… kind of trippy.
Amsterdam was the first city where we investigated a network of free walking tours developing in Europe. By free they mean they their guides work for tips. If they do a good job they get paid. You also get to decide after the tour how much it was worth. Our guide was excellent. We saw the channels, the Jewish quarter, and the squatter buildings among other sites.
Jess has a purse emergency, and we were both pleased to find the tourist information people very helpful and understanding of Jess’ need to find a new bag fast. Like with a lot of larger European cities there are beautiful pedestrian only streets which feature tones shopping options. An appropriate pursue or two were acquired to fill the void of the last purse.
Finally we met up with my brother who was attending University in the Netherlands and had some sushi. Enjoy the rest of the pictures from Amsterdam:
Tags: travel
Berlin is rare city in Europe having been the most damaged after war have been heavily rebuilt with a lot of the old charm replaced with concrete and and glass. Some of the structures are truly impressive, but the charm is missing. It is a good change of pace though compared to the more traditional European towns.
While we we were there we found a few sections of the wall, the Holocaust memorial, and even a giant Lego giraffe. The Blue Man Group was also in town so we decided splurge a bit. Seeing a show like the Blue Man group in another country was a lot of fun, unfortunately for Jess who had not seen it before, some of the text and media clips were in German. Berlin is a clean and beautiful city, with plentiful transit and bike lanes everywhere.
Tags: travel
Struggles with accommodations continued. We stayed one night at a dirty, over stuffed hostel. Tiny rooms with 6 people sleeping in them. Fortunately during the week there were enough places with spaces that we could shop around and find one which worked really well for us. A little basement room in a hotel of sorts.
Marie, whom I met on the tour of the great wall, was kind of enough to meet with us and take us a tour of the city which is close to her heart. She lead on a walk through the old city pointing out interesting things like how the main square had been around for so long that the original entrances to the building were a few stairs down. Also how the building used be painted with elaborate designs but it is only now visible in small area. We also had dinner where I got to try some authentic and delicious Czech food and beer.
There are many sites in Prague and a lot going on. On our way to the castle we stumbled into the best marionette shop ever. They take that form of theater very seriously, too bad we never got to see a show. There are buskers galore and a lot of special exhibitions. It is a city truly full of life.
We spent a day away from Prague at Terezin, one of the camps involved in the Holocaust. It was more of a step on the road to the horrible final solution. Jewish people were brought here to be held in a ghetto before eventual transfer deeper into Poland to places like Auschwitz. We ended spending the day with an older woman who was on the same bus as us. The site was difficult and deeply moving to visit. There is an incinerator where many targets of the Holocaust met their end, though more because the terrible conditions and not a planned extermination. That was for other camps. There was also a higher security military prison which featured the slogan arbeit macht frei: “Work will set your free.”
On our last day in Prague we rented paddle boats from one of the island in the river. Much thanks to Marie to letting us know that this was a must. It is cheap, beautiful and fun. I strongly recommend this to anyone who visit the city.
Tags: travel
If memory serves Munich was a pivotal time in the trip when the month of nearly non stop travel caught up to us. Hosteling is great when you are alone and meeting people is a necessity, but when you are traveling as a couple the total lack of privacy become more of an issue. Fortunately most hostels also have relatively inexpensive (and very simple) rooms for 2. Still some of favorite hostels, even when sleeping 8 to a room were to come.
I don’t think I was entirely sure why we were going to Munich, but once we arrived we discovered many great things. First and foremost was the Deutsches Museum which is this huge complex on a island Isar river. Second were the beer halls/gardens where we meet some very enthusiastic Austrian automotive Engineers and had some delicious pretzels. Lastly just walking around and finding a little sushi place made for a relaxing afternoon. We bought some new water bottles too, after the cleaning of Jess’ didn’t go so well.
The Deutsches Museum feature exhibits on all kinds of technology from electricity, to aerospace, to computers, and even an extensive area on biology which Jess just loved. Yes this is even a giant cell you can walk into and see depiction of mitochondria and other organelles (pull that word out of high school). There is interactive stuff too like bridge building and whole series of push button chemical reactions.
Check out the rest of the pictures:
Tags: travel