Archive

Posts Tagged ‘Development’

Sneaking features through the back door

15/04/2017 Leave a comment

Sometimes programming language developers decide that certain practices are bad, so bad that they try to prevent their use through the language they develop.

For example: In both Java and C# multiple inheritance is not allowed. The language standard prohibits it, so trying to specify more than one base class will result in compiler error.

Another blocking “feature” these languages share, is a syntax preventing creation of derivative classes all together.

For Java, it is declaring a class to be final which might be a bit confusing for new users, since the same keyword is used to declare constants.

As an example, this will not compile:

public final class YouCanNotExtendMe {
    ...
}

public class TryingAnyway extends YouCanNotExtendMe {
    ...
}

For C# just replace final with sealed.

This can also be applied to specific methods instead of the entire class, to prevent overriding, in both languages.

While application developers may not find many uses for this feature, it shows up even in the Java standard library. Just try extending the built-in String class.

But, language features are tools in a tool box.

Each one can be both useful and misused or abused. It depends solely on the developer using the tools.

And that is why as languages evolve over the years, on some occasions their developers give up fighting the users and add some things they left out at the beginning.

Usually in a sneaky, roundabout way, to avoid admitting they were wrong or that they succumbed to peer pressure.

In this post, I will show two examples of such features, one from Java, and one from C#.

C# Extension methods

In version 3.0 of C# a new feature was added to the language: “extension methods”.

Just as their name suggests, they can be used to extend any class, including a sealed class. And you do not need access to the class implementation to use them. Just write your own class with a static method (or as many methods as you want), that has the first parameter denoted by the keyword this and of the type you want to extend.

Microsoft’s own guide gives an example of adding a method to the built in sealed String type.

Those who know and use C# will probably argue that there are two key differences between extension methods and derived classes:

  1. Extension methods do not create a new type.
    Personally, I think that will only effect compile time checks, which can be replaced with run time checks if not all instances of the ‘base’ class can be extended.
    Also, a creative workaround may be possible with attributes.
  2. Existing methods can not be overridden by extension methods.
    This is a major drawback, and I can not think of a workaround for it.
    But, you can still overload methods. And who knows what will be added in the future…

So it may not be complete, but a way to break class seals was added to the language after only two major iterations.

Multiple inheritance in Java through interfaces

Java has two separate mechanisms to support polymorphism: inheritance and interfaces.

A Java class can have only one base class it inherits from, but can implement many interfaces, and so can be referenced through these interface types.

public interface IfaceA {
    void methodA();
}

public interface IfaceB {
    void methodB();
}

public class Example implements IfaceA, IfaceB {
    @override
    public void methodA() {
        ...
    }

    @override
    public void methodB() {
        ...
    }
}

Example var0 = new Example();
IfaceA var1 = var0;
IfaceB var2 = var0;

But, before Java 8, interfaces could not contain any code, only constants and method declarations, so classes could not inherit functionality from them, as they could by extending a base class.

Thus while interfaces provided the polymorphic part of multiple inheritance, they lacked the functionality reuse part.

In Java 8 all that changed with addition of default and static methods to interfaces.

Now, an interface could contain code, and any class implementing it would inherit this functionality.

It appears that Java 9 is about to take this one step further: it will add private methods to interfaces!

Before this, everything in an interface had to be public.

This essentially erases any differences between interfaces and abstract classes, and allows multiple inheritance. But, being a back door feature, it still has some limitations compared to true multiple inheritance that is available in languages like Python and C++:

  • You can not inherit any collection of classes together. Class writer must allow joined inheritance by implementing the class as interface.
  • Unlike regular base classes, interfaces can not be instantiated on their own, even if all the methods of an interface have default implementations.
    This can be easily worked around by creating a dummy class without any code that implements the interface.
  • There are no protected methods.
    Maybe Java 10 will add them…

But basically, after 8 major iterations of the language, you can finally have full blown multiple inheritance in Java.

Conclusion

These features have their official excuses:
Extension methods are supposed to be “syntactic sugar” for “helper” and utility classes.
Default method implementation is suppose to allow extending interfaces without breaking legacy code.

But whatever the original intentions and reasoning were, the fact remains: you can have C# code that calls instance methods on objects that are not part of the original object, and you can now have Java classes that inherit “is a” type and working code from multiple sources.

And I don’t think this is a bad thing.
As long as programmers use these tools correctly, it will make code better.
Fighting your users is always a bad idea, more so if your users are developers themselves.

Do you know of any other features like this that showed up in other languages?
Let me know in the comments or by email!

Advertisements

Beware Java’s half baked generics

13/10/2016 Leave a comment

Usually I don’t badmouth Java. I think its a very good programming language.

In fact, I tend to defend it in arguments on various forums.

Sure, it lacks features compared to some other languages, but then again throwing everything including a kitchen sink in to a language is not necessarily a good idea. Just look at how easy it is to get a horrible mess of code in C++ with single operator doing different things depending on context. Is &some_var trying to get address of a variable or a reference? And what does &&some_var do? It has nothing to do with the boolean AND operator!

So here we have a simple language friendly to new developers, which is good because there are lots of those using it on the popular Android platform.

Unfortunately, even the best languages have some implementation detail that will make you want to lynch their creators or just reap out your hair, depending on whether you externalize your violent tendencies or not.

Here is a short code example that demonstrates a bug that for about 5 minutes made me think I was high on something:

HashMap<Integer, String> map = new HashMap<>();

byte a = 42;
int b = a;

map.put(b, "The answer!");

if (map.containsKey(a))
	System.out.println("The answer is: " + map.get(a));
else
	System.out.println("What was the question?");

What do you expect this code to print?

Will it even compile?

Apparently it will, but the result will surprise anyone who is not well familiar with Java’s generic types.

Yes folks – the key will not be found and the message What was the question? will be printed.

Here is why:

The generic types in Java are not fully parameterized. Unlike a proper C++ template, some methods of generic containers take parameters of type Object, instead of the type the container instantiation was defined with.

For HashMap, even though it’s add is properly parameterized and will raise a compiler error if the wrong type key is used, the get and containsKey methods take a parameter of type Object and will not even throw a runtime exception if the wrong type is provided. They will simply return null or false respectively as if the key was simply not there.

The other part of the problem is that primitive types such as byte and int are second class citizens in Java. They are not objects like everything else and can not be used to parameterize generics.

They do have object equivalents named Byte and Integer but those don’t have proper operator overloading so are not convenient for all use cases.

Thus in the code sample above the variable a gets autoboxed to Byte, which as far as Java is concerned a completely different type that has nothing to do with Integer and therefore there is no way to search for Byte keys in Integer map.

A language that implements proper generics would have parameterized these methods so either a compilation error occurred or an implicit cast was made.

In Java, it is up to you as a programmer to keep you key type straight even between seemingly compatible types like various size integers.

In my case I was working with a binary protocol received from external device and the function filling up the map was not the same one reading from it, so it was not straight forward to align types everywhere. But in the end I did it and learned my lesson.

Maybe this long rant will help you too. At least until a version of Java gets this part right…

Get XML element value in Python using minidom

29/07/2011 Leave a comment

Finally, a “development” post for my “developer” blog.

Recently, I’ve been working on some XML processing programs in Python.

The minidom module is great if you want your XML in a tree, and want tag names and attributes easily accessible, but, what happens if you want the text content inside a tag?

DOM, does not have a “tag value” concept. Instead, every bit of text in the XML, including the indentation is a “text node”, which is parsed as a separate tree element.

That means, that if you have something like this:


<name>John Smith</name>

You will get a tree with two levels: top level for “name” element, for which nodeValue will be None. This element will have a child node (second level of the tree) which will be of type TEXT_NODE an it’s values will be the text “John Smith”.

So far, so good, but, what if the value we want has some XML markup of its own?


<text>This text has <b>bold</b> and <i>italic</i> words.</text>

Now we have a complex tree on our hands with 3 levels and multiple branches.

It will look something like this:

<text>
   |______
          |-"This text has
          |-<b>
          |  |_________
          |            -"bold"
          |-"and"
          |-<i>
          |  |_________
          |            -"italic"
          --"words."

As you can see, this is a big mess, with the text split in to multiple parts on two separate tree levels.

There is no facility in minidom, to get the value of our <text> tag directly.

There is however, a way around it, that is simple but not obvious: you need to “flatten” the desired tag in to an XML string, then strip the tag it self from the string and you will have a clean value.

Here is the code:

def get_tag_value(node):
    """retrieves value of given XML node
    parameter:
    node - node object containing the tag element produced by minidom

    return:
    content of the tag element as string
    """

    xml_str = node.toxml() # flattens the element to string

    # cut off the base tag to get clean content:
    start = xml_str.find('>')
    if start == -1:
        return ''
    end = xml_str.rfind('<')
    if end < start:
        return ''

    return xml_str[start + 1:end]

Just pass the node you want the value of to the function and it will give you back the value as a string, including any internal markup.

I place this code in the public domain, which means you can use it anywhere any way you want with no strings attached.

My FLOSS

13/11/2010 Leave a comment

I decided to start this blog with a post introducing all the Free Software projects I’ve published.
It’s not much, but this is the work I am most proud of.

In case you didn’t know, FLOSS (aside from string used to clean teeth) is an acronym for Free Libre Open Source Software.
Now, you might think that it would be simpler and shorter just to say “Free software”, not to mention, a lot less confusing, but this way people tend to think it is just software you can get free of charge.
Even though most people like getting things for free, surprisingly 0$ cost often has negative connotations.
Besides, its not about cost, its about freedom.

For me though, coding these projects was, first and foremost, about learning.
Implementing each feature required learning the use of a new function or a new technique. Some times even a whole new set of development tools.
Also, unlike projects I do as part of my job, these gave me the freedom to experiment – implement what I wanted in the way I wanted without deadlines, demands or the need to waste time on useless trickery in a futile attempt to protect the final product from being copied.

And there was one additional bonus: ego boost. Seeing the download count and getting comments from users directly was pretty nice, especially when those comments were praises and thanks.

 

LVMTime

This is the first project I’ve ever published. In fact, it went out even before the first commercial app I did as a professional developer hit the market.

It is a “Today screen” plugin for Windows Mobile devices. It displays time and date in various configurations.

This project started out as a way to learn how to write a “Today plugin”.
Since I did not want to do a pointless “Hello world” test, I decided to make it do something useful. At the same time I saw on the forum that people were unhappy with the way date plugin behaved in the then new Windows Mobile 5 OS.
So I made a very simple plugin that just showed date and time on a single line.
I posted it on the forum to see what happens and, to my surprise, it got popular.

So I kept developing it and adding features.
As it turns out, theres a lot more to writing a properly working plugin then MS documentation shows, so along the way I picked up a few tricks that later came in handy on my job.

Two of the neatest (from my perspective) things I’ve done on this project were implementing from scratch a SNTP client to allow synchronizing time from the Internet (just like desktop Windows does) and sticking a small window on the taskbar that looked as though it was an integral part of it.
I actually managed to put the clock display back to where it was in the previous version of the OS, using an outside utility.

At first, I did not think to release the code, though I had no intention to charge money for the software.
I did send it to a couple of people who asked for it because I believed I should share this knowledge as others shared it and allowed me to learn how to write such a plugin.
Later, when I learned about the GNU/GPL and the concept of “Free Software” I properly published the source under GPL v3 license.

Unfortunately, at the time, I was not well familiar with source hosting sites such as SourceForge and Google Code, so I just published the whole thing on the forum I knew.
The down side is, there is no version control and you have to subscribe to the forum to download it.

Some day I might fix it.
For now, the binary version was picked up by a few freeware sites, which added to that ego boost I mentioned earlier:
LVMTime on PocketPCFreeware
LVMTime on FreewarePPC
LVMTime on Softpedia

 

LVMTopBat

This project, like many other FLOSS projects, began as an attempt to “scratch an itch”.

At the time, I had an i-mate Jamin also know as HTC Prophet.
This was a nice and advanced (for those times) smartphone, but it had a very slow processor (200MHz) and little RAM memory.
I wanted a precise battery meter, but all the ones I could find had a lot of fancy features which were both unnecessary and waste of resources.
Plus, I could not find one that looked exactly the way I wanted, so I just wrote one.

It was interesting to learn how to query and interpret battery status data.
I even managed to use the system notification mechanism to avoid constantly polling for data and wasting CPU cycles.

After making a small modification to make it more general, I put this app on the same forum as LVMTime.
Despite being very simplistic with no configuration options at all, it still had some success – several thousand downloads.

Better still, this was the first time someone took my code and made a derivative application with improvements.
And this is the real power of Free Software: collaborative development and continuous improvement.
Here is one such derivative: iBattery

Though not as popular as LVMTime, LVMTopBat also made it to some freeware sites:
LVMTopBat on PocketPCFreeware
LVMTopBat on Softpedia
 

Registry Display plugin

Technically speaking this is not a project, but a part of one.

After gathering together tips and tricks for writing a properly functioning “Today plugin” from various sources on the Internet I wanted to put it all together in a skeleton plugin which could later be used as a base for real projects.
At some point, I even thought about writing an article on it for the CodeProject site.

I never gotten around to writing that article, but I did make a basic plugin.
To demonstrate how to properly implement things like user selected text size and refresh handling I decided to let the plugin display a string from the registry.

Mean while, on xda-developers forum there were people looking to add GUI components to MortScript, a simple but powerful scripting language for Windows Mobile which allowed users with no programming knowledge to automate tasks on their devices.
This plugin example turned out to be useful to them.

It is possible to write registry values using MortScript so any script could use my plugin to display information on the today screen.
It wasn’t fancy, but it worked.

Since this project was so basic I released it in to the public domain, which means anyone can use the code in any way for any purpose no string attached.
Though even something this basic falls under todays ridicules copyright laws, I do not believe in copyrighting basic examples of code, not even under the GPL or BSD style licenses.

 

scr-rotate

This was the firs project I released for GNU/Linux based OS.
Specifically SHR distribution of the OpenMoko project.

It is a graphical application to rotate the screen.

It took me a long while to learn and get used to the different development paradigm of GNU/Linux based environment.
The idea that UI toolkit was something separate from the OS core and that multiple choices were available was a complete novelty.
Programming for Win32 you had one simple API function for creating a window or a button.
Here, you had to choose a widget toolkit and learn its rules.

And before you could do that, you had to familiarize your self with gcc, make and some shell scripting for good masure.
In the end of course, it was well worth it.
And once you do understand the tools and how to use them, you realize that it is the MS way of doing things that is crooked and uncomfortable.

Since the OpenMoko platform was designed specifically for developers to play with, even its most advanced OS is still missing quite a few functions you would find in a commercial phone.
More precisely, the capability is there but the GUI is not.
So it was easy to pick a small feature which I personally was missing and code a fairly simple app to do it.

Once again, this was a learning experience.
And this time, I properly published the sources on a suitable hosting site with open access and version control. There’s even a bug tracking system which I already got to use.

Well, thats all for now.
I hope that in the future, I will have the time to write and release more Free Software projects, maybe even bigger and more useful ones.
For now I do have some bug fixes I want to do on other projects, but as usual 24 hours a day just aren’t enough.

At least, I managed to get this post out.
Thanks for reading.