Saturday, November 3, 2007

Installing Windows, Ubuntu7.10, Mac OS 10.4.10 on MacBook (Triple Boot)


I am happy with my MacBook with its running Mac OS X 10.4.10. I can't stand working on windows any more, I feel more comfortable with Mac OS.

My master Thesis is running on linux environment(basically Fedora, but I successfully turned it to be Ubuntu)
My current project delivery should be done on windows. An easy solution costing 60$ would be to purchase Fusion.
Although Fusion looks very interesting, I didn't submit to this solution, for the following reasons:
  1. Running virtual machines consumes more memory, in this case I will loose some performance which is something I will certainly need during development.
  2. More memory usage, means more power consumptions, hence less battery life time. in normal cases I enjoy having ~4hrs battery life time with my lovely Mac. this is sthg i don't stand to loose.
  3. to overcome the first problem, I can extend my RAM. this will make the virtual software costs me almost (60+90)$....I really can't afford this for now. (I didn't get paid for salary 3 months ago)
So, I decided to to create a triple boot on my MacBook. It was a very risky step for me..But here we go, I have nothing to loose anyway (keeping in mind 150$ :S, 900 LE when converted to our local currency!!!!!! )

My MAC specs are:
Processor: 2.16GHz Intel Core 2 Duo
Memory: 1 GB 667 MHz DDR2 SDRAM
MAC OS X: 10.4.10

The target is to install Ubuntu 7.10 and WinXP SP2 on MacBook.
steps:
  1. Get BootCamp: I think it may force you to update to Mac 10.5. (luckily I installed it 1 month ago before leopard is released)
  2. Update your Mac OS.
  3. Install rEFIT.
  4. run BootCamp assistant and follow instructions to burn driver CD for windows. (don't proceed with installing steps)
  5. Backup your data. (you may not loose your data if things go smoothly)
  6. Check your disk partitions and identify the Mac Partion. I most cases it is /dev/disk0s2. but if you are not sure, you can verify this by running a shell command using the diskutil:
    $ diskutil list
    resize your HDD using Diskutil by running the following command. first you specify the volume to be resized, and its new size, then the type, and the name of the new volumes followed by their size.
    $ diskutil resizeVolume /dev/disk0s2 70G "Linux" "Linux" 20G "MS-DOS FAT32" "Windows" 20G
  7. insert your XP SP2 CD and hold down the "ALT" key.
  8. install XP on the valid partition, just give it a quick FAT32 format.
  9. you should now have a dual boot(windows with Mac).
  10. insert your Ubuntu 7.10 Live CD.
  11. run the installation normally. You should set up the partition manually. Don't mount the EFI system partition. you need only to mount / to the drive you allocated to your linux installation. I didn't make a SWAP file, I just don't need this for now, I relied on my 1GB RAM.
  12. Continue through the installation steps.
  13. When you reboot, you should have triple boot.

Why Software Developers Leave...

i read a nice article with the title "Software Team Turnover: Why Developers Leave (And What You Can Do About It)".

In this article, the writer, Aaron Reed, discussed the negative impact of the fact that some software developers leave their teams and what makes them do that. it is worth to read.
Aaron specified three main reasons that causes developers to leave:

Money
Like any person in any field, Software Developers too are in need for Money to fulfil their human needs. a developer being paid below the market average might be an unhappy unsatisfied developer. and in general, a Developer can increase his income by jumping to a new place more that he can by getting a raise in the same place.

being happy with what they do
Developer can work in some place or on some project even under paid, if they love what they are working on. in other words.. they also can leave for somewhere else (even paying less) if they were unhappy about they current projects/place/team, if they were bored, or if they were not learning more and not getting experience. actually this is a key reason for developers to move to a new place. the need for getting new experiences and new challenges.

Burnout
Software development is not an easy job. It is like having a hard exam for at least 8 hours per day/ 5 days per week. sometimes the developer gets overloaded by working in some project, and due to his experience in that project or field, and due to the fact that resources are need for that project. management do not move the developers to another project. which makes leaving the whole place is the only get away for the developer. i have seen this more than once before.

i totally Agree with Aaron Analysis of the issue. but i also need to add "Politics" as a candidate cause itself. a broken promise from the management side is fatal from the developer's prospective. disharmony among team members or people in the same work place may cause some of them to be unhappy to the extent that they may not want to stay in that place any more. of course that last reason is not specific to software developers, but it matters and do worth mentioning. actually all of the reason aren't specific to them.

Wednesday, October 31, 2007

Effective Java Programming

Effective Java Programming, by Addison Wesley, is one of nice books i have read in software development material. and i recommend it to any developer developing in Java, starter or senior.

programming by nature is very flexible. you have many choices. it is like creating a statue using clay. You choose at every point; Class names, methods and variable names. public methods, internal implementation algorithms, structure of the package classes,.. all of those and others are left for the programmer to choose.

Effective Java programming comes to introduce a lot of the best practices for Java programmers; to enhance the stability, readability, clarity, reusability and maintainability of their code. it also has guides to the proper use of a lot of the java standard classes.

A note inside the book really draw my attention; in item 8, chapter 3, Wesley was talking about overriding the hashCode method, he stated an example of a phone number class and an implementation of a suggested hashCode, then he said "Writing such hash functions is a topic of active research and an activity best left to mathematicians and theoretical computer scientists.". Wesley encourages his readers to use the state-of-art code. that's the goal of the book.

Actually i was thinking about the classes of the open source libraries we use as i proceeded reading the book. the kind of code that shall be used by thousands of programmers all around the world.

Thursday, September 27, 2007

To AJAX or Not to AJAX? That is the question!

When faced with a new web project these days you typically hear the clients listing AJAX as one of the must haves in their brand new web application. Pretty cool as you might be accustomed yourself to AJAX to the extent that you can hardly imagine returning back to the page reload per click days. But, if you are more sensible (or better yet, your clients are so) you would think twice before entirely abandoning the normal site browsing model for an AJAX based one.


Why? I hear you say. Many reasons, including the fact that we live in the early 21st century, where – get ready for this – not all Internet access devices are equipped with state of the art browsers that can consume your AJAX interfaces or whatever Javascript or CSS magic you throw at them. Many mobile phones (millions to say the least) can hardly parse plain old HTML, some can do CSS but not Javascript


Ok, you tell me. “I will have to do two versions, one that is full of AJAX effects and one old boring HTML only version.” STOP IT, I say, you can't be more wrong. Thank God there could be more elegant solutions to the problem than just writing another application around the same database. I present to you my humble take on the problem. Using the Ruby on Rails Framework (you can apply similar thoughts in other frameworks if you like, and many ideas can be copied easily as they only involve Javascript)

First off, the controllers. The controllers are responsible for receiving requests and sending responses. What we need to do is make them intelligent enough to understand different types of requests and respond accordingly. This is done using Rails magical method “respond_to”

class IssuesController < ApplicationController
def index
...
respond_to do |format|
format.html { # do something }
format.js { # do another thing }
format.json { # and another thing }
format.xml { # ok, enough }
end
end
...
end

In the above example we see that each format will have a different response. This is great for a start, that way we can implement slightly varying responses for the AJAX and the none AJAX calls. To make things easier on us we will implement a very simple case of AJAX. Each rhtml view is rendered in a DIV tag within an rhtml layout. In the none AJAX model, pages are rendered by rendering both the layout and the inner view. In the AJAX model, only the inner view is rendered and is sent back to the browser to replace whatever resides in the content DIV.

So, our controllers will work as follows:

class IssuesController < ApplicationController
def index
...
respond_to do |format|
format.html # will render index.rhtml
format.js { render :layout => false }
# the above line will render index.rhtml but without the layout
end
end
...
end

The above lines made our controller ready to respond to normal or AJAX requests (given that AJAX requests will have the .js format). In the former case it will return back the whole page but in the latter it will omit rendering the layout and only send the content.

Ok, but what we still need two views. I hear you, and fear not, you will have to change nothing. Actually it's only a trivia to adapt your views to this model. Let's see how this can be done.

Here's a normal view code sample, and pardon me, I won't use the link_to helper method for clarity purposes:

...
<div id=”content”>
...
<ul>
<li><a href=”url1”>Link1</a></li>
<li><a href=”url2”>Link2</a></li>
<li><a href=”url3”>Link3</a></li>
</ul>
...
<form target=”url4”>
...
<input type=”submit”>
</form>
...
</div>
...

The above fragment shows a list of links and a form. All should behave in the normal way and reload the page when clicked. Now let's imagine that the user is using a Javascript capable browser. What effect could this coming fragment have on his experience?


<!-- Warning, this fragment requires prototype.js -->
<script>
function ajaxifyLinks(){
// check if there is AJAX support
if(!Ajax.getTransport())return false;
// loop on all links
$$('a').each(function(link){
// attach an event observer to each link's 'onclick' event
Event.observe(link, 'click', function(event){
// call the original url (with .js added) with AJAX
new Ajax.Updater('content',link.href+”.js”);
// stop the browser from following the link
return false;
});
});
// loop on all forms
$$('form').each(function(form){
// attach an event observer to each form's 'onsubmit' event
Event.observe(form, 'submit', function(event){
// send the form contents via AJAX
new Ajax.Updater('content',form.action+”.js”,
{params:Form.serialize(form),
method:'post'});
// stop the browser from submitting the form
return false;
});
});
}
</script>

The above code will transform EVERY link and form in the page to AJAX, that is, in case that the browser supports both Javascript and AJAX. Otherwise links and forms will remain untouched and they will behave as usual.

Of course this is a minimalistic example. We knowingly avoided touching on any special case but, in another installment of this article we will get more intimate with the subject and may be we can handle more aggressive ... techniques!

Sunday, July 22, 2007

Zimbra, violating Open Source terms

Not all pretenders are Open source... This is true for zimbra at least which claims it is open source, but actually I see everythg they r doing is against open source. check this for detailed discussion abt zimbra as open source.
I am still in my battle field working on Zimbra.
We upgraded to the new zimbra version now....
In this new version I found sthg in the code that made my nerves; all variables are written in this format "_158", "_140".
I found this hilarious...

Tuesday, July 17, 2007

Java vs. C++ - Part Two

These are some more differences between Java and C++.

JavaC++
Class attributes and behaviors Attributes are "instance variables".
Behaviors are "methods"
Attributes are "data members".
Behaviors are "member functions"
Extending Class Object Every class is a subclass of Object and so inherits the 11 methods defined by class object, you never create a class from scratch. Even if a class doesn't explicitly use extends in its definition, it implicitly extends Object You can create classes from scratch. A class will inherit attributes and behaviors of a base class ONLY when its declaration explicitly implies that it should.
Packages Every class belongs to a package even if not explicitly specified it will be the default package ( the current directory ) Classes do not have packages. Their containing header files are simply included in source files in which they are to be used
Constructor name Methods other than the constructor of a class ARE ALLOWED to have the same name as the class AND to specify return types but these are not constructors and won't be called when an object is created. It is NOT ALLOWED for any member function other than the constructor to have the same name as the class name AND it is INVALID to specify a return type for any constructor.
Initializing attributes Instance variables CAN be initialized where they are declared IN THE CLASS BODY, by the class's constructor, or they can be assigned values by "set" methods. Instance variables that are not explicitly initialized by the programmer are initialized by the compiler (primitive numeric variables are set to 0, booleans are set to false and references are set to null). Data members can be initialized ONLY by the class's constructor, or they can be assigned values by "set" methods. Instance variables that are not explicitly initialized by the programmer are NOT automatically initialized (they will have whatever values that happened to be stored in their allocated memory space).
Accessing hidden attributes An instance variable hidden by a local variable (having the same name) can be accessed in a method by:
this.variableName
(this is a reference to the current object)
Such a hidden data member can be accessed in the member function by:
ClassName::variableName
or
this->variableName
(this here is a pointer to the current object)
Access modifiers Each instance variable or method declaration in a Class definition must be preceded by an access modifier. Access modifiers are followed by a colon and apply to all following member declarations until overridden by another access modifier. If they were omitted they are implicitly applied by the compiler:
for classes: members are private by default.
for structs: members are public by default.
Package access Members that have no access modifier preceding their declaration are accessible to all the classes included in the same package. Members are either public or private and the only way that another class could access a non-public member of a different class is by inheritance of protected members or by being declared as a friend class of that class.
Memory leaks Are less likely to occur because Java performs automatic garbage collection to help return memory back to the system. When an object is no longer used in a program ( there are no references to the object e.g. if null was assigned to the objects reference ) it is marked for garbage collection. Before the garbage collector returns memory resources to the system it calls the finalize method of the object to be destroyed. Are common because memory is not automatically reclaimed to the system it is the programmer's responsibility to do that himself by freeing the memory in the Class destructor when its task is over.
Multiple inheritance Is not supported but interfaces are supported that allow a class to acquire multiple functionalities from any number of interfaces Is supported.
Over-ridden superclass methods Can be accessed from the subclass by:
super.methodName();
Such an over-ridden base-class member function can be accessed by the derived class by:
BaseClassName::functionName();
Calling superclass constructor To explicitly call the superclass Parent constructor from the subclass Child constructor:
public Child( int a, int b )
{
super( a );
x = b;
}
The calling statement must be the first statement in the subclass constructor
To do the same thing here it goes like this:
Child( int a, int b )
: Parent( a )
{
x = b;
}
here we use the member initializer ( : ) to call the Parent constructor before the body of the constructor is executed
Polymorphism and dynamic binding Applies automatically.
When a reference to a subclass object is assigned to a superclass reference which is then used to invoke a superclass method that is overridden in the subclass definition the java compiler only checks that the class of the reference really has that method and the java interpreter calls the method of the actual data type of the object at execution time.
Does NOT apply automatically.
When a pointer to a derived-class object is assigned to a base-class pointer which is then used to invoke a base-class member function that is overridden in the derived class definition the compiler treats the object exactly as an object of the base-class and the base-class member function is called for the object. This is overcome by declaring the function to be a vertual one and in this case the compiler generates code that would at run time access the appropriate version of the function for the actual object's data type.
final methods and final classes

A method that is declared final cannot be overridden in a subclass.

A class that is declared final cannot be a superclass( cannot have subclasses).

Does not have an equivalent in C++
const functions Java has no equivalent for C++'s const functions A member function can be declared as const, indicating that calling that member function does not change the object.
Abstract classes Are declared by using abstract and have one or more abstract mehtods
public abstract ClassName {
private int instanceVariable;
public int someMethod1()....
public void someMethod2()...
public abstract int method();
//abstract method
}
Must have one or more pure virtual functions
class ClassName {
public:
virtual int someFunction1()...
virtual int someFunction2()...
virtual int function() = 0;
// pure virtual funtion
private:
int dataMember;
...
};
GUI support Normally has packages that support frames, windows, Graphical User Interface components. Does not normally support them. Appropriate third party libraries must be obtained to offer their support.

Saturday, June 30, 2007

The fall of MS fan

For most of life, I have been a Microsoft user. I wasn't a MS' fan. But I didn't find it not so bad working on Window, and it was fair enough for me, although it is not perfect.

I had great passion to try the Vista out, the long delayed version, 6 years or sthg. We all read abt it for many years, and how much it will be sthg fascinating and a new..bla..bla..bla
When I tried it on my PC, i was expecting sthg that could dazzle me.

finally, I got Vista business. I was totally wrong, I regret the day i installed Vista.

well, it is beautiful, the 3-D desktop switcher looks nice.
I faced many problems with Drivers. the problem is not that they r not verified by Vista. the problem that from time to time, I had to redefine the drivers.
my network card kept to be disconnected. I couldn't stay online for couple of hours. the stupid thing that when it disconnects, i have to press by myself "diagnose and repair" then should press "get a new IP" to ask the Vista to get another IP. What is this stupidity!!! didn't I define that already in the connection properties!! the answer to that question is the "ALLOW" :))
If u run sthg on vista, and precipitate away from ur PC, don't expect it is working.

u should wait because u will have to answer "Continue with getting a new IP?", "continue with opening this executable file"..etc I really hate this stupid thg in vista. i don't think i have to press 3 clickes just to run exe file..

The gadgets are terrible, I have never turned them on. they are not innovative and poor.

fighting to have a network connection really annoyed me.
this Vista eats the processor and the Memory. it slows down ur PC. without any tasks running ur RAM usage can be 512 MB. to use a PC now, u have to add $X to buy extra RAM for the Vista and $Y extra to more powerful processor, don't forget that u will pay for the Vista too :)
Vista price is too high. despite the license price is the almost the same since many old versions of windows, it is too high comparing with HW costs these days. Windows license can exceed 20% of ur costs!!!!

the only thg that may keep windows alive is that majority of mobile tools need windows to sync, and some softwares are not still available for MACs.

anyway, I didn't like the Vista thg.
Not friendly, no performance, no stability...nothing.

Thursday, June 28, 2007

Improve your Company using Workflow System

Work flow,
Business Process Management,
Business Process Modeling,
Methodologies,
Work Procedures

All these related words can come together to build an understandable vision of the workflow system.

The Business Process is a group of logically related tasks that come together to form a methodology to implement work procedures.
So companies try to automate these procedures using a workflow system to gain its benefits.

You can find more about workflow here.

So why do we need workflow system?
Some of the benefits we can get by using workflow can be briefed by the following

  • Improved efficiency - automation of many business processes results in the elimination of many unnecessary steps
  • Better process control - improved management of business processes achieved through standardizing working methods and the availability of audit trails
  • Improved customer service – consistency in the processes leads to greater predictability in levels of response to customers
  • Flexibility – software control over processes enables their re-design in line with changing business needs
  • Business process improvement - focus on business processes leads to their streamlining and simplification

How to implement a workflow system for our company?

Well, Thanks god we didn't have to pay for a license to build a system using Microsoft workflow foundation.

We have JBPM (Java Business process management) which is a flexible, extensible workflow management system build on Java and it is an Open source project.

A lot of open source projects are developed like (Open Business Engine The Open for Business Workflow Engine OpenWFE WfMOpen XFlow Micro-Flow JFlower YAWL Zebra Apache Agila Antflow Beexee Dalma Swish etc...)

I believe that RunaWFE is one of the best.

RUNA WFE is an environment for JBoss jBPM workflow engine.
It is a cross-platform end user solution for business process development and execution.

Together RUNA WFE and JBoss jBPM provide an easy to use business process management system.

RUNA WFE provides:

  • an end user GUI to define business processes without any coding: draw flowcharts, define roles and variables, lay out forms
  • an end user GUI to load and execute processes
  • an administrative interface to create and remove users/groups and grant rights
  • a possibility of writing automatic "bots" that can participate in business processes
  • a possibility to code new GUI elements, variable types, organizational structure functions etc. that extend existing RUNA WFE components and will be available to end users through the GUI

Then How to integrate our workflow system with other software?

Integrating RunaWFE with MySQL Database:

Simply, do the following steps:

  1. Add The File mysqldb-ds.xml to your jboss deploy folder "server\default\deploy".
  1. In server\default\conf\hibernate.cfg.xml Change

connection.datasource = java:/MYSQLDS
Dialect = net.sf.hibernate.dialect.MySQLDialect

  1. Important Note : Don't forget to add "com.mysql.jdbc.Driver" to your library This is done by adding "mysql-connector-java-3.1.14.jar" to the lib Folder.

Integrating RunaWFE with LDAP:

Unfortunately integrating RunaWFE with LDAP can be done to a certain level,

In any workflow system you have to define your users, their groups and also their permissions and Roles.

So you can authenticate through LDAP but you still has to add your ldap users in your workflow system so you can define their groups and Roles.

So what we will do now is to authenticate using our LDAP and also import our LDAP users into RunaWFE to manage their groups, Permissions and Roles.

Fortunately RunaWFE helps us in doing both the importing and the authentication.

To Add LDAP Authentication To RunaWFE Do the Following:

  1. Add the LDAPPasswordLoginModule to src\af\logic\ru\runa\af\authentication
public boolean login() throws LoginException {  

SERVER_URL= LDAPImporterResources.getServerURL();

if (callbackHandler == null)

throw new LoginException("No CallbackHandler provided.");

Callback[] callbacks = new Callback[2];

callbacks[0] = new NameCallback("actor name: ");

callbacks[1] = new PasswordCallback("password: ", false);

ExecutorDAO executorDAO = null;

try {

callbackHandler.handle(callbacks);

String actorName = ((NameCallback) callbacks[0]).getName();

if (actorName == null)

throw new LoginException("No actor name was provided.");

char[] tmpPasswordChars = ((PasswordCallback) callbacks[1]).getPassword();

if ((tmpPasswordChars == null) || (tmpPasswordChars.length == 0))

throw new LoginException("No password was provided.");

String password = new String(tmpPasswordChars);

env.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, SERVER_URL);

env.put(Context.SECURITY_AUTHENTICATION, "simple");

env.put(Context.SECURITY_PRINCIPAL, "uid="+ actorName +

",ou=people,"+ LDAPImporterResources.getDC());

env.put(Context.SECURITY_CREDENTIALS, password);

DirContext ctx = new InitialDirContext(env);

ctx.close();

executorDAO = DAOFactory.getInstance().createExecutorDAO();

actor = executorDAO.getActorCaseInsensitive(actorName);

succeeded = true;

return succeeded;

} catch (IOException e) {

throw new LoginException(e.toString());

} catch (UnsupportedCallbackException e) {

throw new LoginException(e.getMessage());

} catch (NamingException e) {

throw new LoginException(e.getMessage());

} catch (InternalApplicationException e) {

throw new LoginException(e.getMessage());

} catch (ExecutorOutOfDateException e) {

throw new LoginException(e.getMessage());

} catch (RuntimeException e) {

throw new LoginException(e.getMessage());

} finally {

try {

DAOHelper.close(executorDAO, true);

} catch (InternalApplicationException e) {

throw new LoginException(e.getMessage());

}

}

}

  1. Modify the LDAP importer Configuration File ldap-importer.properties in server/conf to point to your LDAP server.
  2. Change login-module.properties in server/conf to the LDAPPasswordLoginModule.

#ru.runa.af.authenticaion.NTLMLoginModule=SUFFICIENT
#ru.runa.af.authenticaion.InternalDBPasswordLoginModule=SUFFICIENT
#ru.runa.af.authenticaion.ADPasswordLoginModule=SUFFICIENT
#ru.runa.af.authenticaion.KerberosLoginModule=SUFFICIENT
ru.runa.af.authenticaion.LDAPPasswordLoginModule
=SUFFICIENT

  1. You have to modify some constant variable in class LDAPImporter to accommodate with your LDAP settings.
private static final String OBJECT_CLASS_ATTR_USER_VALUE = "user";
private static final String DISPLAY_NAME = "name";
  1. Run the LDAP importer Tool which will import your users to the ldap users group in the Executors list of Runa.
  2. If you changed the Administrator login password don't forget to change it in the LDAPimporter script.

Finally to create your own processes you can use the Graphical Process Designer which helps design your processes in a very simple way.

Enjoy your Automated Workflow System and your process improvement.

Java vs. C++ - Part One

These are only some of the differences between Java and C++ that I found so far that drew my attention and I think are worth mentioning.

JavaC++
Compiling source codeWhen we compile a source file a .class file is generated in java byte-codes ( machine independent = portability ) that is not translated into machine language instructions until in the run time when interpreted by the Java Virtual Machine
( machine dependent ).
A source file is compiled and linked to produce an .exe file in machine language that is ready to be executed when the file is loaded into memory and run by the operating system.
Function definitions All function definitions and variable declarations are inside Classes even main(). Nothing is allowed to be outside a class except imports and package names.Function definitions are outside class definitions. They have prototypes whose place decides function scope
Including classesImporting a package.* just directs the compiler to the location of classes used in the code so that only these classes are included not the whole package. while in C++ I think that including a header file means that all classes in that header file will be compiled and linked to the source file.
Ending class definitionsClass definitions end with } without a semicolon.Class definitions end with };
Creating objectsAll objects must be created with new, so they are all dynamically allocated in the heap.
Only primitive data types can be allocated on the stack.
Objects can also be created and allocated without new so they are put on the stack in the scope of their declaration.
Declaring a reference:Declaring a reference to an object without using new to allocate the object only reserves space for the reference and no object is created in memory until new is used and the resulting object is given to the reference to point to it.Declaring an object without initializing it makes the compiler call the default constructor for the object's class and memory is allocated for the object in the stack.
Calling static methodsClassName.methodName( args.. );ClassName::functionName( args.. );
or ObjectName.functionName( args..);
Wrapper classesJava has wrapper classes for primitive data types that contains static methods for handling them
e.g. String string = Integer.toString( intVariable );
C++ doesn't normally have them in its standard libraries
Array declaration and memory allocation:An array is considered an object that has to be allocated by new.
   int c[];

declares the array reference

   c = new int[ 12 ];

allocates the array and automatically initialize its elements to zero for numeric primitive types, false for boolean, null for references ( non-primitive types)
or

   int c[] = new int[ 12 ];
// in one step

you can never specify the number of elements in the [] in a declaration unlike C++.

For multiple array declarations:

   double a[], b[];        

a = new double[ 100 ];
b = new double[ 27 ];

or

double a[] = new double[ 100 ],
b[] = new double[ 27 ];
or
double[] a, b;

a = new double[ 100 ];
b = new double[ 27 ];

or

double[] a = new double[ 100 ],
b = new double[ 27 ];

Elements of an array of a primitive data type contain values of that data type.
While elements of an array of a non-primitive data type contian "references" to objects of that data type.

( they have the value null by default )

Allocating and initializing arrays in declarations:

   int n[] = { 32, 27, 64, 18, 95,
14, 90, 70, 60, 37 };
// initializer list

Array size is determined by the number of elements in the list. Here new operator is provided by the compiler and the array object is also dynamically allocated.

When a Java program is executed. the Java interpreter checks array element subscripts to be sure they are valid. If there is an invalid subscript, Java generates an exception: ArrayIndexOutOfBoundsException.

   int c[ 12 ];

declares and allocated memory for 12 int elements without initializing them also memory is not dynamically allocated but instead it is preserved from the start and is allocated in the stack I presume.
For multiple array declaration:

   double a[ 100 ],
b[ 27 ];

Elements of an array of any data type contain values or objects of that data type.
Objects for which an array can be declared must have a default constructor to be called for every element when the array is declared.

initializing arrays in declarations:

   int n[ 10 ] = { 0 };

initialize elements of array n to 0

   int n[] = { 1, 2, 3, 4, 5 };

array size determined by the number of elements in the list

No checking for array bounds is done by the compiler so the programmer must be very carful not to exceed the array length

Multiple subscripted arrays

Doesn't directly support multiple-subscripted arrays but allows the programmer to specify single-subscripted arrays whose elements are also single-subscripted arrays, achieving the same effect.
i.e. arrays of arrays:

   int b[][] = { { 1, 2 },
{ 3, 4, 5 } };

b[ 0 ] is a reference to an array of 2 elements
b[ 1 ] is a reference to an array of 3 elements

   int b[][];
b = new int[ 3 ][ 3 ];

3 by 3 array allocated

   int b[][];
b = new int[ 2 ][];

allocates rows

   b[ 0 ] = new int[ 5 ];

allocates columns for row 0

   b[ 1 ] = new int[ 3 ];

allocates columns for row 1

Directly supports multiple-subscripted arrays and their elements are placed consecutively in memory row after row and the elements are actually located by pointer arithmetic.

   int b[][] = { { 1, 2 },
{ 3, 4, 5 } };

causes an error in C++ "error: declaration of 'b' as multidimensional array must have bounds for all dimensions except the first."

   int b[][ 3 ] = { { 1, 2 },
{ 3, 4, 5 } };

declares and preserves memory for a 2 by 3 multidimensional array b and initializes missing element in the list b[ 0 ][ 2 ] by 0.

Passing arguments

Java does not allow the programmer to decide whether to pass an argument call-by-value or call-by-reference:
Primitive data type variables are always passed call-by-value.
Objects are not passed to methods; references to objects are passed
(call-by-value just like pointers in C++ are) so the method can manipulate the object directly.

You can pass primitive data types or objects either call-by-value or call-by-reference ( using pointers or references to them ).

Returns

When returning information from a method via a return statement:

Primitive data type variables are always returned by value (a copy is returned).
Objects are always returned by reference - a reference to the object is returned.

Objects and primitive data type variables can be returned by value or by reference

Constant variables

Constant variables ( read only variables ) are declared using final.

They are declared using const.

Tuesday, June 19, 2007

Erlang Vs Object Oriented

I think that in the future the functional programming community will be at peak. currently I think that the functional communities are too small to beat the Java community.

the functional is easier to any person. any one can write a functional code. Erlang for example has a light VM that can make profit of multiprocessors availability. this doesn't depend on certain compiler that enables parallelism in Java or C.
if you don't care about performance, think about parallelism as simpler way to structure software in a domain where there is a lot of natural concurrency.

I read a paper "Structured Programming Using Processes" in which the author tried to prove that the Erlang was capable to support a personal accounting software application. Maybe you can take a look to it later.

also, I like to point to Yaws, Yaws is a HTTP high performance 1.1 webserver particularly well suited for dynamic-content webapplications. I ma really amazed by its performance.

well, but why Erlang is not widely used till now?
Well the world likes Java. the type safe language. Security in distributed Erlang is “all or nothing,” so when a node is authenticated to another, it can perform any operation.
process isolation is not complete, a process can flood another process with messages, or it can steal all the CPU cycles by entering into an infinite loop.

Friday, May 25, 2007

Using action+client caching to speed up your Rails application

Too many visitors are visiting your website and loads of dynamic data are being delivered to your clients?. Of those visitors, you have more people reading your site's content than people modifying it? meaning, you get lots more GET requests than POST, PUT or DELETE?

If the above questions are all answered with a YES, then, my friend, you are desperately in need of caching. Caching will help you lessen the load on your servers by doing two main things:
  1. It eliminates lengthy trips to the (slow by nature) database to fetch the dynamic data
  2. It frees precious CPU cycles needed in processing this data and preparing it for presentation.
I have faced the same situation with a project we are planning, we are bound to have much more GETS than any other HTTP command, and since we are building a Restful application we will have a one to one mapping between our web resources (urls) and our application models. The needs of our caching mechanism are the following:
  1. It needs to be fast
  2. It needs to be shared across multiple servers
  3. Authentication is required for some actions
  4. Page presentation changes (slightly) based on logged in user
  5. Most pages are shared and only a few are private for each user
We have two answer the following now, what caching technique and what cache store we will use?

The cache store part is easy, memcached seems like the most sensible choice as it achieves points 1 & 2 and is orthogonal to the other 3 requirements. So it is memcached for now.

Now, which caching technique?. Rails has several caching methods, the most famous of those is Page, Action and Fragment Caching. Greg Pollack has a great writeup on these here and here. Model caching is also an option, but it can get a bit too complicated, so I'm leaving it out for now, it can be implemented later though (layering your caches is usually a good idea)

Page caching is the fastest, but we will use the ability to authenticate (unless we do so via HTTP authentication, which I would love to, but sadly is not the case). This leaves us with action and fragment caching. Since the page contains slightly different presentation based on the logged in user (like a hello message and may be a localized datetime string) fragment caching would sound to be the better choice, no? Well, I would love to be able to use action caching after all, this way I can server whole pages without invoking the renderer at all and really avoid doing lots of string processing by Ruby.


There is a solution, if you'd just wake up and smell the coffee, we are in Web 2.0 and we should think in Web 2.0 age solutions for Web 2.0 problems. What if add little JavaScript to the page that dynamically displays the desired content based on user role. And if the content is really little, why not store it in a session cookie? Max Dunn implements a similar solution for his wiki here and thus the page is served the same with dom manipulation kicking in to do the simple mods for this specific user. Rendering of those is done on the client so no load on the server, and since the mods are really small, the client is not hurt either, and it gets to get the page much faster, it's a win win situation. Life can't be better!

No, It can!. In a content driven website, many people check a hot topic frequently, and many reread the same data they read before. In those cases, the server is sending those a cached page yes, but it is resending the same bits which the browser has in it's cache. This is a waste of bandwidth, and your mongrel will be waiting for the page transfer to finish before it can consume another request.

A better solution is to utilize client caching. Tell the browser to use the version in its cache if it is not invalidated. Just send the new data in a cookie and and let the page dynamically modify itself to adapt to the logged in user. Relying on session cookies for dynamic parts will prevent the browser from displaying stale data between two different session. But the page itself will not be fetched over the wire more than once, even for different users on the same computer.

I am using the Action Cache Plugin by Tom Fakes to add client caching capabilities to my Action Caches. Basically things go in the following manner:
  1. A GET request is encountered and is intercepted
  2. Caching headers are checked, if none exists then proceed
    else send (304 NOT MODIFIED)
  3. Action Cache is checked if it is not there then proceed
    else send the cached page (200 OK)
  4. Action processed and page content is rendered
  5. Page added to cache, with last-modified header information
  6. Response sent back to browser (200 OK + all headers)
So how to determine the impact of applying these to the application
  1. We need to know the percentage of GET requests, which can be cached as opposed to POST, PUT and DELETE ones
  2. Of those GET requests, how many are repeated?
  3. Of those repeated GET requests, how many originate from the same client?
Those numbers can tell us if our caching model works fine or not, this should be the topic of the next installment of this article

Happy caching

Thursday, May 24, 2007

Agile Process

My first reading about Agile was the Agile Manifesto. I think this was enough to get the whole picture of the agile philosophy.
I really don't recommend that you get a book to read about the agility; it will be a contradiction to read a book as long as the Agile methods emphasize working software as the primary measure of progress. It is better you spend the time understanding your working environment and maturity to apply the best process that can fit with it.
I lived a similar experience with eSpace when they were aiming to get a CMM certificate, CMMI or whatever they will call it after couple of years.
I think that finally the people here got the good decision by putting this plan away, and put their own standards, the standards best fitting with their teams and their environment. I doubt their was a need to some white collar guy to supervise our documents and our working styles to decide whether we fit to Mr XYZ standards or not. I also got the chance to meet some guys from another company having this certificate. What I found is that they modified their hierarchical structure just to accommodate the standard although their team wasn't capable at all to support this hierarchy.

Anyway, let's move to another point...
When we talk about the agile process we should identify very well any possible limitations before adopting it. It is very clear for me that it needs mature people, and this can be considered as one of the limitations.
Another factor that may limit the agile adoption is the "distributed software". Lately all our projects are distributed. Customers are from Europe, USA, Gulf. Developers are working remotely from different areas.
What are the challenges behind such distributed agile work???

The answer of this question can be summarized in knowing that agile methods mainly rely on informal processes to facilitate coordination while distributed software development typically relies on formal mechanisms.

Challenges in Agile Distributed Development
  1. Communication need vs. communication impedance: How can we achieve a balance in formality of communication in agile distributed environments?
  2. Fixed vs. evolving quality requirements: the Agile relies on ongoing negotiations between the developer and the customer while Distributed Development often relies on fixed, upfront commitments on quality requirements.
  3. People- vs. process-oriented control: We appreciate the people-orientation the most.
  4. Lack of cohesion: Generally speaking about the distributed environment, people may feel In distributed development, participants are less likely to perceive themselves as part of the same team when compared to co-located participants. the agile adds some more excitement to it :)

Practices
  1. Process Refactor: Continuous process adjustments instead of following strictly the agile practices. It is also recommended to document the requirements at different levels of formality.
  2. Knowledge Spreading: the team should share their knowledge regarding different domains(business, code, test cases..etc). Alot of tools were developed to reduce the overhead of knowledge-sharing activities. Code/process Repository is a must. the Wiki also is a very important way to share the How-Tos between members. I can't neglect the Bugzilla with its role in creating database to help teams report issues and assign priorities.
  3. Short Iterations: any iteration should not exceed 2 weeks. short iterations help to detect any misunderstandings for the project business and prevent any time waste.
  4. Start with well-understood functionalities: In order to create the best atmosphere for developers there should be a solid sand to start to be familiar with the processes, tools, and the application. this can be some kind different that the agile which advocates the development of features prioritized as critical by the customer.
  5. Improve Communication: Synchronized work hours are very important for the team. Also try to make the informal communication be done through formal channels. for example, let it through the emails so that it can be archived. In the distributed Projects it is recommended that the project leader/manager should be involved in the communication and the synchronization process than the Agile practice. finally, some daily mechanisms should be done to maintain minimal communication like morning online meeting.
  6. Building Trust: trust involves both the team and the customers.

References:
1. Ebert, C. and Neve, P.D. Surviving global software development. IEEE Software 18, 2 (Mar./Apr. 2001), 62–69.
2. Highsmith, J. and Cockburn, A. Agile software development: The business of innovation. IEEE Computer 34, 9 (Sept. 2001), 120–122.
3. Matloff, N. Offshoring: What can go wrong? IT Professional (July/Aug.2005), 39–45.

Wednesday, May 23, 2007

Virtual functions in C++


"In order to implement the concept of Polymorphism which is a corner-stone of OOP the C++ compiler has to find a way to make it possible."


Lets see how the story begins.

Derived classes inherit member functions of the base class but when some member functions are not exactly appropriate for the derived class they should provide their own version of these functions to override the immediate base class' functions and make their own objects happy. So if any of these functions is called for a derived object the compiler calls its class' version of that function.

This works quite fine when the types of objects are known at compile time so that the compiler knows which function to call for each particular object. The compiler knows where to find the copy of the function for each class and so the addresses used for these function calls are
settled at compile time. ( static binding )

Suppose that we have a lot of derived objects at different levels of the inheritance hierarchy that have a common base class and that they need to be instantiated at run time. Here the compiler does not know in advance what derived class objects to expect. These objects would be dynamically allocated and the code for handling these objects should be able to deal with all them.

It is perfectly legitimate to use base class pointers to point to these objects but that requires the compiler to handle them exactly the same way they would handle their base class objects. So they would call base class versions of member functions and none of the member functions specific for the derived class would be accessible.

To solve this problem
Virtual functions are used to allow dynamic binding.

"...It seems that our friend, the compiler of course, is very resourceful."


To support Polymorphism at runtime the compiler builds at compile time
virtual function tables ( vtables ). Each class with one or more virtual functions has a vtable that contains pointers to the appropriate virtual functions to be called for objects of that class. Each object of a class with virtual functions contains a pointer to the vtable for that class which is usually placed at the beginning of the object.

The compiler then generates code that will:

1. dereference the base class pointer to access the derived class object.
2. dereference its vtable pointer to access its class vtable.
3. add the appropriate offset to the vtable pointer to reach the desired function pointer.
4. dereference the function pointer to execute the appropriate function.

This allows dynamic binding as the call to a virtual function will be
routed at run time to the virtual function version appropriate for the class.

Impressive isn't it?

Well that made me try just for fun to write code that would do these steps instead of the compiler.

But as I did this another question evolved.

How does member functions get their "this" pointer ? ( pointer to the object the function is called for )

I know that the compiler should implicitly pass 'this' as an argument to the member function so that it can use it to access data of the object it is called for.

I used in my example a single virtual function that takes no arguments and returns void.
So at first I tried calling the destination virtual function with no arguments. The function was called already but the results showed it has used some false value for 'this' that pointed it somewhere other than the object and gave the wrong results.

So I tried calling the function and passing it the pointer to the object and it seemingly worked just fine.

Here's the code I tried...


#include <iostream>

using std::cout;
using std::endl;

class Parent {
public:
Parent( int = 0, int = 0 ); // default constructor
void setxy( int, int );
int getx() const { return x; }
int gety() const { return y; }
virtual void print();
private:
int x;
int y;
};

Parent::Parent( int a, int b )
{
setxy( a, b );
}

void Parent::setxy( int a, int b )
{
x = ( a >= 0 ? a : 0 );
y = ( b >= 0 ? b : 0 );
}

void Parent::print()
{
cout << " [ x: " << x << ", y: " << y << "] ";
}

class Child : public Parent {
public:
Child( int a = 0, int b = 0, int c = 0 , int d = 0 );
void setzt( int c, int d );
int getz() const { return z; }
int gett() const { return t; }
virtual void print();
private:
int z;
int t;
};

Child::Child( int a, int b, int c, int d )
: Parent( a, b )
{
setzt( c, d );
}

void Child::setzt( int c, int d )
{
z = ( c >= 0 ? c : 0 );
t = ( d >= 0 ? d : 0 );
}

void Child::print()
{
Parent::print();
cout << " [ z: " << z << ", t: " << t << "] ";
}

class GrandChild : public Child {
public:
GrandChild( int = 0, int = 0, int = 0, int = 0, int = 0);
void sete( int );
int gete() const { return e; }
virtual void print();
private:
int e;
};

GrandChild::GrandChild( int a, int b, int c, int d, int e )
: Child( a, b, c, d )
{
sete( e );
}

void GrandChild::sete( int num )
{
e = ( num >= 0 ? num : 0 );
}

void GrandChild::print()
{
Child::print();
cout << " [ e: " << e << " ]";
}

int main()
{
Parent parentObj( 7, 8 );
Child childObj( 56, 23, 6, 12 );
GrandChild grandchildObj( 4, 64, 34, 98, 39 );

// declare an array of pointers to Parent

Parent *parentPtr[ 3 ];

cout << "size of Parent = " << sizeof( Parent ) << " bytes\n";
cout << "size of Child = " << sizeof( Child ) << " bytes\n";
cout << "size of GrandChild = "
<< sizeof( GrandChild ) << " bytes\n";


parentPtr[ 0 ] = &parentObj; // direct assignment
parentPtr[ 1 ] = &childObj; // implicit casting
parentPtr[ 2 ] = &grandchildObj; // implicit casting

cout << "\nThe Derived objects accessed by"
" an array of pointers to Parent:\n\n";

for ( int i = 0; i < 3; i++ ) {
cout << "Object " << i + 1 << " : ";
cout << "\tvtable ptr (" << *( ( void ** ) parentPtr[ i ] ) << ")\n" ;
// vtable ptr at the beginning of the object

// initialize pointer to function

void (* funptr ) ( Parent * ) = NULL;

// assign to it pointer to function in vtable

funptr = *( *( ( void (*** ) ( Parent * ) ) parentPtr[ i ] ) );

cout << "\t\tpointer 1 in vtable is (" << ( void * ) funptr
<< ")\n\t\t( pointer to virtual function 1 'print()' )";

cout << "\n\n\t\tdata: ";

funptr( parentPtr[ i ] ); // call the 1st function in vtable
// and passing ( this ) to it
// without using parentPtr[ i ]->print();
cout << "\n" << endl;
}

return 0;
}


The output should look like this:


size of Parent = 12 bytes
size of Child = 20 bytes
size of GrandChild = 24 bytes

The Derived objects accessed by an array of pointers to Parent:

Object 1 : vtable ptr (0043FD90)
pointer 1 in vtable is (00401480)
( pointer to virtual function 1 'print()' )

data: [ x: 7, y: 8]

Object 2 : vtable ptr (0043FD80)
pointer 1 in vtable is (004015B8)
( pointer to virtual function 1 'print()' )

data: [ x: 56, y: 23] [ z: 6, t: 12]

Object 3 : vtable ptr (0043FD70)
pointer 1 in vtable is (004016E6)
( pointer to virtual function 1 'print()' )

data: [ x: 4, y: 64] [ z: 34, t: 98] [ e: 39 ]



In order to reach the function pointer to the desired function ( print() ) the parentPtr of the object which normally points to its beginning had to be casted to type pointer to pointer to pointer to function before it was dereferenced to give the vtabel pointer and then dereferenced again to give the first pointer to function in the vtable.

Polymorphism uses virtual functions in another interesting way. Virtual functions enables us to create special classes for which we never intend to instantiate any objects. These classes are called abstract classes and they only used to provide an appropriate base class that passes a common interface and/or implementation to their derived classes.

Abstract classes are not specific enough to define objects. Concrete classes on the other hand have the specifics needed to have a real object. To make a base class abstract it must have one or more pure virtual functions which are those having = 0 added at the end of its function prototype.

virtual void draw() const = 0;

These pure virtual functions should be all overridden in the derived classes for these to be concrete ones or else they would be abstract classes too.

Suppose we have a base class Hardware. We can never draw, print the production date or price unless we know the exact type of hardware we're talking about. So it looks that class Hardware could make a good example for an abstract base class.

Another example could be class Furniture and it might look something like this:


Class Furniture {
public:
...
virtual double getVolume() const = 0; // a pure virtual
function
virtual void draw() const = 0; // another one here
...
}

Here class Furniture definition contains only the interface and implementation to be inherited.
It even does not contain any data members.

That's it.
Hope you liked this article.

I will be happy to receive your comments.

Bullet Proof code using RegExp.

Regular expressions is a great way to find those hard to find strings.
Suppose you have a bunch of old code and you want to bullet proof it or you might be interested in auditing it and for starters you want to find those uninitialized variables using eclipse.

the pattern of uninitialized variable could be in the following format

(Var)(space or more)(Alphanumeric word)(Possible space or more)(semi-colon)


to search for (space or more) we use expression
(\s+)
\s means space
+ means one or more

to search for(Alphanumeric word) we use expression
(\w+)
\w means Alphanumeric character
+ means one or more

to search for (Possible space or more) we use expression
(\s*)
\s means space
* means zero or more

so the regular expression would be int(\s+\w+\s*); it will return all uninitialized ints.

It would be great if we collect those regular expressions and keep them in a library to be our arsenal towards bad code.

do u have more regular expressions to share?