uC++ and Shooting Yourself in the foot

Almost at midterms….
One of the most enjoyable courses this term have been my concurrency course CS343. In the class we are using uC++ (mico C++); which is an extension by the University of Waterloo to add high-level concurrency in an object oriented language. So far what we have covered is the ability for “Coroutines” to maintain a separate stack through execution. Using this feature we have been able to solve some interesting problems in novel ways.

Here is an example of some uC++ code. Every coroutine must have a main method which is executing upon calling the inherited Resume() function of every couroutine on the this object. Once inside main(), you can suspend() the coroutine and execution will then unwind back to the original caller. Further resumes() cause the coroutine to start from the same location where the suspend() had occurred (“partial coroutines”).

I am curious to see where the course leads us to in use of uC++. Would be perhaps even neater to learn some GPGPU “ultimate concurrency” in my opinion.

Shooting myself in the foot

Interviews have begun and I made the mistake of applying to 54 jobs. The result is that I now have tons of interviews. Sounds like a good thing? In theory perhaps; although I am finding myself short on time to complete assignments and my extra curricular activities. I will try and get a photo up soon of all the interviews I’ve had once they are complete.

Crossing my fingers

Return to Campus

The North American engineering tradition of dying yourself purple.
The North American engineering tradition of dying yourself purple.
Orientation week is now over and I am begining to return to my normal skin colour as opposed to the beatiful colour purple I was sporting as week. It’s been a while since my last post but hopefully once I am back on track with school work they will come a bit more steadily.

Well where to start…. School has finally started up and I’m excited scared to be entering 3B in Software Engineering. I’m not going to lie and will admit what excites me is that this year is that we are the most senior Software class looking for internships during the winter term which means we have no competition (hardly none) from schools in the US. Hopefully my hard work and awesome program will pay off and my excellent stream of co-op jobs continues.

I want to take a unusual diatribe in something non-Software related, specifically Science Fiction. Today marked the first day of my Science Fiction class; something which I have been looking forward too. I must admit that within the first lecture I am already noticing that by the end of the class I will have undergone a few paradigm shifts.

The professor has already posed some challenging questions that have caused me to begin to rethink my ideals. One such question posed was “What defines a novel to be classified under Science Fiction?”. The challenge was distinguishing Science Fiction from Fantasy; something which turns out to be rather difficult in some situations. A great example was Star Wars. Essentially the original three novels introduces the reader to the novel idea of “The Force” without any explanation. The lack of explanation is something representative of Fantasy, however in Episode I it is explained that the Force is the cause of Midi-chlorians. At this point the genre begins to tread towards being more classicly Science Fiction. Clearly the line between the two can easily be fudged and ultimately the sad answer was that the publishers will simply attach the label Science Fiction if they feel it will cause the book to sell copies.

One of the most interesting & simple questions also posed was:

“Know thy toaster, know thy self”

The professor had began the lecture asking the class for what they believed this sentence meant. Essentially there was no proper answer, but listening to the different meanings people extracted from the sentence was intriguing. Essentially the discussion revolved mainly around points involving technology and its impact on society. If anyone has any interesting takes on the quote please post them, I find it very interesting.

Elegance and Gripes

Much of the programming I have done at the University of Waterloo has laregely been solo endevours. Even in the few cases where the projects supposed to be group efforts; I would end performing the majority of the leg work and keep a meticulous eye on the others. It is not an issue where I believe myself to be a dictator in group scenarios rather that:

Many people are terrible at collaboration and lack elegance to their programming.

What do I mean by elegance?

Most programmers given a task can complete it. The distinction lies to me is in the programmer who can solve the task elegantly. Writing “elegant” (I will continue to use the word for lack of a better one) can be most of the time somewhat straightforward if you are the only person working on the task. It becomes difficult once it is in a group setting. It is bad enough to avoid toe tripping on a large project without having to spend obscene amounts of time to understand a simple function.

An example of certain hair-pulling wtf moments is something similar to what I saw today;

Instead of returning true/false if the function succeeded, enabling panic mode in fact returns the state it is in…

Pulls hair…

My list of things that have given me grief:

  1. Hungarian Notation
  2. What is Hungarian Notation?
    Hungarian Notation essentially boils down to the use of prefix for the purpose of giving a hint to the purpose of a variable.
    There are plenty of issues I found I have disliked with regards to Hungarian Notation, most of them stem from the fact that it is no longer needed, especially in C#.

    Examples:
    bBusy: boolean
    m_wheels : member of a structure/class

    My gripe with Hungarian Notation is that everyone follows a slightly different version of it, resulting in code that is less readable. For instance I prefer to begin all my member variables simply with a _ (underscore) whereas others prefer m_.

    Half the reason of using a constant notation is so that the auto completion (intellisense) can quickly provide the full list of member variables. Using two different notations defeats this purpose.
    Assigning prefixes to denote what the variable represents is becoming less important as well now with the ability for qiuck introspection provided by the IDE.

  3. Needlessly throwing exceptions
  4. There is a time and place when throwing exceptions is the better course of action. My frustration is when developers (specifically one who is working on the same project as I am) simply decides to embed every function call with a try and catch (not the main issue) and then write code prone to many exceptions (the main issue). One simple improvement I had made in the application we are developping was simply that the application was throwing constant DivideByZero exceptions. A simple debugging of the program shows that it is a result of mis-ordered initialization, which was a 1-2 line fix.Instead of investigating the fix initially, what was instead implemetned was a series of try&catches everywhere the divides took place.

  5. Compiler Candy
  6. This one may not be a big deal however I find it a nice bonus. One of the nicest features of using a language still in constant development is to take advantage of the new features that arise. C# is great with providing tons of candy with each new release (looking forward to 4.0). A quick list of features I wish were used more frequently which I really won’t go into detail but will just like to write down:

    1. Automatic Properties:
    2. It helps readability and refactoring. I </3 the mix&match of assigning variables within the class through the Property and directly through the member variable itself.

    3. Lambda Expressions:
    4. If all the function does is compute A+B and isn’t resused save me from navigating to a different project and file just to see the method and use Lambda Expressions.

Next post I’ll probably talk to myself about how I an learning to use the Visual Studio Profiling Tool.
It is a pretty impressive tool, one which I had a book on rather than the simple tutorials online.

Simulateous Learning

I can never seem to focus on learning one particular thing at a time. I have written previously about how I’m investigating IronPython; which probably to most people would be their main focus. I however cannot settle to learn one thing at a time.

I recently picked up a book on iPhone development and decided to spend the next week or so learning the SDK and perhaps creating a few simple applications.

I have a small serious problem of beginning projects and not completing them because I try to create something too large initially. To hopefully fight this urge, I have been restraining myself towards more simple projects with multiple iterations, something which I experiemented (and found success) with when attempting to develop a ridiculously simple image application. I plan to follow a similar approach for an iPhone application.

IronPython

I am currently learning (or rather re-learning ) old as well as some new design patterns in Python. Existing GOF design patterns can be tweaked slightly in Python to take advantage of dynamic programming, as well as some additional dynamic programming exclusive patterns.

So far my largest gripe currently with learning Python/IronPython is the lack of auto-completion.

There are attempts to provide auto completion for IronPython however once you have tasted Intellisense…

It’s tough to go back.

Much of my learning originally of the .NET framework through C# I can attribute almost soley to the autocompletion. A typical case of experimenting with a new Control object might follow something like:

Create object -> browse autocompletion -> find something sounding like what I am looking for -> Visit MSDN

Not to mention that a lot of the code written for WinForms is handled by the visual editor; I am not acustomed to setting many of the “default values” that VS hides nicely from me. I found it useful to howveer keep VS running along side writing any IronPython code and copy/paste much of the code in the designer.


Python making things easier; will IronPython do the same?

A little more IronPython

Working my way very slowly through IronPython in Action, and so far it is pretty neat. Probably the craziest thing is building a GUI application using the interactive console. However in order to get the most of out that joy, the interactive console must be placed on a separate thread. Normally it is on the same thread as the GUI thread causing it to lock up since the Form is in a continous loop.

The IronPython distribution provides a sample script winforms.py. Winforms specifically places the console on a separate thread to play around with the GUI interactively. With winforms imported, you can display the form by calling form.Show () rather than Application.Run (form).

Making changes to the GUI during runtime makes me smile

IDE

I find an important IDE crucial in learning/using a language. Most people I speak to try to persuade me otherwise telling me it is more of a crutch and that I should perhaps stick to basic text editors (ex. VIM). For example; it was a joy to use VS2008 in learning C# (especially the visual editor). I cannot imagine doing most of the work I did without the visual editor and Intellisense. Therefore I set on a search for an IDE to learn IronPython as it is currently not supported in VS (hopefully 2010).

A somewhat quick long browse through the web resulted in me experiementing/investigating the following options:

  1. IronPython studio

    Initially I was excited to see a build of Visual Studio (using the VS SDK) that includes IronPython support. However I quickly discovered that it doesn’t work with IronPython 2.6.

  2. Eclipse

    Rule #1 I learned is to always check Eclipse for a plugin for a lanaguage I’m learning and most of the time there is great support for it. This was true with respect to IronPython and Eclipse does have a plugin for IronPython support. Best of all they included auto completion for most of the .NET libraries which I’ve been reading is somewhat difficult to do given that most of the type checking is done during runtime. The two drawbacks I noticed is that :

    • No Debug support
    • Takes a while to launch script
  3. I looked into some more lightweight options which mainly focused on a simple good text editor.

  4. Notepad++

    I’m a fan of Notepad++. I install it on every machine I use, since I find it to be a very great lightweight but highly extensible editor. Although it has no native support for IronPython, the Python language support was enough to make it useable.

  5. Davy’s IronPython Editor

    This one caught my attention because it is an editor written in IronPython and it is very lightweight. It is still in development so some features seem to be incomplete although it gets the job done nicely.

In the end I decided to go with a more lightweight approach since I am sticking to smaller scripts. The IronPython tutorial (included with the distribution) includes help for setting up on using VS to debug IronPython or using the CLR debugger.

Time to pick up the pace…

IronPython, C# 4.0 & Me


IronPython In Action

This week I had picked up a copy of IronPython in Action.
Lately I have been writing or mentioning at least how I am pretty impressed about the .NET framework/Visual Studio/C#. I found things to be extremely fluid, logical and pretty much straight forward using C# (with the nice room to get complicated if you’d like).

Python though has remained outside my scope of knowledge for quite a while. I have friends who swear by it and I’ve been always been meaning to learn properly a scripting language. I had briefly touched Python in one my CS courses, however it was using Jython which is an implementation of Python written in Java. To be honest, at the time of learning Jython (pretty much Python) scared me a little.

Dynamic programming languages scare me a little
The ability to cast on the fly scares me.
The ability to create member variables on the fly scares me.

Data attributes correspond to “instance variables” in Smalltalk, and to “data members” in C++. Data attributes need not be declared; like local variables, they spring into existence when they are first assigned to. For example, if x is the instance of MyClass created above, the following piece of code will print the value 16, without leaving a trace

I can see the benfit of being able to do it and the marvels that it can accomplish; but it seems it might cause a nightmare for debugging someone else’s code. Although I’ve never tried so hopefully I’m wrong.
I think buying this book is a good start. It seems as if Dynamic programming requires rethinking problems in a different way; similar to trying to solve a problem parellelly rather than sequentially.

The real reasons what attracted me to try and learn IronPython is the development cycle to create a sample WinForms application looks to be much shorter and much of it can afterwards be easily ported to straight C#. Finally a good CS professor once told the class that:

All programmers should know at least one scripting language

C# 4.0

Seems as if learning Dynamic programming design pattern and principiles could be useful especially with the upcomming release of C# 4.0 I was recently going over the WhitePaper for C#4.0 and they are heavily entering the forray of dynamic programming it seems especially.

C# 4.0 introduces a new static type called dynamic. When you have an object of type dynamic you can “do things to it” that are resolved only at runtime:

The C# compiler allows you to call a method with any name and any arguments on d because it is of type dynamic. At runtime the actual object that d refers to will be examined to determine what it means to “call M with an int” on it.
Other things which will allow for code refactoring seem to be introduced in C# 4.0 as well such as optional and named arguments. I can finally stop from creating so may overloaded methods.

Anyways in the mean time I will be playing with IronPython. There is something sexy about interacting with a GUI through an interactive interpreter.

I have failed.

Although I love C# and how it does a good job in hiding a lot of the intricasies of pointers that C++ enjoys exposing. I prefer to have things hidden from me only when I know I understand what is going on. Today was a rude wakeup.
I hadn’t thought it had been that long in which I had written any C++; I guess I was mistaken.

Today I had the pleasure of completely implementing a simple linked list in C++ completely wrong.

In my defense, I really only faulted on aspect; however this aspect I guess now seems very silly to have gotten wrong and I am actually not even sure how I become misinformed when the right answer is so obvious.

Passing pointers by reference

For some delusional reason, I was under the impression that the only difference of passing a pointer and passing a value by reference is that the compiler would hide all the *’s and -> for code readibility.
Turns out (like always) apparently there is more at work than I understood.

Passing a pointer is pretty much passing a value and a local copy is made onto the method’s call stack. Where I err’d is in attempting to re-assign pointer values within the method.

What I should have done was clearly too either pass in a pointer to the pointer or in my case I chose to do:

Now to be fair. I decided to implement LinkedLists in such a way so that the head never needs to be reassigned once declared. This is obviously a stupid way of doing it; with the logical solution to always just return the pointer to the new head.

Anyways, at least I can say that I ended up learning the right semantics.

What did you do with my resources?

‘.”n”;
}
add_action(‘wp_head’, ‘mypage_head’);
?>

I spent a good chunk of my day today really trying to learn about .NET Garbage Collection because as I had found I was under a lot of misconceptions/misunderstandings of how it works.

Dispose()

I was mistaking this for something similar to a destructor which in C# is the finalize method. This method is defined for any class that inherits from the IDisposable Interface. Implementing the interface allows you to also declare the object with the “using” keyword.
The purpose of this method is to provide the programmer a chance to “clean up” the object properly before they are released especially if they have references to unmanaged code (Window handles etc..)

The part where I was thrown off and took a long time reading to understand and learn is that in most cases (pretty much all if done correctly) you still don’t need to call Dispose on the object.

For instance, say you have a managed object with an unmanaged member variable. If the Dispose design method is implemented properly there should be a Finalize method that is declared which calls Dispose () anyways.

But remember, C# doesn’t support destructors. Don’t let the identical syntax fool you.

The problems begin to creep up in doing this in a efficient way. Thankfully .NET provides all the functionality needed to properly do this. When a class implements a finalize method, the way in which it gets garbage collected is differently than other items (Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework).In short, it gets put on a different queue which can result in a large performance cost.

The following example from MSDN shows how to properly implement the Dispose pattern:

You can see how using this proper dispose pattern, if Dispose() was called by the programmer the finalize method is suppressed removing the need to call it again which would essential attempt to clean up the resource twice and we reattain the lost performance.

So I am in the woods on when someone would have to or would actually want to call Dispose on an object considering the GC will eventually call it for me anyways and the following:

What doesn’t happen when you call Dispose():

  1. Calling Dispose does not prioritize the object for garbage collection. It simply unloads the object’s (unmanaged) resources from memory.
  2. Calling Dispose does not deallocate the object from memory. Only the GC does that when it performs a collection of the generation in which the object resides.
  3. The CLR does not insert or run any code not in Dispose. The behaviour of Dispose is defined by the developer.
  4. Dispose must be called explicitly by the application. It is never called by the runtime. The only exceptions are when using C#’s using statement (see ShawnFa’s article for a good explanation of what exactly goes on), and the foreach keyword in C# will result in Dispose being called on the Enumerator
  5. Dispose is NOT threadsafe. This means two threads can call Dispose on the same object at the same time. Like for any other synchronization-sensitive method, take steps to make sure this doesn’t happen. I’ll give an example of when this might happen in the next section.

The oddest aspect as well is that the object is still alive after a call to Dispose (can very well be alive for a long while if the GC is in fact not needed to be called) and can be continued to be used unless the class checks in every method isDisposed which they never do.

Who knew being a garbage could be so fascinating…

Fireballs and ToolTips

So this probably my first none software related post ( /hurray?). Anyways I’m still learning and tinkering around with WordPress (will probably eventually change to a custom theme) and learning PHP.

You might have noticed I had just installed Now Reading Reloaded plugin, so that I can attempt to try and keep track of books I have read/reading. Will add the large list of books I have already read soon.

I have just finished reading Raymond E. Feist’s new book “Rides a Dread Legion”. Although not the best book he has written, it is still quite good (and I am pretty thankful he is still writing additional novels). I think the problem for me is that Pug&Party have just gotten so powerful that I am just surprised when something exists that can even be bothersome to them. This novel had introduced the new spell lore of Demonology; one of the key reasons for my enjoyment of the book. It was very interesting reading the experiences of someone who had through trial and error mastered the art vs. through studies.

Tooltips

What is people’s fascinations or rather intrinsic need to desire everything in a user interface to have a tooltip? I really value the information most tooltips provide however I think that there are a lot of applications (including the one I am developping on) that have gone to far to place a tooltip especially when it is unneeded.

Today I noticed one of the applications I have been using has tooltips for the “OK” & “Cancel” buttons, with the tooltips saying “OK” and “Cancel”as well.

This really brings up an issue taught in my User Interface course to build user interfaces under the assumption that every piece of software you will write can be assumed to be operated/used by someone who has the IQ of a shovel. Where has the idea of developping for power users’ disapeared to? I understand the benefits that developping for all users in an attempt to achieve a curb-cut phenomenon, however the choice to develop a lightweight/simple implementation designed for power users seems to be neglected.

Trying to get feedback faster than you can say…

I must be tired trying to work on the RGB sliders because I don’t seem to be thinking straight. I’m beginning to think that with only one background thread it may be very difficult to get quick response even with the upgrade to the code using unsafe even with my little test images (forget it for sure working with large ones).

I’m spawning a background thread (don’t mention the overhead 🙁 ) everytime the slider changes which begings the method to alter the red value of each pixel rather than “reset” the single thread which didn’t seem any better.

I have to get some actual times down to see if this is even better than just using one single background thread and cancelling the operation everytime the slider changes.

Anyway here’s some of the code so far of my sort of weak attempt to get fast feedback:

Let there be red
Post any help if you have some.

Side Stuff

I guess I’ve made a lot of posts which are really really software oriented versus rather than some other stuff I’ve been doing lately (squash, ice hockey) I just found this project to be really interesting and on my mind.

Part of what is so interesting is noticing how a project envisoined initially so small can just explode if not checked. I seem to be working just as hard to fight thoughts to add the application than I do actually working on it. I guess that is what the benefit of programming something for an assigment versus than for you is. You tend to not go beyond the scope of the assignment, whereas for yourself you let your thoughts run free.

I feel this is something it is good to get a grip on. It seems to be a big problem some people lack in the software industry especially evidently from my current and previous jobs. People never want to freeze the software and label it feature complete; but rather seem to happy tacking on feature after feature.