Jan Varwig

  • Profile
  • Projects

Programming education

March 11, 2011

This video by Glenn Vanderburg about “Software Engineering” and this one by Sandi Metz about Object-Oriented Design provide more educational value than most university courses on software development ever will. They exceptionally well transport two main messages:

  1. Detailed, rigorous software planning (as known by “Waterfall” and seemingly loved by university professors) does not work.
  2. Software design is emergent, evolutionary and very much a process of trial and error and constant refinement. There’s no way to build quality software by designing it upfront and let it be implemented by others.

That both points contradict what’s tought in universities speaks volumes about the values of formal education and learning this stuff on your own.

Continue reading

Flex “undefined property” compiler bug

February 18, 2011

As my colleague Mathias pointed out today, “ActionScript is War”. On the one side are we, innocent developers, on the other hand Adobe’s crappy piece of a compiler.

Introduction

In the program we’re currently working on, we moved some classes to another namespace and generated a bunch of components that were referencing each other. We had

  • VariableList: A container for variables with a DataGroup in it.
  • VariableEditor: A ItemRenderer for the DataGroup in VariableList
  • ValueEditor: A tiny component that was loaded into the VariableEditor depending on the variables type.
  • Numeric-, DropDown-, Generic-, String-, BooleanEditor: Type-specific descendants of the ValueEditor.

The VariableEditor had an internal property globalOrSegment, that was accessed by method in the concrete ValueEditors though a reference to the VariableEditor, namely variableEditor.globalOrSegment. So far so boring. Now the fun part was, that sometimes, this reference caused an error in the compiler:

Access of possibly undefined property globalOrSegmentVar through a reference with static type .components.variables:VariableEditor.

WTF! This message came seemingly random, sometimes the code would compile, sometimes it wouldn’t. Now, performing a clean before each debug run would eliminate the randomess. Code compiled this way would either always fail or always pass. But this just turned our attention to something even stranger: Several certain commits to the VariableEditor that was (or seemed) completely harmless (another if statement in one of the methods) would turn the compiler from friend to foe. There was no pattern.

Good luck I had a vague memory of something like that happening somewhere before (not even remembering where, it wasn’t even ActionScript). That time, the compiler was trippin up because it didn’t resolve the namespaces and classes correctly. Now you’d guess that was a rather simple problem and compilers these days were smart enough to solve this problem?

After all we did TopSort in our first semester.

Well, guess again.

Now you only need a way to give the compiler a nudge in the right direction. A hint which classes to resolve first. I solved this by putting a simple list of references into the body of the VariableList class:

public class VariableList extends SkinnableContainer implements IDisposable
{   
    VariableEditor;
    BooleanEditor;
    DropDownEditor;
    GenericEditor;
    NumericEditor;
    StringEditor;
    TextEditor;
    ...
}

Well that worked. Took me 45 minutes. Could have taken me forever if I didn’t have had that gut feeling. And googling “possibly undefined property” does not exactly yield helpful results, just a bunch of noobs not understanding static typing.

Well, here’s the solution.

Continue reading

Getting Dokuwiki to run with Homebrew nginx and php-fcgi

February 10, 2011

It took me quite a while to get this all right. Put the following into your nginx.conf. Adjust paths as needed.

server {
  root   /Users/USERNAME/development/9ewiki;
  listen       8080;
  server_name  9ewiki;
  server_name_in_redirect off;

  location ~ \.php$ {
    fastcgi_pass   127.0.0.1:8888;
    fastcgi_index  doku.php;
    
    include        fastcgi.conf;
  }

  location / {
    root   /Users/USERNAME/development/9ewiki;
    index  index.html doku.php index.php;

    rewrite  ^/_media/(.*)              /lib/exe/fetch.php?media=$1 last;
    rewrite  ^/_detail/(.*)             /lib/exe/detail.php?media=$1 last;
    rewrite  ^/_export/([^/]+)/(.*)     /doku.php?do=export_$1&id=$2 last;
    rewrite  ^/$                        /doku.php last;


    if (-f $request_filename) { break; }
    if (-d $request_filename) { break; }
    rewrite /(.*)  /doku.php?id=$1  last;
    
    rewrite ^index.php$ /doku.php;
  }
}
Continue reading

Flex 4 performance tips

August 22, 2010

At 9elements, Mathias and me have been working on a fairly large Project since March 2010 which is developed using Air and Flex 4. Recently we started optimizing a few things and found out a few tricks that we didn’t find on the internet before.

CSS

The CSS implementation in Flex for styling components is awful. If you’re not careful, CSS will eat into you CPU consumption because all but its most primitive features make use of very expensive component navigation to look up rules.

You should never use the descendant selectors as they require expensive parent() lookups. Limit yourself to id or Class-Selectors. If you need complex behaviour, it’s more efficient to introduce custom style properties via the [Style] metadata tag than to use descendant selectors. Example:

Instead of this:

<fx:Style>
custom|MyContainer s|Label {
  color: #FF0000;
}
</fx:Style>

...
<custom:MyContainer>
  <s:Label text="Hello World in Red" />
</custom:MyContainer>

<s:Label text="Hello World" />
...

Replace descendant selectors by moving them into the components directly as Stylenames. You can also use Class- or ID selectors, as they are all processed equally. This is of course conceptually unelegant and not even always possible, but trust me, it can be much faster:

<fx:Style>
s|Label.in-myContainer {
  color: #FF0000;
}
</fx:Style>

...
<custom:MyContainer>
  <s:Label text="Hello World in Red" styleName="in-myContainer"/>
</custom:MyContainer>

<s:Label text="Hello World" />
...

Your experience with this may vary depending on how complex your CSS already is but we could reduce CPU-consumption about 60% by optimizing our CSS this way. You might also think that traversing the ancestors, trying to find applicable CSS-Rules is a one-time cost. You’re only half-right. For one, this has to happen every time you add a UIComponent to the display list. Additionally, the CSS framework is far from bug-free. In our app complex rules had the engine run into an endless loop traversing the displaylist up and down, making the component unusable as soon as this happened.

XML

Try not to keep XML-Objects in Memory. Only create them for processing but release them as soon as you’re done with them. Better store the XML data as a string. This is because ActionScript seems to use a horribly inefficient way of storing XML structures. In our app we had about 20MB of XML-Strings, which took about 1.5GB of memory when converted to XML-Objects!

DescribeType vs. DescribeTypeCache

ActionScript uses XML to perform reflection via the flash.utils.describeType method. This has two disadvantages: First the XML implementation is bloated (see last paragraph), second describeType is terribly slow. Actually Flex has a cached implementation of describeType.

For some reason however, Adobe thought it was a good idea to hide this from developers. Instead of using mx.utils.describeType, you should use the mx.utils.DescribeTypeCache.describeTypemethod. To make this a little harder, the DescribeTypeCache class is marked with[ExcludeClass]`, removing it from FlashBuilders autocompletion.

This makes the repeated lookup fasterbut if you are dealing with lots of different classes, the cache starts to become a memory problem. So, an even better idea would be to write your own cache, storing only the properties of each type that you’re interested in, in a plain object or Array that doesn’t use as much memory.

Resizing images

For certain components we were generating previews of images from disk. This should have been easy, with Flash being basically an engine for manipulating images quickly. It wasn’t. At least not in acceptable quality. After much googling the trick was to raise the renderquality of the stage, otherwise the generated thumbnails were jaggy and ugly. Long story short, this is how we did it:

public function scaleBitmap(originalImage:Bitmap, scale:Number):BitmapData
{
    var originalWidth:Number = originalImage.width;
    var originalHeight:Number = originalImage.height;
    var newWidth:Number = originalWidth * scale;
    var newHeight:Number = originalHeight * scale;
    
    var matrix:Matrix = new Matrix();
    matrix.scale(scale, scale);

    stage.quality = StageQuality.BEST;
    originalImage.pixelSnapping = PixelSnapping.NEVER;
    originalImage.smoothing = true;
    var scaledImageData:BitmapData = new BitmapData(newWidth, newHeight); 
    scaledImageData.draw(originalImage, matrix, null, null, null, true);
    stage.quality = StageQuality.HIGH;
    
    return scaledImageData
}
Continue reading

REST in Place now with Multiline Editor

February 5, 2010

Yesterday I pushed the changes introducing the Multiline editor to REST in Place I announced last week.

The definition of forms for editing inline-content is now separated from the Code that deals with Ajax and editing state. This makes it very easy to extend REST in Place with your own Editors.

I also decided to keep the jQuery backwards compatibility for a bit longer.

Check out the changes at http://github.com/janv/rest_in_place.git

Continue reading
Prev Next

Impressum/Datenschutz

Powered by Jekyll with Type Theme