Adobe Flex: Program in Style or an Elevator Pitch

We usually run Flex training for private clients of Farata Systems , but once in a while we teach public classes for people with different programming background (my next Flex class at New York University starts in November). All students usually get excited by Adobe Flex, but each of them comes with different understanding of how to do things right. So I “ll tell you the story that might have happened in a real life, but first, let me remind you of an old Indian tale about seven blind men and an elephant . In short, one blind man touched the elephant “s head, the other one touched the tail, someone was by the leg. And each of them visualized an elephant differently based on what he touched hellip; My students usually arrive to the classroom early, but this time three seats were empty. Five minutes later I got a phone call from one of them explaining that they got stuck in the elevator and will stay there for another fifteen minutes until the serviceman arrives. Needless to say that each of them had a laptop (do not leave home without it), so I gave them a short assignment to trying to help them use this time productively.

Here “s the assignment: Create a Window with a Panel that can resize itself by clicking on the button +/- that is located in the right corner of the panel. One click should minimize the panel “s height to 20 pixels, and a subsequent one should maximize to 100 pixels, and so on.

For example, these are the two states of such panel:

I forgot to tell you that one of these guys was a Cobol programmer, the other one had Java background, and the third one was a Smalltalk fan.

From Cobol to Flex

The Cobol programmer thought to himself, rdquo;We used to write long programs because during job interviews they usually ask how many lines of code did I write. These guys are different, so to earn a good grade, this program should be small rdquo;. He finished the program on time and this is what it looked like:

lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “http://www.adobe.com/2006/mxml ” layout= “absolute ” gt;

lt;mx:Panel id= “thePanel ” title= “The Panell ” height= “90 ” width= “100% ” headerHeight= “20 ” / gt;

lt;mx:HBox width= “100% ” horizontalAlign= “right ” paddingRight= “2 ” gt;

lt;mx:Label text= “- ” fontSize= “16 ” width= “20 ” height= “17 ” fontWeight= “bold ”

id= “minimizeActions ”

click= “{if (minimizeActions.text== ‘+ ‘){

minimizeActions.text= ‘- ‘;

thePanel.height=100;

} else {

minimizeActions.text= ‘+ ‘;

thePanel.height=20;

}

} ” / gt;

lt;/mx:HBox gt;

lt;/mx:Application gt;From Java to Flex

The Java programmer thought, “The standard Flex Panel class does not have the property that remembers the current state of the Panel, but Flex components are easily extendable, so I “ll create a descendent of the Panel in ActionScript, add a private state flag (minimized) , public setter and getter, and resize function. This way my new Panel class will be reusable and self contained. rdquo; This is his reusable ActionScript class called ResizableJPanel:

package {

import mx.containers.Panel;

public class ResizableJPanel extends Panel {

// state of the panel

private var isPanelMinimized:Boolean;

public function get minimized():Boolean{

return isPanelMinimized;

}

public function set minimized(state:Boolean){

isPanelMinimized=state;

}

public function resizeMe():void{

if (minimized){

minimized=false;

height=maxHeight;

} else {

minimized=true;

height=minHeight;

}

}

}

}

This is Javist ‘s mxml code:

lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “http://www.adobe.com/2006/mxml ” xmlns:local= “* ” layout= “absolute ” gt;

lt;local:ResizableJPanel id= “aPanel ” height= “90 ” width= “100% ”

title= “The Panel ” minHeight= “20 ” maxHeight= “100 ” headerHeight= “20 ” / gt;

lt;mx:HBox width= “100% ” horizontalAlign= “right ” paddingRight= “2 ” gt;

lt;mx:Label text= “- ” fontSize= “16 ” width= “20 ” height= “17 ” fontWeight= “bold ”

id= “minimizeActions ” click= “resizePanel(aPanel) ” / gt;

lt;/mx:HBox gt;

lt;mx:Script gt;

lt;![CDATA[

function resizePanel(thePanel:ResizableJPanel):void{

if (thePanel.minimized){

minimizeActions.text= “- “;

thePanel.resizeMe();

} else {

minimizeActions.text= “+ “;

thePanel.resizeMe();

}

}

]] gt;

lt;/mx:Script gt;

lt;/mx:Application gt;

From Smalltalk to Flex

The Smalltalk guy thought, “Let me see if the standard Panel is a dynamic class. If not I “ll extend it just to make it dynamic and will be assigning the panel “s state on the fly. I hope Yakov is not one of these object-oriented Nazis rdquo;. This is his panel ActionScript class that just adds a dynamic behavior to the Panel:

package{

import mx.containers.Panel;

public dynamic class ResizableSmtPanel extends Panel

{

}

}

His mxml class looked like this:

lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “http://www.adobe.com/2006/mxml ” xmlns= “* ” layout= “absolute ” gt;

lt;ResizableSmtPanel title= “The Panel ” id= “thePanel ” height= “90 ” width= “100% ”

minHeight= “20 ” maxHeight= “100 ” headerHeight= “20 ” gt;

lt;/ResizableSmtPanel gt;

lt;mx:HBox width= “100% ” horizontalAlign= “right ” paddingRight= “2 ” gt;

lt;mx:Label text= “- ” fontSize= “16 ” width= “20 ” height= “17 ” fontWeight= “bold ”

id= “minimizeActions ” click= “resizePanel() ” / gt;

lt;/mx:HBox gt;

lt;mx:Script gt;

lt;![CDATA[

function resizePanel():void{

if (thePanel.minimized){

minimizeActions.text= “- “;

thePanel.minimized=false;

thePanel.height=thePanel.maxHeight;

} else {

minimizeActions.text= “+ “;

thePanel.minimized=true;

thePanel.height=thePanel.minHeight;

}

}

]] gt;

lt;/mx:Script gt;

Since we are not in the classroom, I “m not going to go to a code review and lengthy discussions, I will just say the I gave an “A rdquo; to each of these guys…and here ‘s the Flex version: lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “http://www.adobe.com/2006/mxml ” xmlns= “* ” layout= “absolute ” gt;

lt;mx:Component className= “ResizablePanel ” gt;

lt;mx:Panel gt;

lt;mx:Script gt;

[Bindable]

public var minimized:Boolean = false;

lt;/mx:Script gt;

lt;/mx:Panel gt;

lt;/mx:Component gt;

lt;ResizablePanel title= “The Panel ” id= “thePanel ” minimized= “false ” height= “{thePanel.minimized?thePanel.minHeight:thePanel.maxHeight} ” width= “99% ”

minHeight= “20 ” maxHeight= “100 ” headerHeight= “20 “/ gt;

lt;mx:HBox width= “99% ” horizontalAlign= “right ” paddingRight= “2 ” gt;

lt;mx:Label text= “{thePanel.minimized? ‘+ ‘: ‘- ‘} ” fontSize= “16 ” width= “20 ” height= “17 ” fontWeight= “bold ”

id= “minimizeActions ” click= “{thePanel.minimized=!thePanel.minimized} ” / gt;

lt;/mx:HBox gt;

lt;/mx:Application gt;

What “s the morale of this story? Learn another language, no matter what ‘s your current background. Initially you will try to bring your own culture to this new language, but eventually your horizons will broaden, which will make you a better programmer.

P.S. Isn “t it funny that the Cobol guy “s version was the shortest one? But was it the best one? Can you offer a different solution?

Recommended Java books

I keep hearing on te radio that every person has to eat five fruit servings a day. But this is about phisical health. How about mental healthy diet for Java developers? I recommend to consume at least five Java books a year. JDJ has published a review of Java books that I like, which includes four books.

The fifth one is “Java Reflection in Action ” from Manning. This 250-page book has lots and lots of useful and well covered information not only about reflection, but also about proxies, class loaders, stack introspection and design patterns. A really good book.

Bon Appetite!

Brian Goetz has been hired by Sun

Brian Goetz, has been hired by Sun Microsystems as a Technical Evangelist and he will be joining the SE organization at Sun. Brian will get to continue his role in the Java Community as an “Evangelist “.

I have an honor to know Brian personally, and have to say that he belongs to a really small and exclusive group of experts who really know what ‘s uder the JVM ‘s hood, and what ‘s the best way to design multi-threaded applications.

My short review of his new book is located over here . This book is one of the best Java books of the year.

On one hand, Sun is laying off several thousands of people, on the other hand, they hire the best of the best: recently they ‘ve hired lead JRuby developers , and now Brian. I guess, JS decided to improve the overall quality of the workforce.

Congratulatins, Brian and good luck!

Joel against Sprint

Sprint sent their new phone to Joel Spolsky for a review. And he did it . Joel did not like the phone. Now Sprint does not like Joel.

I trust Joel ‘s review, but I ‘m still not sure if he should have published such an agressive post. Every pot has its lid. If the price is right, there could be many people who will like this phone. Because this may be the only phone/service with MP3 they can afford.

I often receive review copies of new books from publishers. Only some of them stand out, but I prefer not to write about the books I do not like. For example, I already had 4 books on AJAX, which I do not really like, so I just ignored them. Yesterday I ‘ve received the fifth one, which looks very promising. I ‘ll finish reading it in a couple of weeks and will write a review, if I still like it. Otherwise, I ‘ll keep my mouth shut. Being a book author myself, I know how much time the authors spent to give birth to their one and only. And again, each book has its readership, no matter I like it or not.

Buying butter in New York

I work on project with Joe, a brilliant business analyst from Chicago . He knows financial industry back and forth. He ‘s so good that the client does not mind flying him over to New York every other week and pay for his stay in Midtown Manhattan hotels. When Joe is in town, we often go to have lunch together. Yesterday, when he asked me of I want to join him for lunch, I said that I have plans – I had to go to Citarella at 75th street to buy some butter. This statement got him excited. He said, “Only in New York a person can walk 20 blocks just to get some special butter! “. I politely explained that regular butter that is sold in supermarkets is junk, and he should not eat it. He said that I should get “Land ‘O ‘lakes ” butter that according to Consumer Reports is the best butter. Yeah, right! Consumer report believes that Toyota is the best car as well. I usually eat the French butter called President, but last week a friend of mine gave me to try a butter from a Ronny Brook farm , which she buys at Union Square Market on Saturdays. This is THE butter! If you are older than Internet, you might remember that taste of a real butter that some people in villages were making for themselves.

Ayway, Joe got excited and started to google the butter. To his surprise, there is a Web site where the US Department of Agriculture tells you how to buy butter . I really respect the USA authorities, but when it comes to war in Iraq and buying butter, I have a different opinion.

The USA is a great country where everyone has something to eat. When I came to this country fifteen years ago with no money in my pockets, I thought that the brand “I can ‘t believe it ‘s butter ” tasted great. Now I understand that this is garbage. But it was affordable to me. Chicken legs – 39 cents a pound, hot dogs two packs for a dollar. This was OK at that time. I survived, and now can afford to purchase better quality products. If, God forbid, I ‘ll be poor again again (caused by things that are not controlled by you: disease, divorce, my son gets accepted to Harward, et. al.), I ‘ll start eating at Golden Arches (a.k.a. McDonalds).

Not every American programmer can afford to own a nice house (not a mortgage) and drive a luxury car. But any computer programmer (and business analyst) in the United States can afford to eat tasty and healthy food (do not even try to tell me that eating fat is bad – it ‘s not even funny anymore). Have a good day, and treat yourself to a good lunch and dinner tonight

Do Spring combined with Hibernate weigh less than EJB?

Lofi Dewanto in his blog asks if Spring framework used with Hibernate is still a lightweight or a heavyweight champion of Java frameworks?

I believe that not only this combo, but even each of them separately is pretty heavy as ANY framework. Only reusable loosely coupled components are lightweights.

Spring framework is advertised as a set of components that can be used separately, but you can also wire them together by adding two pounds of XML. The minute you do this, you fall into an XML trap. So if you use any single component of the Spring framework it ‘s lightweight. But as soon as it takes two to tango, and you are pulling a tiny roll of thin wire out of your pocket, it becomes heavyweight. Because wires (a.k.a. XML) tend to twist and create a mess.

Speaking of Hibernate, I ‘m not even sure WHY so many people are using it in the first place? I can understand if an enterprise architect is laying out a new design of a stack of business applications and wants to enforce to a firm-wide standard for data persistence. But if you are developing a typical CRUD application, especially when it comes to using already existing and not perfectly designed database, why even bother with Hibernate? Do you really hate or afraid of SQL?

Anyway, take an application built on the Spring framework components interconnected with thin wires, put on top of it Hibernate with the wires of different diameter, and maintainability of your application decreases while hard to find bugs are making themselves at home in your application.

During the last three to four years many people like bashing EJBs as an unnecessary complicated framework with lots of convoluted XML descriptors. Now EJB 3.0 with its annotations is trying to appeal to enterprise developers again. This won ‘t be easy, because bad memories last for years. But do not kid youself when you substitute EJB for Spring/Hibernate combo. Your life won ‘t be easier at all.

I do believe in standalone POJOs which know nothing about environment they are in (an IoC concept works fine), and know how to perform a specific function, i.e. send a message, manage transactions, create a pretty report based on provided SQL, model some financial process, find an optimal route, e.t.c. Just pass the required parameters to this black box, get the result back and do whatever you want with it. Stop wiring, just write the freaking code specific to your business application and forget about it when the new project start. Do not forget though about independent reusable components.

Spring is probably one of the best Java frameworks available today. It has only one drawback: it ‘s a framework.

I just sold my business card for 97 cents

I just sold my business card for 97 cents to the soup nazi , which is a small soup place made famous by Jerry Seinfield. They have a large glass jar with a sign “Give us your business card and you ‘ll get 10% “. So I gave them my business card, what the heck!

Here comes the mathematical quiz: how much money did I spent for lunch today if the sales tax in New York is 8.65%?

Weve taught Adobe Flex to talk to Microsoft Excel

We are finishing writing our book on RIA with Flex and Java , and the last chapter is called “Integration with External Applications “. It ‘s sixty five pages of some advanced read. After writing this chapter one of us suggested that it would be easier to re-write Excel from scratch. It ‘s not straightforward coding, but the results are good. After covering some basics about ExternalInterface and how it enables bi-directional communications between JavaScript inside HTML page and embedded Flex application, this chapter leads you through jungles of Flash technologies such as External API and LocalConnection as well as through maze of Microsoft old timers like ADODB and OWC. We have been coding in VBA, JavaScript, XSLT, not to mention the ActionScript, of course.

We ‘ve also provided the code for a more complex example of integration with external applications, namely Microsoft Excel. These days the data entry or analytical applications with no Excel integration are simply hard to find. Be that saving of data in native Excel format or CSV, accepting data entered in Excel or dynamically creating Excel Charts based on the database queries ndash; we see it everywhere. Microsoft Excel is one of the most popular business applications ever .

This solution involves two ActiveX objects embedded in an HTML page with instant as well as on demand data synchronization between the objects. One of these ActiveX objects is the Flash Player control, executing Flex application. The second ActiveX is a Spreadsheet from Microsoft Office Web Components (OWC). You may think about Spreadsheet as of miniature Microsoft Excel application running inside the HTML page.

Disclaimer: This particular sample works under IE in Windows XP, but has some issues on Windows 2000. I ‘m sure, if you are still running Window 98 you may face some issues as well, which we ‘ll be happy to fix for its customers. If you do not see the spreadsheet while running this example, most likely you do not have OWC installed – get it from this site .

Meanwhile try to make changes in the left (Flex) and right (Excel) spreadsheets and the data should be replicated back and forth. We ‘d appreciate any suggestions/critique of this application.

Application like this opens the doors to some interesting project ideas: we also know how to generate and execute formulas on the fly, provide proper reporting and printing, and more. But that ‘s later. Let ‘s finish the book first.

The best programming language

eWeek magazine published an article titled “10 Programming languages you should learn right now “. You may also find Tiobe statistics useful in making the what-to-learn-next decision (it ‘s sad to see that PowerBuilder is as popular as Algol and PL/1).

I believe that in era of outsourcing, when programmer are treated as a commodities that you can easily purchased by a dozen, learning the business you are in has a higher priority than any specific programming language.

The name of the programming language does not matter as long as you are in top 20% of developers in this language. I ‘m sure, best Cobol programmers never have problems finding jobs.

My logic is simple: most of the programming jobs will be outsourced and not because programmers in India charge less ( in most cases it ‘s not cheaper at all), but because the pool of programmers here in America is shrinking year after year. Enrollment in Computer Science in all US colleges goes down.

Not every programmer is or can become a superstar. Some people write computer programs because it pays better than delivering pizza, painting or acting in Broadway musicals. They come home after work and forget about programming while enjoying other activities (delivering pizzas, painting or acting in Broadway musicals). If you are looking for job security, and can ‘t get into these 20% of the best, learn business you ‘re currently in (finance, telecommunications, entertainment, pharmaceutical, et al). A mediocre programmer with business knowledge will always find a job. This rule does not apply to people that develop general software and work for companies like Yahoo, Google and the like. But usually, people in these companies technically are pretty good anyway. For the reference: main languages at Google are C++, Java and Python.

The other strategy to use is to become one of the best in on of the least popular programming languages. The job market will be very narrow, but there is literally no competition, cause everyone wants to program in cool languages. Back in 1992 I ‘ve been doing SQLWindows from Gupta a.k.a. Centura. Anyone remember this? Certainly not Tiobe. Guess what, I know a lady who works as a Centura contractor charging top dollars. My wife is still a PowerBuilder programmer and her check comes in the mail regularly regardless of what eWeeks or Tiobe writes. Wondering when Cobol will die? Not within the next thirty years: there is no money for the funeral as too many applications are deploed (and work!) in production. So if you are already a Cobol programmer, you might be better off just improving your Cobol and related skills than learning Java. But again, this ‘ll work only if you are capable of getting into these 20% of top Cobol ‘eros.

Recently, I was interviewing Bruce Tate , and he suggested that learning another programming language will help you to become a better programmer in your primary language. I agree, this is a good idea.

Set yourself a goal to learn one additional language each year, and you ‘ll definitely will become a better and more marketable software developer. Which one to learn? Follow me as I have a pretty good nose for what ‘s going to be hot next. Do not trust me? No problemo.