Wednesday, March 19, 2008

Code WTF #1

  
def logResult(mti,resp,data)
if resp != nil
super(mti,resp,data)
else
super(mti,nil,data)
end
end

Saturday, March 15, 2008

dog - better cat than cat

Every *nix user has used "cat" command. It takes one or more files or stdin, concatenates them and puts result into standard output. Common use case is to print out contents of some file to console.
$ cat ~/myfile.txt
Situation is nowadays different from the time cat was born. At that time, users were usualy connected to one mainframe and all was done on local system. Today we use web2.0 applications, which spread across several systems,. We use our computer as terminal for network services. That's why enhanced version of cat, that supports network URL as source and/or target, several string conversions, newline conversion, and more has emerged. The name of this utility is dog (after all, dogs are much more usefull. Don't you think ?).

Friday, March 7, 2008

Wednesday, March 5, 2008

Things I hatedon't like about Ruby

Ruby is quite cool, I like lot of features, but for me as C/C++ developer there are some things, which are quite annoying. Please note, that I'm aware that lot of issues I mention here are actually Ruby features. This post will be dynamic, so more items will be eventualy added. For each issue, I'll try to search for solution or workaround. Also I encourage you to post any ideas you might have about solving these problems.

  • multiple classes per file
    I know that you can have more classes in C++ as well, but I like Java way. I also know that Ruby doesn't force you into writing multiple classes per file, but it also doesn't discourage people that write spaghetti code. If you end up maintaining project like this, it's hell.
    Solution: Use search to navigate project, or refactor to class per file.

  • variables doesn't have to be declared
    This is anoying, if you have to study some old code. In Java/C++ world you have to declare class/object variables. If you look at the code after some time you have all members of the class neatly organised at declaration section. In ruby you're free to use variable wherever you like, without first declaring it.
    Solution: correct way of using members is to use accessors: attr_reader, attr_accessor, and attr_writer.
    Well ... one friend had good note: "Always code, as if the guy which end up maintaining your code is violent psychopat, who knows where you live."

  • arguments lack type information
    This is related to previous item. Coding with strongly type-based languages is quite addictive. Switching to Ruby, you'll start missing those: "cannot cast x to y" errors from compiler.
    Solution: ?

Monday, March 3, 2008

Eclipse and Java VM memory tuning

How to improve performance of eclipse instalation ? Well, I'm sure there are lot of posts about this on the internet, but it all narrows down to few steps:
  1. get rid of modules you don't use
  2. tune heap size of JavaVM
  3. tune perm size of JavaVM
Let's look closely at these steps.

1. get rid of unused modules
I like to test new features and modules for eclipse. But after some time I found out that these modules consume lot of memory and increase startup time. You're likely to accept slowdown due to some module that you realy use and it improves your coding. But modules, which you use only ocasionaly should go, or at least you should disable them.

2. tune JavaVM heap size
All objects instantiated during runtime live in heap. Therefore, the more heap you have, more likely all objects will fit there. JavaVM needs to have all of it's VM memory online in physical memory. You can specify maximum size of JavaVM memory with -Xmx<size>. Attempt to allocate more than -Xmx memory will end with OOM exceptions. On the other hand there is -Xms option to JavaVM. With this you specify how much of -Xmx should be directly allocated by JVM. Specifying this to same size as -Xmx means there will be longer startup, but application will run smother.

3. tune JavaVM perm size
The perm memory is the area of the VM, which is used to store data structures and class information. In case you have project with huge amount of classes, JVM may reach the limit of MaxPermSize. Simmiliar with -Xmx and -Xms there are two options:
-XX:PermSize=<size>
-XX:MaxPermSize=<size>

Use -vmargs as parameter to eclipse.exe together with these parameters to tune performance of your eclipse. My values are: -vmargs -Xms256m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=64m