Monday, December 24, 2012

Merry Christmas!

6d605d6a3345ad0edae720f424704a91

May the Christmas Cheer spread around the world and bring peace, health and wealth to everychristmas_pinguinone!

I just found this nice site to create fancy fonts!

Sunday, December 23, 2012

Software State of affairs - Maintaining OO

Last week, a friend at work was analysing code in Powerbuilder and question came up about function Overloading and we branched into Object Orientation in general. That question got me thinking. Though Object oriented programming has been around for a while, the concepts of OO are not completely understood or followed.

I posted about OO concepts here earlier. To do that I bought and read several books, sifted through the web a lot. There were so much confusion and conflicting ideas. Most of those books use obvious objects like animals, people etc in explaining OO concepts that what we learn in those is not enough to translate in to real life OO programming. I mentioned about some of these problems in my earlier post. More and more languages are adding OO concepts, yet people who are used to traditional (procedural) way of programming, struggle to adapt to it or even completely reject it. Often times, Syntax comes in the way of understanding. For e.g., Java borrowed so much from C, C++, that it's often considered to be just enhanced C, thus OO nature of it is completely ignored. And even if these developers switch to OO eventually, the objects they come up with are just repackaged C code, thus perpetuating the problem!

The application I currently support was written in Powerbuilder using a framework called EAF. The software also has business framework that is built around this, definitely a good extensible architecture. Yet, the software has degenerated over the years. Part of the problem is lack of understanding of the framework and/or OO concepts in general. I remember, when I started here, this framework was considered to be the main reason for all evil in the software! There was not enough documentation about proper framework usage, thus it was easy to blame it!

When new developers came in, they wrote and rewrote functions and objects that completely ignored underlying framework thus code is bloated. Further, supporting business has been utmost priority, so such technical issues were completely ignored. I feel, every maintenance project should include the maintenance of the software itself - in addressing technical issues and retuning it to keep it running good, like we do with our cars. After all, business will eventually be affected, if we didn't take care of such technical issues in time! The very first problem I solved in this software was a technical issue - there was a memory leak (due to improper handling of Object pooling) in the software that kept crashing the server during peak usage in the prior years. Business of collecting taxes, was indeed affected during those crashes. And I cannot imagine how much time, effort and resources were wasted during those times.

Another problem I see in software maintenance, is the tendency to solve code smells and code bloats by adding more resources - more servers, more disk space, more memory. When a car runs sluggish, we can't solve it by just adding more engines or more tires, can we? Why is it OK with software? Prior solution to the memory leak problem I mentioned above, was to add more servers and a script to automate the Server Restarts! Once the memory leak was fixed, no more frequent crashes!!!

To curb some of the issues in the application, I recommended and started on a project to re-factor some of the code in the application earlier this year. Main goal was to improve performance in our web application, but I wanted to clean up and reintroduce some of the framework methodologies.  Though there was some push back initially, other developers started buying into the idea. Yet, I see the reluctance to change existing ways; after all the software works during normal (low?) usage and business is OK (if not happy), why change those? When I first introduced log4J (logging libraries for Java) for logging in our JSP pages, it took the team at least couple of months to buy into the idea, in spite of me showing stats to support the change.

Our application architecture uses EA Server (EAS) as the middle tier. EAS supports Java and Powerbuilder objects naturally.While Powerbuilder is great for developing applications quickly, Java was used for any multi-threaded activities such as caching and logging etc. We even have Java Stored procedures running inside Oracle database! I recently introduced tools like Eclipse, Ant etc. to streamline Java development activities. With such exposure and addition of new Java packages prompted a team member to say to the effect "we are becoming a Java shop whether we like it or not". I've tried to explain in vain that it was all there before and why we even need them!!

Part of the problem is also lack of career planning and/or training. I remember the days when companies invested in training employees to keep up with the industry trends. Now, we seem to be spending more in buying those extra servers to support more memory leaks :)

Tuesday, December 11, 2012

Tools: Web Statistics Tools - Analog

We have a web site used by the tax payers to file their annual taxes. This is used by 100s of thousands of users within a short period during peak season (Feb - Mar) and reasonably used other times. During the peak times, it almost explodes.

I have been working on improving performance of this site. Also, there have been no stats whatsoever to be able to confidently say how many users were served successfully or when it failed where the bottlenecks are. We relied on information stored in the Database to "guess" these stats. I am adding a bunch of tools and (Java) programs to get more stats as well.

I am currently doing load testing on a test version of this site using Apache JMeter. While this tool is great and comes with a suite of reports, and I have added a tons of stats on the server side (an old Sybase EA Server) I needed something outside of these two. So, I started looking at the HTTP Access logs. EA Server includes a light weight Apache server at the core. So, the format of the log file is almost identical to a Apache Server Access logs. (On EA Server the file is called JaguarHttpRequest.log).

As usual, I looked around for tools that will help with analyzing these logs (when the computer can do the work, why do it manually?!). While searching for the tools, initially I looked for Apache log parser. I did find a few, but nothing great. (I found one PHP program though, which is kind of interesting from programming point of view). Eventually, I landed in Web Analytics Software and our good old Wiki (here) came to the rescue. I tried a few software listed there (Webalizer, AWStats) look interesting.

Finally, I tried Analog. Bingo!! This is what I have been looking for. Analog is a very simple tool to install and use. (Installation is just unzipping the zip file). Using the tool is as simple as Double clicking or running from command line, the analog.exe file.

This actually produces a (sample) report with the name Report.html. After reading more on Analog web site, I was able to customize the config file named analog.cfg. Once I tweaked this file, I started parsing the log files in minutes. I was even able to append date to the report file name and thus was able to run it in a loop to capture the stats every few minutes.



Apart from the Summary page, it also has several graphs to show stats for Hourly, Daily, Weekly, Monthly etc.

If you are looking to analyse usage patterns on your web site, this is a great tool. Check it out!

Friday, December 7, 2012

Quick Tip: How to exit from SQL*Plus on command line

This is about running SQL*Plus in a batch mode. Suppose you have a script (shell or batch file) that calls SQL*Plus to run a SQL in a batch mode. I am talking about running it from Command line, thus:

$ sqlplus <user_id/password>@SID @<sql_file_name)

Chances, you will want to exit SQL*Plus as soon as the script is done (EOF file is reached in SQL file), so we can continue our processing.

Typically, we add an EXIT statement at the end of the SQL file itself and this will force SQL*Plus to quit. What if you forgot or couldn't add EXIT (you use the same script in different scenarios). If you don't have an EXIT or QUIT statement at the end of your SQL file, you will end up seeing the SQL Prompt:

SQL>


Here is a quick tip to exit Sql*Plus after it's done with the script:

exit | sqlplus <user_id/password>@SID @<sql_file_name)

(That's it. Essentially piping exit into sqlplus command! When the End of file is reached, SQL*Plus returns to the shell and your shell script can go on!

This tip works on both DOS (Windows command prompt)_ and *nix systems.

Saturday, December 1, 2012

Gotcha: JSP includes

This is related to my earlier post. When we fixed the issue in Servlet Filter in the JSP Web Application we were troubleshooting it went past the first page. We landed on a blank page this time!! We were testing in a mirror site that I had created. According to the developer, the original site work and the new mirror site was having this issue.

To cut the long story short, this was caused by a change developer added to a JSP file to invalidate session. This file was included in the main JSP page that was erroring out. After some digging, I found out that the original JSP wasn't compiled in a while (See below). Bingo! I made it to recompile, and the original site also failed at the same place. This proved the invalidate() added to be culprit.

JSPs are just short hand notation for Java Servlet. It lets the developer/page designer to code the page using HTML like tags. The first time the user accesses the JSP file, the Servlet container automatically generates and recompiles a Java Servlet for the JSP file. After the first time, this step of generate/compile is skipped. In development we could make the Server to recompile each time the JSP file is changed. This works fine. The problem in our case was that an included file was changed and since the main JSP file itself did not change, the Server didn't bother to regenerate the Servlet class.

The lesson learned is not to believe what you see or not see on a JSP page. Always make sure your JSP is compiled up-to-date. To do this, you could use the compiler that comes with the Server (Servlet container) software. Most Servers (for e.g., Apache Tomcat, IBM Websphere, Sybase EA Server) come with a command line script called jspc. Another approach is to clear the your Server's work directory. See here for a discussion of this.

Gotcha: Java Concatenation

The Problem

Recently one of my co-workers had an issue with a website written in JSP. Every time he went to a page, a Servlet Filter was failing with an error that the Java class "StringBuilder" was not found. I've added this Servlet Filter in the application recently and this worked fine on other machines and in our Test Servers, but failed on this one machine. Also, we use Java 1.4 and there is no way we could be using Stringbuilder which was only introduced in Java 1.5.  (Java 1.4 has only StringBuffer. See here for a discussion on the two).

(I know we are using older technology - we use Sybase EA Server 5.5, which forces us to stick with Java 1.4, but go with me on this.)

Java Strings and Java Compiler

I set to find our why it was complaining about something we don't use. I looked at the class file generated. It had "StringBuilder". Then it hit me. We use a lot of String Concatenation in our code and we use String concatenation using plus sign (+). This seemingly innocuous usage caused it and the error has to do with Compiler Optimization.

Java Strings are immutable, meaning they cannot be changed at run time. Each time we concatenate Strings, we are essentially creating newer and newer Strings.

So, str1 + str2 + str3 is essentially equivalent to

res1 = str1 + str2

res2 = res1 + str3

But Compiler may take a different approach to optimize concatenation using StringBuffer or StringBuilder (See here). So the above, concatenation can be rewritten (by compiler) as,

StringBuilder(str1).append(str2).append(str3);

So this is what happened in our case. Some concatenation got turned into StringBuilder by the Compiler and hence the error. Wasn't that supposed to read StringBuffer (because it's supposed to be Java 1.4)? Well that was because of another mistake. When he set up Eclipse our friend used a newer version than the one I recommended which defaulted to Java 1.6 compiler which caused the optimizer to produce > 1.5 code!! Hence the error on StringBuilder. But the error showed up in the first place because of the Compiler Optimization! If the compiler used the String.Concatenate in the first place, we would not have seen this error!!.

And Compiler may sometimes choose to do just that, use String.Concatenate as shown in preciseJava.com. Also, such Compiler optimization may not be always good. See this interesting blog about various possibilities of concatenations.

Oh yeah, We overrode the workspace settings in Eclipse to use 1.4 compiler for the project and all is well now.

Reference Links

  1. http://chaoticjava.com/posts/stringbuilder-vs-string/

  2. http://javamoods.blogspot.com/2010/02/optimization-dont-do-it-compiler-will.html

  3. http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CF4QFjAE&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.122.5641%26rep%3Drep1%26type%3Dpdf&ei=LIK6UMCwBMWUjAK4_4HIBw&usg=AFQjCNFo6trLwU4wlxgrTNFBa1tGb1p9Cg&sig2=qHQIg8S0qI-CXdJ6EOaa8w

  4. http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

  5. http://code-thrill.blogspot.com/2012/08/stringbuilder-optimizations-demystified.html

Java: What's up with "Impl" in Class Names?

What to name it?

This is a constant question we are faced with in daily life. We want to come up with perfect names for our babies. We want to name our animals (pets) and they seem to be happy to have a name! And it seems to be a big thing! Otherwise we wouldn't have naming ceremonies like these. We try to name (rename) our buildings (otherwise what would  you call Empire State Building?), streets, cities and countries. (Myanmar or Mumbai is yesterday's story. Mexico wants to rename themselves as Mexico! See here.) And naming is not an easy thing to do. A world recognized music composer in India, Illayaraja, called his symphonic music album "How to Name it" for lack of a better name.

What's that got to do with this?

Java developers (or any developer for that matter) are merely imitating the real world and they too have to face such challenges while naming their programs, classes and objects. We don't want to name (can't) two entities same name. That would cause confusion (Name Collision). To avoid this, we use packages, name spaces, domain etc.

Each "object" tends to have 2 forms in Java world (or .Net, C++ etc) - the class and it's interface! An interface is a class's "public profile" - much like public profile we have in linked-in, what we want other classes to know about this class.

We could say the interface is not unlike a business card or a cover letter we send with our resume when we apply for a job. Cover letter contains exact amount of information that may be of interest to the other party while the resume may have lot more inner details. Now they both are about you, but not the same.

So, what's up with "Impl"?

So, if you are a developer, what would you call the interface and implementation for an object that models a real car?

[caption id="" align="alignright" width="356"]OO interface names OO interface names[/caption]

This is not as simple as it seems. There are at least 2 camps. One camp, the Microsoft based camp calls the (implementation) classes by just the name and prefixes their interfaces with "I". To them the Implementation is the real object and interface is it's business card. The other camp (seemingly started by IBM) uses the plain name for interfaces and the (implementation) classes are named with a suffix "Impl" for Implementation. Here the interface represents the abstracted real word object and the Implementation class merely has extra (inner) details that doesn't concern outsiders (other classes). The Java world mostly follows the "Impl" convention.

So there you have it, when you see Java classes named with "Impl" at the end, you know why. End of story, right? No, not really. Object Oriented pundits and purists don't agree. Here is a nice post where the author diisagrees with "Impl" usage. And here is another perspective to it. And it's an ongoing debate. Don't believe me? See here, here, here.

So which way to go?

Naming your class can be a personal choice, but most of us write programs that will be shared with others in the community. That's why conventions and best practices exist. If you are working on a project, set up and follow standards appropriate to your shop.

In some cases, you may not be able to change the "I" or "Impl" in names. Many programs may have some type of interface (protocol) like CORBA, COM, Web Services. In these cases, they generate code (Stubs, Skeletons etc) that you have to based your own code on. Document these and stick to those conventions to avoid any surprises.

Personally, I am OK with using Impl suffix. I work with EAServer which runs both Powerbuilder and Java objects. These objects "talk" to each other using CORBA IIOP. Both Powerbuilder and EAServer tools generate CORBA IDL files which in turn are used to generate stubs and skeletons in Java (or C++). Developers then write Implementation classes for the interfaces generated. The class files generated and the impl classes may end up in the same Classpath and to avoid Name Collision, we have to name implementation classes differently and the convention is to use Impl suffix. This is a convention I am sticking with in my current project.

Reference Links

A note about my "cheesy" cartoon there. While searching for something relevant I found Toondoo where you can make your own cartoon. You can start with a free account. I also use openclipart for some of the pictures in my other posts.

http://www.toondoo.com

http://openclipart.org

http://isagoksu.com/2009/development/java/naming-the-java-implementation-classes/

http://docs.oracle.com/cd/E13183_01/en/alui/devdoc/docs6x/aluidevguide/ref_idk_deploymentimplkeys.html

http://leshazlewood.com/2009/03/03/java-class-naming-conventions/

http://www.gnu.org/software/libc/manual/html_node/Interface-Naming.html

http://osherove.com/blog/2006/6/14/interface-naming-anything-but-javas-standard-please.html

Thursday, November 22, 2012

Now you know: Serifs and Fonts

Have you ever felt like you always knew something and turns out you didn't? Paul Harvey used to host a show called "Rest of the story" on the radio. He would take a familiar subject and give you a little known fact or story about the subject. If Paul was alive and decided to run such a show on technology, he would never run out of ideas for the show. There are several terminologies we take for granted on a daily basis. Some of them are real simple and others more complex. I will share my list of such stories here. If you know of any, please feel free to add them as comments below.

I've always taken HTML and CSS for granted. So, I decided to learn it in a proper way. Who better to learn than w3schools (W3 stands for WWW)? While I was going through their chapters, ended up on a topic about Fonts,  that caught my attention and this is the basis of my post today.

I use various fonts, like Arial, Times New Roman, MS-Sans-Serif etc. Didn't pay much attention to the names or how they looked. These Fonts vary in size and shape. Some of them are basic fonts and there are fancy fonts. And of course, there are various other characteristics of a font that make them look different from each other. One such Characteristic is a "Serif". According to Merriam-Webster Dictionary, Serif is actually a word (noun) that means the little edgers added to letters when we write them. Where as, San-Serif would mean the opposite, those fonts without Serif's!! (I don't know if they teach this in English classes in schools, but this is the first time I came across this).

font-typesThough the word is adopted for the computer fonts, the idea of a serif is not new. Apparently, like any western concept, it has origins in Latin and Roman scripts. But it is not unique to the west either. Chinese and Japanse characters have something similar too. See the wikipedia post for more interesting facts.


So next time, you pick a font, enlarge it and see if it has "serifs". As shown Times New Roman font does and Arial doesn't.


Apart from these two types, we also have Monospace fonts that have constant width for all characters. Courier New is of this type and is typically used by text editor and program (code) editors. "Courier New" uses Serifs to adjust the widths to be uniform. Check it out.


Now you know (the rest of the story)!


References:

http://www.w3schools.com/css/css_font.asp
http://en.wikipedia.org/wiki/Serif
http://www.merriam-webster.com/dictionary/serif
http://www.merriam-webster.com/dictionary/sans

Sunday, November 18, 2012

Powerbuilder and SAP

As I mentioned in my earlier post Sybase's merger with SAP seems to be complete. Since SAP took over, there was some uncertainty in the developer community about their interest in keeping the tool alive. From various posts online so far, SAP seems to committed to developing PB further.

If you haven't already noticed, Powerbuilder (PB) is definitely making a come back (among those who left it in the late 90's and the new developers trying to explore PB). I see lot more interest in the tool and many more blogs like this are coming up on the subject. This is almost like before Sybase took over in late 90's. While Sybase kept the product alive, there was a dormant period where the product lost community following. With SAP in the picture, there seems to be renewed interest in PB. They are constantly adding resources related to PB on their site.

http://www.youtube.com/user/saptechnology/videos?query=PowerBuilder
(Courtesy: Bruce Armstrong)

If you are new to PB, these videos may be helpful. I will be posting more links here later.

Thursday, November 15, 2012

Gotcha - JMeter: Response Data Display

This Gotcha is about the Load Testing tool called JMeter. JMeter is a real nice open source tool from Apache to load test Web Apps. You can set it up as a proxy and capture web pages and then customize the scripts in the GUI to build powerful Load testing scripts. I will be posting more about JMeter in separate post(s) later.

For those who are familiar with JMeter, this tip may be helpful. When you look at the results in a "View Results Tree" (See image below), the Response Data typically comes out as Text (HTML source). I was looking at these text HTML for a bit, baffled what to make out of it. I recently (accidentally) found out that you can change that. There is a drop-down below the tree (list) of pages visited. This is normally defaulted to "TEXT". If you change it to "HTML" or "HTML (download resources)", then bingo! the HTML page is displayed. "HTML(download resources)" downloads images etc, so the page looks much more like the original web page. There are other options like XML etc.

This choice (drop-down) is kind of away from the response view, so I didn't pay attention to it. Moral of the story is to click on every buttons and drop-downs on a GUI!! (or read the documentation fully!)

[caption id="attachment_616" align="alignright" width="1024"] JMeter Response View Gotcha[/caption]

PB developer Center on SAP site

Sybase merger with SAP seems to be complete now. If you didn't already hear the news, John Chen, Sybase CEO, has already stepped down. So, expect more activities to move to the SAP side of things. So far, SAP seems committed to PB.

As part of the migration, they are integrating Sybase news groups into SAP Developer Community Center. Below is the link for PB.
http://scn.sap.com/community/developer-center/powerbuilder

forums on EA Server continues to be on Sybase site through this month and later will be merged into SAP forums as well. See here for more details.

Sunday, November 4, 2012

Quick Tip - Oracle on the net (APEX)

While trying to come up with sample SQLs for the previous post, I wanted to test them before posting them. I googled for tips online like always. I found Oracle Application Express. All you have to do is enter a name for the Workspace you would like and an e-mail address. Then they send you an e-mail to validate. When you click on it, they ask you some more details and a reason why you should be given access. Once I completed this, I got another e-mail with temporary password. When I logged in the first time, I had to change the password. After this, it asks me for database name and that's it. I was able to try a few SQLs quickly!

[caption id="attachment_596" align="aligncenter" width="474"] Oracle Application Express[/caption]

So far, I've only tried SQL Workshop menu. This has a nice SQL interface like SQL*Plus, only simpler. And it's free!! I am impressed. Apparently, Application Express (or APEX as it is known) is much bigger than what I saw. See this link for more on this nice tool.

Thursday, November 1, 2012

Quicktip - MS Word: How to Paste/format code?

Have you ever tried to paste code into a word document? Ugly! It loses formatting, wraps code around, loses syntax coloring etc. Today, I tried to document my sample Java/JSP code in word. I wanted to intersperse code with additional comments explaining the code for other developers, the way some web pages do it. I tried pasting code into word, it lost formatting and also syntax coloring. I searched good old Google and landed on this page in Stackoverflow (SOF). Out of the several tips, the one using NotePad++ (Npp) caught my attention.

Export to HTML using Npp
I use Npp both at work and home and it's a great tool. Why not use it? I had an older version. Downloaded and installed version 6.2. They have added few more plugins and options. Now, I can colorize JSP code (though I don't like their choice of colors for HTML elements). Per tip in SOF, I tried Npp Export plugin to copy RTF to Clipboard option. This didn't work OK (colors ran in word). I tried the other options. Of all, the "Export to HTML" option worked out the best for me. Colors softened in HTML. Then I just opened the HTML file in Word, saved as word and got down to business with documenting the code. To add flavor, I added a Style called "Code" in Word to customize further. I added "box" to this style and the results are shown below.

Adding Line Numbers
NppExport does not add line #s by default (There is a "feature request" opened for this issue!). I poked around the web for line #s in Npp and found this tip. Essentially, use TextFx Tools to Insert line #s into the file in Npp. Then do the Export like above. Bingo! I've got formatted/syntax highlighted code with line #s pasted into word document. And one more kudos to Npp for job well done!

Fig 1 - Sample code snippet formatted in Word with description around it

Saturday, October 20, 2012

Gotcha - Oracle: Null

NULL essentially points to an "undefined" value in the database. It helps to conserve space in database columns. It's useful when we want to store "nothing" in a column. NULL is defined in ANSI, so all databases including ORACLE implement them. But, ORACLE differs from ANSI standards in dealing with NULLs. Some of these could be real gotchas for developers coming from other databases.

Gotcha 1 - Empty String is the same as NULL in Oracle SQL

In case you didn't know, empty String (also known as null string)('') and NULL are considered the same in Oracle. This is not ANSI standard and is a real gotcha for developers coming from other databases, where they are different.

Here is a SQL I found in one of the programs I am supporting:
SELECT * FROM employee WHERE NOT((employee.dept_nbr IS NULL) AND (employee.dept_nbr = '') );

In Oracle, the above Where condition is *not* really checking for 2 different things. Seems to me, the developer wanted to make sure it was not empty and it was not null either. This is because, he/she didn't realize Null String is essentially same as NULL in Oracle.

When in doubt, I always try a simpler query than original. Oracle system tables and the built in table DUAL come in handy for those queries. Here is a sample query that shows empty string is Null.
SELECT 'empty string is null' FROM dual WHERE '' IS NULL;

Another Gotcha - a bug in the SQL above


The above example had another bug. if the NULL was not the same as '', the above query would have failed completely!! (How can same field be NULL AND something else (here null string ('')) at the same time?). The above condition must have been
SELECT * from employee WHERE NOT((employee.dept_nbr IS NULL) OR (employee.dept_nbr = '') );

--
OR even below SQL:

SELECT * from employee WHERE (NOT(employee.dept_nbr IS NULL) AND NOT(employee.dept_nbr = '') );

Gotcha 2 - Checking for NULL

Did you notice "IS NULL" there? When you check for a value being NULL, you cannot use equal to (=). You have to use IS or IS NOT. This is yet another gotcha for many new developers to Oracle. Strange thing is Oracle will not complain if you entered '' = NULL in the above SQL. It simply won't find any rows!! Who better to explain this than Ask Tom?









[caption id="attachment_585" align="alignleft" width="329"] Oracle Select "IS NULL"[/caption]


[caption id="attachment_587" align="aligncenter" width="412"] Select equal to NULL results in "no rows found"[/caption]

If you are coming other databases, this may be a surprise to you (SQL Server for e.g., allowed = NULL check). It's a real gotcha in Oracle, because Oracle won't complain if you said "= NULL", but the query may not work as expected. A query may return less number of rows than expected, because of NULLs in some fields in the where condition. For e.g., below SQL will return only 2 rows (I expected the row with NULL to show as well).
SELECT emp_nbr, dept_nbr, first_name || ' ' || last_name || '(' || dept_nbr || ')' AS employee
FROM employee WHERE employee.dept_nbr IN (NULL, 100. 200);

The above WHERE condition must be written as,
WHERE employee.dept_nbr IS NULL OR employee.dept_nbr IN (100, 200);

Another way of doing this is to use the NVL function in Oracle. NVL translates NULL value to the value passed in.
WHERE NVL(employee.dept_nbr, 0) IN (0, 100, 200);

Here NULL is translated to 0 and this we can check in equality or IN condition.

Gotcha-3 Concatenating NULL (Empty String)


When you concatenate text fields together, if any of the value is NULL, the result will *NOT* be NULL in Oracle. In other databases (for e.g., SQL Server, mysql), String + NULL results in NULL. In Oracle, only NULL + NULL results in a NULL.
SELECT emp_nbr, dept_nbr, first_name || ' ' || last_name || '(' || dept_nbr || ')' AS employee
FROM employee WHERE (employee.dept_nbr IS NULL);

In this case, if you expected NULL, you are in for surprise. The concatenation actually works. Only the NULL values will be missing. (For e.g., this will return Sam Varadar() based on the sample data in the screenshots above). Though, Oracle is not promising this in the future versions. See here.

Gotcha-4 Nulls and Indexes


Another hidden gotcha in this is that when we a query uses an index, we are implicitly applying Equality checks. If a column that is in an index has NULL values in some rows, then those rows will not be indexed and thus won't be picked up!

This also means a primary key column cannot have NULL values. This is because a primary key column value identifies each row uniquely and thus they have to be in the primary index on the table.

Other NULLs


We saw above, concatenation of strings works even if one of the values is NULL. This is because, a NULL is also considered an Empty String. Other operations may not be as kind:

For e.g., Adding NULL to a Number will result in NULL.
SELECT 1 + null FROM dual;

In PL/SQL NULL is a NULL statement - a statement that does nothing. It is often needed in control blocks, like if-then-else where we want to leave the if or else part empty, but PL/SQL syntax doesn't allow this.
IF (dept_nbr = 100) THEN
dbms_output.put_line('Inside IF block');
ELSE
NULL; -- Do nothing
END IF;

When sorting a query result using ORDER BY, you can sort rows with NULLs to come FIRST or LAST (default).
SELECT * FROM employee ORDER BY dept_nbr DESC NULLS FIRST;

Functions to deal with NULLs

Because of the unpredictable  behavior of queries around NULL values, it's often better to translate NULL into something meaningful, so equality (or inequality) tests can be done without worrying about NULLs.

We showed the use of NVL above. It translates NULL into a more meaningful value, 0. NVL2 is a similar function that returns one value for NULL and another for Non-NULL values. COALESCE is another function that could be passed in a series of comma separated values and it will return the first non-NULL value in the list.

These functions can also be used while creating index on a column that may have NULLs and thus help with performance also. See here for more on this.

Other functions include NULLIF, DECODE etc. See here for a nice discussion of the functions related to NULL.

References

  1. http://psoug.org/reference/null.html

  2. http://stackoverflow.com/questions/203493/why-does-oracle-9i-treat-an-empty-string-as-null

  3. http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:17320984423926

  4. http://www.sqlines.com/oracle/string_concat

  5. http://www.oracle-base.com/articles/misc/null-related-functions.php

  6. http://thinkoracle.blogspot.com/2005/06/nulls-in-oracle.html

Quick Tip - EA Server: Reset Jagadmin password

This is a really quick tip on how to reset password for the administrator id "jagadmin" used in EA Server. We use this id for everything from running it in Development to production environment, only difference being in DEV we don't have a password for the id, to make it easier to move from machine to machine.

By default there is no password for the jagadmin id. The problem is, once you set a password, you cannot reset the password directly. The tools Jaguar manager (GUI) and the Jagtool (command line version) don't allow you to reset password "for security reasons". After I googled a bit, I found a way.

Surprisingly, all you have to do, to reset the password is to remove the line that has the below setting in the file called jaguar.props (this is the properties of the server itself, named jaguar).
com.sybase.jaguar.server.jagadminpassword=<password>

Where the <password> itself is stored encrypted.

This tip is only about resetting the password. So, I won't go into any further detail here. But this opens up the bigger topic about the default security model in Sybase EA Server, which  seems very weak. The administrator id, jagadmin is the default id and is needed if you want to do anything serious with EA Server. But, we manage to make it secure enough.More on this later.

If you are new to EA Server (also Jaguar), several of the terms will be new to you.  I intend to write  more on EA Server in future post. Please check back again later.

Quick Tip - *nix: Back Quote

While researching on an issue with a Java Stored procedure at work recently, I had to go to oracle udump directory to find out if there was any errors. To do this, we do grep like,
grep -i -l "nullpointer" *

to list the names of all log files that may contain NullPointer exceptions. -i = ignore case, -l = list only file names.

This tip is to be able to use that list in another command using back-quote (`). This is the character (also called grave accent) before 1 on PC keyboards, if you haven't used it. Back-quote is used for command substitution in *nix (unix, linux) systems. It's available in

For eg, to list those files with full directory listing:
ls -lt `grep -i -l "nullpointer" *`

Or to open the trace files found by grep, all at once in vi:
vi `grep -i -l "nullpointer" *`

(and then use :n in vi to go through the files)

Below line copies the found files out to another directory,
cp `grep -i -l "nullpointer" *` ~/nullsod

(where nullsod is a directory in my home directory.)

Command Substitution (using Back quote)

This process of executing commands inside back-quote is called command substitution. The command inside the back-quote is executed first and the output is plugged into the commandline that contains it. For e.g.,
echo `date`

returns System Date. Notice a back-quoted command is mostly embedded inside another. If you miss the echo there, it will try to return the date string to the shell which it will try to execute as a command which results in "Command not found" error.

Back-quote is often used in scripts to set the result into a variable. For e.g., we can save the date value in a variable called dt. Then we can use this variable in script.
$> export dt=`date +"%m%d%C%y%H%M%S"`
$> echo "Date is $dt"
$> cp output.log output.log."$dt"


#Where, $> is the shell prompt

In the above list, Date is executed with a specific format (mmddyyyhhmiss). This is then stored in a variable called dt. First we echo dt. Then we use dt to rename a file with date suffix, very handy when you are running a script on a regular basis and want to save the output in timestamped logs.

Incidentally, the statements above, show another kind of substitution, a string or variable substitution using double quotes("). Any expression inside a double quote is often evaluated first and substituted in the command that uses it. Notice the difference: double quote simply evaluates a string/variable and plugs in the value. A back-quote on the other hand, actually executes the string inside as a command and returns the  output to the enclosing command.

You can combine many of these expressions, and that's what makes it all powerful. If you haven't tried back-quote yet, try it. You will be glad you did!

Useful links

  1. Command Substitution in Unix

  2. Unix Shell Substitutions

  3. Advanced Bash Scripting






† udump - user dump - contains user trace files in Oracle database; all print statements from stored programs in Oracle (Java, PL/SQL) end up here.
‡ udump directory may have a large # of files depending on activity, in which case, grep may not work with wild card (*). You may have to refine the grep parms.

Quick Tip - *nix: ulimit

Last week one of our DBAs called me. She was trying to install Oracle 11g. She obtained zip files for the installation from another co-worker. This had many zip files, probably one per installation disk. She was trying to unzip the files and got some error.

Like always, I got down to business of checking and googling. When I tried to unzip the first file, it gave an error as to missing zip file headers. It suggested that the file could be part of a multi-part zip file. Good old google suggested how to combine a multi-part zip file. Just cat each file into one big file, then unzip it:
cat file1.zip file1.zip file3.zip > fullfile.zip

Unzip failed on the fullfile.zip. When I checked the file size, it had he same size as the 1st file, the largest file. Strangely, they both had the size of about 1GB. More googling revealed role of ulimit!

Resource limits on *NIX systems (ulimit)

All *nix systems (all flavors of Unix and Linux) have a limits on system resources per user. The limits include cpu time per process, number of processes, largest file size, memory size, number of file handles etc (file size is defined in number of blocks.). The root user gets unlimited resources, but all others are assigned a finite settings as defined in /etc/security/limits on AIX. (/etc/security/limits.conf in Ubuntu). Here is a good post, as to why you want to limit resources for users. To check the limits, use ulimit command.
ulimit -a

There are Hard limits (ulimit -Ha) set by the root and Soft limits (ulimit -Sa) a user can set herself.  See this blog for details on using ulimit.

Role of ulimit in our problem

Continuing our story about faile Oracle install above, when we did ulimit -a, one particular entry caught our attention. file size was set to 1GB (approx., remember it's set in blocks?). Bingo, this is why when we try to create the fullfile.zip, it got truncated at 1GB, the limit for the user. Apparently, the DBAs are supposed to have unlimited file size. She got onto calling the Unix admin.

To cut the story short, apparently ulimit played a role in getting the original zip files as well. When she downloaded the files, it truncated the biggest of files to 1GB each. Our first file happened to be one and that's why it looked incomplete. To make matters worse unzip and gzip ended in pointing to a multi-part zip file, which they weren't.

Like I said, this is true for all *nix systems. This is very powerful, in controlling runaway processes sucking system resources away. But there may be a legitimate situation like above, where you need to override the settings. So, next time your file seem corrupted or truncated, remember to check ulimit.

References

  1. ulimit in IBM AIX

  2. ulimit on HP-UX

  3. ulimit on Linux - blog

  4. ulimit on Unix - blog






Depending on the *nix you are using, this may be variously mentioned as limits per user, per shell and it's descendants. Some of the settings like cpu time is applied to individual processes user/shell is running.

Sunday, September 16, 2012

EA Server

I've been posting about Powerbuilder a lot lately. The PB application, I work with currently is actually an n-tier application written in PB, Java and JSP. The application is built around a middle tier from Sybase called Enterprise Application (EA) Server. It's also called Jaguar (which is actually a part of the EA Server). A quick search for EA Server in google brings up Electronics Arts page, a gaming site. Search on Jaguar brings up all kinds of other things. So, there is not a lot of help on EA Server, though Sybase's own site has a wealth of information on the product. In my current project, we rely on EA Server for all our business rules and multi-tier application support. I am learning about the product in an effort to improve our architecture and thus our application performance. I will my share my experiences here, so it can benefit others trying to learn or use the product.

EA Server is an Application Server like Oracle Weblogic, IBM Websphere, JBoss etc. It is a Java container like all others mentioned. EA Server is a full blown Java server, so it supports Servlet, JSP, EJB and JMS. Apart from that, it has a speciality of being able to run PB objects as well. With support for CORBA (IIOP) and COM (on windows machines) components, it enables PB developers to develop application modules in PB, Java, C++ that can talk to each other through CORBA. For e.g., we have a site built using JSP pages that "talk" to PB objects. Recently, I developed a wrapper in Java for PB classes, so our Coldfusion applications can use PB objects as well. With this setup, we are able to implement an n-tier application in various languages.

While researching the issues and figuring out new methods to improve our Server performance, I realized how powerful the server is. I currently work with EAS 5.5. The latest version is 6.4. In 6.x, Sybase rearchitected the product completely to be more Java centric. To be able to do that, they included a Jetty Java container and wrapped each PB object as a J2EE object, so everything runs as Java objects. I haven't tried this version yet, but it sounds interesting. Unfortunately, from what I heard, Sybase (SAP) is thinking of getting rid of this completely in a few years and re-architect PB support into SAP's Netweaver server. So, really the knowledge about EA Server may disappear in a few years. (This is exactly what happened to Forte! Is it my luck or what?).

Irrespective of the fate of the EA Server, I believe the techniques learned in this exercise will be useful with other servers as well. I am already, trying our web app in Apache Tomcat which can then connect to our PB objects running in EA Server. I will share these experiences here as well.  So, if you are interested in EA Server or writing n-tier in PB or even how to make PB talk to Java components usingCORBA, please check back in.

Quick Tip - Oracle SQL*Plus: HTML output

Recently, I posted about SQL*Plus, a database querying tool from Oracle. Like I mentioned there, the tool is rudimentary and has some basic file handling capabilities. It has a command, SPOOL, to write the console output to a file. Traditionally, you only spooled the output to a text file. Some releases ago (I believe in 8i), Oracle included option to spool the output in HTML format. Useful when you are dumping data from tables (Have you tried dumping table data in plain text file format??) and for generating simple reports.

To generate HTML output from SQL*Plus, all you have to do is add markup option either on command line or inside the script. By the nature of it, it is only useful when you are spooling the output. Just add below to the script before spooling: and the output will be spooled to the file mentioned  in HTML format.
SET MARKUP HTM ON SPOOL ON PREFORMAT OFF ENTMAP ON -
HEAD "<TITLE>My TABLE Listing
</TITLE> -
<STYLE type='text/css'> -<!-- BODY {background: #FFFFFF} --> -
</STYLE>" -
BODY "TEXT='#000FF'" -
TABLE "WIDTH='90%' BORDER='5'"


...
spool dump_table.html


...

SPOOL off

That's it. Just be aware that the output file will be larger than the usual text file spool. By the way, the hyphen "-" at the end of each line is the line continuation character in SQL*Plus. So, it's really one line command with several options.

Alternatively, you can specify the HTML option on the commandline as shown below:
sqlplus -S -M "HTML ON" <user>/<password>@<SID> @dump_table.sql > dump_table.html

The option -M "HTML ON" is equivalent to MARKUP HTML ON in the script above. The option, -S is for silent, which turns off spurious output from SQL*Plus.

 

See here for more about creating HTML reports using SQL*Plus.

Monday, September 3, 2012

My Journey with Ubuntu Contd - Switching Desktop

Last time, I posted about upgrading to Ubuntu 12.04, Precise Pangolin. It has a user interface (desktop) called Unity. It looked different from Gnome desktop I got used to in ver 10.x. It was an adjustment, but it was easy to get used to. One problem I had though was that it kept freezing up at random. I believe this is a bug in the interface, judging by the bug reports opened for the issue.

Installing a new Desktop


While researching the issue, I found out that I could replace my desktop interface easily with Gnome desktop I used to like!! This blew my mind!! What? Replacing desktop? You mean re-installing complete OS, right? At least, that's what I am used in the MS-Windows environment. No, not with Linux. Here is a link I used in getting the new desktop installed. This put Gnome right beside Unity. All I had to was switch to Gnome when I logged in (See Fig. 1). So far, it's working fine. Apart from this, my applications and settings are still there. It connected to network and printer right away, so I didn't miss anything. (Note: While logging in I had to use Gnome (No effects), to get the same desktop look I had in 10.x.).

[caption id="attachment_455" align="alignnone" width="274"]Gnome in Pangolin Fig 1. Gnome in Pangolin[/caption]

(Note: to get to this list, click on the ubuntu icon while on the login screen. This is like pressing F8 to get to Safe/Normal mode in MS Windows).

Coming from MS Windows environment, I didn't think this was possible*. Not that I am a Micorsoft basher or anything. I've worked on MS Windows, most of my working career. I believe, MS-Windows is part of the evolution process a lot of us went through in the Software industry. Even so, I couldn't avoid being disappointed with MS-Windows. First of all, there is no such option with MS-Windows. With windows, if you wanted a different desktop, you would have to reinstall different version of OS. Remember Vista? When it came out it was buggy. Microsoft  offered that you could "downgrade" to XP one time. But then, you lost the shiny new OS called Vista. On top of that, when I switch to a new OS like that, I am sure to face an issue or 2 to get it to work with the set-up I have. No, this was not supposed to be possible.

(Note *: There were some custom desktops available from 3rd party, but nothing fitted in smoothly, as Gnome did here! Further, they typically ran on top of the existing windows desktop, so they were slower).

Secret lies in Linux Architecture


So, I researched this a bit more. Apparently, Ubuntu (actually any linux and Unix)  OS is built in a modular way. One layer can be easily replaced without disturbing another. As you know, underneath it all, there is kernel, the core shell in the OS. Then there is command line interface which is what we were used to, if you were using Unix. Add to that, a windowing layer, you got a Desktop. So far, this is the same way in Windows. (At least it used to be through Windows 98). But there ends the similarity. Fig 1, shows the building blocks of a Linux OS.

[caption id="" align="alignnone" width="499"]Linux Archirecture Linux Architecture - Courtesy Paul Cooper (pgc) on Flikr[/caption]

So, when I installed Gnome, I basically added to the desktop layer at the very top. The underlying layers (including Window Manager and X-Windows) were unaffected. This is why, my set-up was unaffected. Wow, truly modular programming! Similarly, I could actually replace a hardware and Linux would simply find appropriate drivers (or you can install it) and everything is working again! I couldn't help compare this to MS-Windows, if I changed hardware (even as simple as moving the hard drive with the OS to a new machine), it will be a nightmare to get it working again. Kudos to Linux community.

I really hope they fix the bug(s) in Unity, so I can get back to it in the future. But, I am not exactly missing anything here. Because, Gnome is actively developed in the community. Further, my desktop is definitely running faster than with Unity. (Another complaint about Unity on the web).

P.S: If you are wondering how I got the screen capture of the logon page, that's another strength of Linux (Again, you couldn't do this in windows). There is a whole slew of things to read up on X-windows. I will post more on this later. For now, refer to the link in the references below.

References



  1. Gnome Desktop

  2. Unity Desktop

  3. Ubuntu

  4. How to add Gnome Desktop to Ubuntu 12.04

  5. Another site about installing Gnome Desktop

  6. How to get screen capture of the Login page


Now I can proudly say, my PC is

Learning Opportunities online

In Computer Science, we constantly face the challenge of "keeping up" with technology. If you catch up with one, chances your skills are outdated within a few months or years. Part of the problem has been lack of affordable learning opportunities, just to keep up with trends in the field. Of course, there are courses available in Community colleges and local universities, for continuing education. They were challenging in terms of logistics and time. What if you can learn at your own pace and they are affordable and of good quality? Wait no longer!

Did I say affordable? Read Free! Recently I found that a lot of universities are offering free courses online. These are real courses that are offered in classes in those universities, with home works, programming assignments and even exams. The courses are literally free, including study materials online. Some courses even offer some kind of Certificate. They do not offer any course credits though. The universities include reputed institutions like Harvard, MIT, Princeton, Stanford, UC Berkeley. You can read more about this in this NYTimes article.

For more information on the courses offered, check out the links below.

This blogger has listed a few. MIT has a Open Courseware that offers lectures from their courses online.

I am currently taking a course at Coursera and it has about 60,000 students enrolled from all over the world! It's definitely opening up new avenues in bringing technology to those remote areas of the world.Personally, if not anything else, I am using it to keep in touch with the trends in Computer Science. This is also a great opportunity to get your hands on newer technology, if you are stuck with old technologies at work!There is one course coming up at edx,org, offered by UC Berkely, called SAAS - Software as a Service. In this course, one can pick up some cloud programming and hands on experience in Ruby on Rails.

If you are a self learner, there are a lot options throughout the web. Youtube has been a great resource for several topics. Some great minds have put together videos explaining complex topics like Algorithms to mathematics modelling. There is also wikibooks and even a wiki university that have great resources for a self learner. But if you need structure and discipline of a classroom to learn, above sites will help.

So, if you want to brush up on those data structures before the next interview, pick up one of those links and have fun learning!

Sunday, September 2, 2012

To OO or not to OO

I originally wanted to name this post "Object Oriented Concepts - Contd....", but then the Hamletian dilemma came to mind. Every programmer faces this, even today, many years after OO has been widely accepted. Part of the problem is that the OO paradigm requires a change in how we think about a problem.

As we saw earlier, in traditional languages we always think "how to do a task" in a procedural sense. This we could adapt to - after all, we humans are good in giving instructions and making recipes and grocery lists. Programming was a natural extension of this - we told the computer what to do and how to do it. We told the computer what to do while feeding data (nouns) required as parameters to these commands (verbs).

When it comes to OO, we have to start thinking what will be the objects we need and "leave" it to those objects to do the work. Really, it's just repackaging, but it's not easy. We are now forced to think of everything in terms of objects. If it's objects like animals, cars, people etc that are typically used in examples about OO, we can understand. But the moment we try to come up with objects for abstract concepts and purely action oriented tasks, it becomes a daunting task.

Baking a Cake

For e.g., let's look at a simple problem of baking a cake (no disrespect to the chef in the kitchen, but let's abstract it here as simple task here for a minute). A recipe typically talks about how to get, do, heat, bake etc. All verbs. Interspersed will be details about data to be used - how much flour, how much sugar, what temperature, how many minutes etc. These "nouns" are used as parameters to the recipe and off we go. Of course, every now and then people try to "reuse" recipes by switching ingredients.

This simple recipe for a cookie , that highlights these points. It has an instruction with 3 simple steps to make delicious cookies. It has some ingredients (attributes) mentioned - dough, apricot jam, and parameters like temperature at 350 degrees, time for 15 - 18 minutes and 1/3  cup of jam. It is straight forward and it even "reuses" recipe for "basic sugar cookie dough"! We certainly know who is making this cake - you, and the ingredients and implements you will be using.



In our abstraction in a procedural language, we created a program with the instructions to bake a cake. We probably broke down each step into a separate procedure (or function), and created a main function that looked like the recipe and called these functions to get the baking done. We created some variables outside of these functions for the parameters required. This works and even is simple to write. But, we assume a lot of things here, like the main function itself acts like the baker executing the instructions. Controlling the oven may be built in as a built in function, but at some point it has to call something to make it happen.

In OO, the task gets a bit more complicated! First, you would have to start thinking differently. Now, you will have to think about the nouns first. Of course there is cake, the final product. But, there is also the bowl, oven and a fictitious baker - may be you, but object none the less. These other objects were included in our procedural programs, but were not given much importance. Any reusable, replaceable parts of the program will now have to be separate objects. In our example, if we make the Oven and the Baker and bowl objects correctly, they could be reused in making/baking pies as well as switch to make or even pot roast! Guess what, you can even replace the baker easily!! This is what makes OO so reusable and to some "so bloated!".

If you are interested in OO cakes, here is a link on a cake OO.

 

 

 

So, To OO or not to OO?


OO is almost everywhere around us. Language after procedural language (recently PHP, Coldfusion, ABAP, Flash ActionScript) have added OO capabilities in recent years. But then, there are also others that write about the difficulties and shortcomings of OO. The post titled  Debate Still Rages Over Object-Oriented Programming discusses such dilemma. While you are there, don't forget to check out  Marc Funaro's orginal post here: How OO Almost Destroyed My Business. He has articulated very nicely, why he couldn't and didn't have to switch over to OO completely. And see here for a funny take on Java's OO techniques! Irrespective of such views, OO is here to stay. Even if you don't use OO technology currently, chances are, sooner or later you may be exposed to it.

OO methodologies could get complex and could actually distract a developer from the original goal of getting a working program quickly. General rule of thumb I have, is to use procedural for any small (read quick and dirty - most of my utility programs and scripts fall under this) programs and use OO methodology for any larger (business, corporate, enterprise, framework) programs and programs that have potential to grow in the future. (The truth is, even the quick and dirty program/script could outgrow their originally intended lifetime and objects do creep into those every now and then!). OO definitely enforces certain planning ahead of time, so it takes a bit longer to develop it upfront, but if a program lives long enough, re-usability makes up for the cost over time.

In the cake example above, if I am just making a cake for the family on a weekend, simple procedural implementation would be fine! But, if you are going to be making a cake each weekend or thinking of starting a bakery with several branches down the road, then I better start thinking in OO terms, so we don't have to re-invent a lot of stuff later.

General Design Considerations
Irrespective of whether we use OOP (OO programming language) or not, we are bound to use the modern techniques like Abstraction, Encapsulation, Inheritance and Polymorphism in our program designs. Underlying all these, is the code reuse, data hiding and of course modular design. These of course are not OO by themselves, but are the foundations of OO techniques. By nature of it, OO is almost automatically modular.




  • Keep your code Clean




If the code is not "cleanly" written,  even you cannot understand it a year later. By clean, I mean well commented (documented) and following some coding standards and best practices.


As for coding standards, what's good for one language isn't always good for another - for e.g, in Java we follow Camel case for naming variables while in PB  identifiers are mostly in lower case. So, if your system includes programs in different languages, make sure to include language specific standards as well. Also, best practices for any language may change over version. For e.g., there may be newer constructs and/or functions may be available in a later version that are more efficient than what was available earlier. Make sure to keep up with those in your programs.


OO Note: OOP languages such as Java promote commenting and documenting of the programs easier, with the Javadoc tool (even more so, with IDEs like Eclipse and Netbeans). A well written framework for Java, actually enforces some coding practices automatically. But we can achieve such coding practice with libraries of well written API. A well commented header file distributed along, could act as the documentation for the API.



  • Keep it modular



By breaking down your program into smaller modules, your program becomes more manageable. If your program includes specific modules for various functionalities, it will be easier to replace or rewrite or even add a specific module without affecting the entire program. For e.g., if you isolate all database related functions to a separate module, then if your database changes, then all you have to do is to replace this one module. In our cake example above, we could simply add an object for Microwave and start using it instead of Oven, if oven breaks down!


OO Note: Writing such "modular" programs comes naturally in OO languages as the program is essentially built around objects (or classes). But this concept itself is not new. For e.g., in C, you can achieve this by keeping different modules in separate source files), your program becomes more manageable.




  • Make the code Reusable



How many times, have you "cut and pasted" your code from one source file to another to "reuse"? If the program is properly designed, then the reuse would be much easier and automatic.


OO Note: In OO, inheritance promotes reuse naturally. A well written framework or class libraries contain reusable classes and methods. Apart from using the classes in the framework themselves, one could extend those via inheritance and thus more custom classes for others in a company to reuse. For e.g., you can create custom loggers specific for your need, by extending classes in log4J library.


But again, this is not specific to OO. In a procedural language like C, one could build libraries of reusable functions. While C doesn't allow extension through inheritance, reuse can be achieved through composition. An inheritance is typically defined by a is-A relationship, while a composition by a has-A. Here is an excellent article that compares the two.

Saturday, August 4, 2012

My journey with Ubuntu Contd - On to Precise Pangolin

Linux via Ubuntu

I posted about my first Ubuntu machine earlier. Though I've used Unix for a long time and Linux for a bit for work related activities, this was the first time, I boldly went for Linux at home! Ubuntu made it possible with a great Windows like Desktop.

It was an experimental machine we built with Intel Atom PC (Atom 230 160GHz X2). It worked well with Ubuntu 10.x I installed earlier. In fact, for over a year I managed with10GB hard drive. Sure, it complained about running out of space constantly and I kept moving my files out to Windows PC (Remote Desktop from Ubuntu to Windows worked like a charm). With limited memory and resources, it worked so great, often times better than my newer, more powerful HP machine with Windows Vista.

It has been an amazing journey with Ubuntu. A lot of software I came to know on a Windows PC over the years, are now available on Linux, free and sometimes with better functionality. I installed, Firefox, Google Chrome (it is called Chromium), Libre Office (successor of Open Office, replacement for Microsoft office, I use this a lot on Windows as well) and even my mail client, Mozilla Thunderbird. I could do Remote Desktop to Windows, copy and share files, print to a HP printer almost automatically (on a Windows PC, each time I have to install drivers, here Linux almost finds its own). And of course, internet works right out of the box in Linux.

Moving on, Upgrade

Recently, I decided to continue using Linux and it was time to upgrade. It's still running on an Atom mother board, but I added a new 1TB hard drive. This time I wanted to try to different flavors of Linux. So, I tried installing Ubuntu 12.04, Fedora and Debian. Debian interface was not that great, but seems more powerful. Wiki page on Debian mentions about 3000 volunteer developers developing it over a time period (since 1993). Definitely a matured operating system. But, it's also overwhelming for a first time user.

Fedora 17 is looking nice too. It's community project sponsored by Red Hat. In version 17, it's installation is as easy as Ubuntu and desktop was as flashy too. I tried all 3. I first installed all the 3. But, some how I was drawn back to Ubuntu. Nothing against the other 2, except Ubuntu as ever was easier and better for Ubuntu beginners.

I've also looked at Open Suse and Oracle Linux. So far, I haven't tried them out yet.

Partitioning

Each Linux Dist. comes up with their own Partition Editor which was easy to use. Ubunbtu has GParted, a Partition Editor which is really user-friendly. I remember using Fdisk on DOS systems earlier. You have to exercise, so much caution, otherwise you could lose all partitions. GParted lets you "play" with partitions and it doesn't save anything until you confirm it. When you partition the drive using GParted, you specify which drive belongs to what location (File System and Mount Points). Ubuntu assigns File System names automatically and you can assign a mount point (like /, /home, /boot etc) to each partition. I researched on the Internet and came up with a simple partitioning scheme (I gave 300GM to Ubuntu):

Recommended Partitioning Scheme for a simple Ubuntu installation

































PartitionLocationSizeComments
Partition 1/boot10GB
Partition 2/50GB
Swap File5GMTwice the size of memory
Partition 3/home235GM

This was good, until I installed too many software and root "/" was running out of space. I booted with the Ubuntu Install CD and adjusted the partitions. It was so easy, as I had a lot of empty space to widen the partitions. (Getting to the partition Editor on installer CD was a bit tricky. I had to make the PC boot sequence to CD first. Then when CD booted, I had to pick "Try Ubuntu". Gparted is part of the installation disk).

Multiple Operating Systems

Since I got a bigger disk, I decided to go with all the 3.I tried Ubuntu, Fedora and Debian, side by side. I also tried Partition editor with multiple OS installed. Each OS will see the other OSs partitions in Partition Editor. I learned about sharing the Home directory with multiple OS and changed the mount point for /home to point to the same partition in each OS. Make sure to un-check the "Format" Checkbox, otherwise new install will overwrite any earlier home directory.

One problem I had, having all 3 OS installed was a problem with Grub (Linux Boot Loader). Some how, it was hanging in one of the OSs. (I mixed 64-bit OS for Fedora and Debian, so it was not a fair comparison. Later I tried 64-bit Ubuntu, it crashed too! Intel Atom 230 is claimed to be 64-bit compatible, but 64-bit OS didn't perform well. Probably has to do with memory limitation too. I only have 2GB).

Ubuntu, again!

Having issues with multiple OS installations, I decided to go with one main OS. After reading a bit about comparisons on-line, I decided to go with the known devil (sorry to call it that), Ubuntu. But Ubuntu itself has changed much. Version 10.x was a great OS. The new version, Ubuntu 12.04 LTS is code-named Precise Pangolin ( Apparently Ubuntu 10.x was called Lucid Lynx). LTS means Long Term Support series (love the way they come up with these names!!), they already have plans for Ubuntu 14.04 through 2019 (See here), that's long-term for you and me!!

Precise Pangolin User Interface

To be honest, I was a bit disappointed with the latest version of Ubuntu initially. It still has a windowing desktop, but they have changed the software behind it. They have switched their Graphic interface to use what's called Unity Interface. Prior versions used Gnome interface. To me Gnome was a bit more user-friendly. I am still getting used to the new launcher bar that shows quick launch icons for applications. Every new Application installed already gets pinned to the launcher bar and you can unlock it.

[caption id="attachment_423" align="aligncenter" width="300"] Ubuntu 12.04 Pangolin Desktop[/caption]

The problem is the launcher bar also includes those that are running. Only thing that shows it is running is the small triangle on either side of the icons. (For e.g, see image on the left.   The white triangle on the left shows that the settings->Appearance is running.

[caption id="" align="aligncenter" width="115"] An icon on the Launch Bar; Triangle on the left indicates running[/caption]

Apart from these, how you get to things is also different. The Gnome toolbar that had "Applications", "Places" etc disappeared. To get to the applications, you either go through Launch bar and if you don't have it there (Launch bar is menu of icons, so size is limited), then you go to Dash Home  by clicking on the icon on the Launch bar and then type the application you need. For e.g., to get Kompozer (HTML editor that's not in my Launch Bar, I have to pull up Dashboard and type some chars to get to the application.




Sort of going back in time, it seems. (Windows 7 changed their task bar too. It works something similar to launch bar here. Why do they keep changing something we are used to??).

Apparently, I am not the only one that's not happy with Unity Interface. This link goes one step further and talks about how to install and get back Gnome desktop on Pangolin.

Copy the old files

I wanted to copy the files I had on the old Linux drive. After a bit of fumbling, I put the old drive inside an external hard drive enclosure and connected to the machine as a USB drive. This worked like magic. The hard drive showed up as a mounted volume.  All I had to do was copy the home directory.

[caption id="attachment_411" align="aligncenter" width="535"] External Volumes are mounted and show up in Devices List[/caption]

Unfortunately, I had encrypted my home directory when I installed Ubuntu last time. So I tried the other way around. I went into BIOS menu (pressing F2 at boot time, on some computers you may have to use DEL key) and changed the boot sequence to boot from USB drive, saved and rebooted. It was a bit slow, but surely it booted from the external drive with the old version (v 10.x) of Ubuntu. And as usual, the new hard drive showed up as a mounted volume too. Then it was just a matter of copying the files over to my new home!!! On windows, some of these tasks could have turned into nightmare. It's so refreshing to use a OS that has less issues for a beginner.

Go Virtual

Then, I discovered about Virtual box. I un-installed all 3, wiped the disk and reinstalled Ubuntu and this I gave it full 600GB. My goal is to install Virtual Box and install multiple Guest OSs.While researching this, I found out about appliances! You can actually build minimal OS distributions with specific applications (like Web Server). Take a look at the Suse Linux page for creating such appliances.I also tried Oracle Developer Day Database appliance. With this I could try out Oracle 11g (we are still at Oracle 10g at work) finally.

[caption id="attachment_412" align="aligncenter" width="300"] Virtual Box Application main Screen[/caption]

I will post about Virtualization separately.

Monday, April 16, 2012

Object Oriented Concepts - An Introduction

Work has been busy for a while. I am finally returning to my blog on PB.
We started looking at PB program as a collection of Objects in the last post. Yes, PB is now an object oriented language, but it sure did not start that way. Sure it has some objects like windows, buttons etc, but does that make it an Object oriented environment automatically? This topic itself is so huge, So, I decided to touch on these first before continuing further with PB.

What  is an Object?

The simplest definition of an object is a thing, a noun or an entity. In the programming world, this has come to mean a self contained programming unit. By hooking different "objects" you can make a program. Objects themselves are not new. Remember the old C programs? When you compile individual C files in a program, they were converted to "object" file. Though it doesn't mean the same, it stands for a program unit. By linking different "object" files, we arrived at the final program (Executable). Then there were modules. Even though there is a subtle difference between a module and an object, in theory they are the building blocks of a program. Remember, COM (Microsoft .Net evolved out of this eventually)? The idea was to have bunch of OLE (eventually called Active-X) that would be glued together in a program. In fact, Visual Basic was invented for such a purpose originally. We will explore more about objects in the coming posts. But, while you are at it, think about another term frequently used in OO: a Class. Several authors allude this to a blueprint, cookie cutter, a mold for creating objects.

What is Object Orientation (OO)?
An object has several other characteristics that makes an object in an Object-oriented language different from earlier definitions of objects. If you Google for the word “Object oriented”, you will find a million links. If you sift through all those, you will come to the following conclusion:
Object Oriented is a new paradigm (though it's been around for 30+ years now) and it is difficult to learn and/or master. Yet you see, every programming language (PHP, Coldfusion, PERL, PYTHON, you name it) out there is joining the OO way. So, if you are going this route, you have to get the concepts right.

Object oriented is many different things to many different people. Even the languages that claim to support OO now, are not fully OO themselves. So, let us discuss fundamental definition of being object oriented. In later posts, we will continue to drill down this topic.
Several books talk about Object Oriented (OO) approach to contain the following four basic characteristics

  • Abstraction

  • Encapsulation

  • Inheritance

  • Polymorphism


We will analyze each of these below.

Abstraction
Almost anything we do with computer software is an abstraction of the real world. An ATM machine is essentially modeled after a real-life teller it came to replace. Abstraction itself is not OO. Cobol and Fortran programs abstracted business models and mathematical models respectively for about 50 years now. But the abstraction in OO, is much closer to the real life. After all, real life is full of objects: people, earth, machines, food, microwaves, books, TV, radio etc.

In OO, we try to call a Radio a radio and define it's properties and operations to emulate real life one (actually a radioObject is more appropriate name for it; of course, it's a software object not a real one, but you could make it to play music!). A software abstraction is context based. For e.g., when we abstract customers, shop owners and employers we are abstracting people. In such an abstraction, we definitely would like to know their names, but we don't need know their race, gender, height, weight etc. Where as, if the person abstracted is a patient, suddenly some of these characteristics matter!

A speedometer in a car is a good example for abstraction. We see the needle to get an idea as to how “fast” or slow the car is going. Abstraction in this case varies between MPH in the US to Kilometers in some other countries. A thermometer, similarly abstracts how "hot" outside temperature is.

Encapsulation
This is the next characteristic of an OO programming environment. Encapsulation, simply means hiding private data and showing proper (public) interface. We don't want to see inner-workings of a microwave or a car when we are using it to heat our pizza or driving to get one. When we shop for a car, we are happy if we find a steering wheel, something that looks like gear, brakes and of course, those 4 wheels that the car needs to “run”. Of course, some of us would like little more glossy finish, but none of these involve putting engines right next to you on the front seat! And of course, the way those engines come pretty well sealed these days, they are very well encapsulated themselves. There is one “interface” you see, the oil cap that lets you fill oil. The content, level of oil, is not immediately visible. If you open and use a dip stick, you can find out the actual level of oil!

Speedometer mentioned above is also a good example of encapsulation. To “see” speed one doesn't need to look at rotating gears, tires, road etc. They just look at the interface called “Speedometer”.

Encapsulation itself is not a new concept. It's been around since the early days of programming. In Pascal, you could achieve certain amount of encapsulation by proper function design. C of course, offered modular programming (file level – see post “Modular progamming in C” for more information).

Inheritance

The most common meaning for this English word is the property (read “Wealth”) inherited from ancestors when they pass on. And it's not just property that can be inherited. We also talk about some characteristics passed on (genetically). It's this latter mean that's adapted in the computer lingo. Inheritance in OO allows an object to acquire properties and behavior of some other. So, we talk of a Dog object inheriting properties of an animal object (actually class, we will discuss these shortly).

Of course, in real life we don't say Dog inherited from Animal. We say, Dog “Is A” animal. We also say Car “has a” engine. And we have access (reference) to computers in the hotel. These are different types of object relationships in OO. An “Is A” is considered to be inheritance. A “Has A” relationship is called composition. And a reference to an object out there, is an Association.

Of these, Composition and Association were possible is non-OO languages as well. Inheritance itself could be indirectly implemented through these 2. For e.g., in C, using plain structures and function pointers we could mimic any of the above relationships.

Polymorphism

This came from a Greek word. Poly means many, morph – form. For e.g., different forms of shapes. Circle, Square, Rectangle and Triangle are all shapes. Of these, Rectangle and Square are similar, in that they both have sides. Circles and Ovals could be similar.

Now, shape is a noun, an object. How about a verb – an action or behavior? Let's take the word “Play”. Try using this English word in sentences and you will see how many different objects can play in how many different ways. Like playing a piano, like playing Football, Playing cards, playing a part in a play, Play with a child, Playing with life! This is truly a polymorphic word. How about the word, “Talk”? “Talk” to a dog, barks, cat meows and the cow moos, not exactly talk in the human sense, but in the sense of “open your mouth and make a sound” it's the same operation, with various forms!

In this post, we tried to explain what OO is and try to understand the terminologies typically related to the OO. This wasn't totally a technical discussion. Now that we got some definitions out of the way, let's get down to business with the topic of Object Oriented. Try to remember some of the examples when we get to our next post.

Below picture summarizes all these terminology as they relate to a Microwave.