Yakov Fain's Blog

My notes about everything in IT

The Dark Side of Google’s HR

with 15 comments

Typically, the conversations that involve Google’s hiring process revolve around the cool questions during technical interviews. Something like, “How many piano tuners are in San Francisco?” Business Insider even published and article titled “15 of Google Interview Questions That Will Make you Feel Stupid”.

But Google’s HR made me feel stupid in a different way. I’m one of the partners of a small consulting company Farata Systems. Some time ago we’ve received a request from another company (let’s call it XYZ) to bid on an interesting project. We won the project competing with two larger bidders. The project started. Our software developers were doing a good job. The XYZ’s team lead praised our job.

Last year Google acquired XYZ, and one of our employees (let’s call him Alex) continued working on this project as a consultant. When we hired Alex, he signed the offer letter with a typical clause that he’s not allowed to work directly for Farata’s clients for the period of 12 month after terminating the employment with us. Previously Alex signed an agreement that had a clause about typical two week notice.

A couple of months ago, the Google’s HR employee approached us asking if they could hire Alex directly – they really like him. No wonder, Alex has excellent technical skills. The HR worker from Google offered us a certain compensation for such conversion, which is also a typical practice between the IT vendors and their clients. There were phone conversations too. We’ve rejected their initial offer and responded explaining how they can keep Alex on the project.

About a month ago, on Friday, we’ve received an email from Alex stating that this was his last day with Farata. No two week notice. He just left and stopped answering phone calls or emails. The team lead from Google didn’t respond to our attempt to contact him either. The Google’s HR person keeps silence as well.

Now I have an easy question for you, “Guess where Alex works now?” You got it! He works at Google, we know it for sure.

We have not decided yet, if we want to take any legal actions against Google, but this experience left a really bad taste in my mouth. Knowing how many piano tuners live in SF is important, but there are some rules in IT that professionals should respect.

Written by Yakov Fain

September 3, 2014 at 6:25 am

Posted in Uncategorized

JCrete 2014

leave a comment »

I spent the last week in the beautiful island of Crete. It was my second JCrete unconference. Last year I was blogging each day and you can see all these blogs here.

This year JCrete was as good as always. Geertjan Wielenga wrote a detailed report here.

Special kudos to Stephen Chin. Here’s his awesome photo of all unattendees. He also recorded a bunch of videos with unattendees of JCrete (myself included), and you can enjoy them at http://nighthacking.com/category/jcrete2014.

My hat off to Uros Djunisijevic who’d never leave his room without  a camera, and you can see lots of JCrete photos here.

This event doesn’t need any promotions – the number of people on the wait list is the same as the number of attendees. JCrete is and will remain successful because the attendees are either seasoned software developers or are eager to learn from others.

But for me, the atmosphere is most important part of this event. Everyone is friendly and is willing to discuss whatever is on your mind. There are no official speakers. Everyone is accessible and willing to share.

After participating in JCrete unconferences I know who’s doing what. If I need to find an answer to a question on a particular Java-related technology, I know who to ask. Networking at its best. And no, I’m not talking about people who can give me work. This is networking of people in the know.

But more importantly, this is probably the best vacation a Java developer can think of. Three hours of session a day followed by relaxing at the great beaches while continuing professional discussions with respected people. This works perfectly for me.

photo (60)

I hope to be able to come to each of the future JCrete unconferences if circumstances permit. I also hope to be able to organize a similar event in the city of Odessa, Ukraine next summer. But I don’t want it to be limited only for Java developers. In 2015 the words “I’m a senior Java developer” will sound like “I’m a master of a Phillips screwdriver”. Modern programmers needs to know several programming languages, don’t they?

Written by Yakov Fain

August 31, 2014 at 8:33 pm

Posted in Uncategorized

Blood Pressure While Presenting at Conferences

with 2 comments

In June I was presenting at the ThingsExpo conference  at Javits Center in New York City. During my talk I was demonstrating how to integrate consumer devices into a business workflow (we develop software for insurance agents). In particular, I was doing live measuring of my own blood pressure to show how to integrate consumer devices into a business workflow. Internet of Things, you know. The results were automatically appearing in my customized Web application.

When the results appeared on my mobile device I was quite surprised to see that my pressure was 200/120. This was damn high. My regular blood pressure is typically around 140/90. If you think that presenting in front of a large group of people makes me stressed out, this is not the case. I’m pretty experienced speaker and, actually enjoy presenting. I thought that was a device failure.

.bloodpressure

I’m writing this blog from the beautiful Crete Island, Greece. As you see, on August 28 at 5:45AM my blood pressure is normal. I’m participating in a small conference called JCrete – this is where experienced Java developers unwind.

Guess what, I was delivering a scaled down version of the same presentation in a small room in front of a dozen people. The atmosphere in the room is friendly and relaxed. May be that’s the reason why the results of my blood pressure measurement where not as high as in New York: 180/110. It’s still pretty damn high. Again, I was not nervous at all. Yes, I was a little excited.

This make me wondering if talking from the podium increases blood pressure? In October I’ll be measuring my pressure on stage at the conference in Moscone Center in San Francisco. I’ll update this blog with fresh numbers then.

Is presenting at a conference dangerous for your health? Should people with high blood pressure avoid public speaking?

If you know of some scientific studies of this subject please leave a comment on this blog. If you are a speaker yourself, do a little experiment – take a blood pressure monitor with you next time you’re presenting. Do the measurements immediately after your talk is over. Don’t be ashamed. Let’s contribute to medical science.

Written by Yakov Fain

August 28, 2014 at 3:55 am

Posted in Uncategorized

Tagged with

What’s the difference between abstract classes and interfaces?

with 3 comments

Probably the most popular question during Java technical interviews is “What’s the difference between abstract classes and interfaces”.  In my own three year old book I’ve offered the following answer:

“An abstract class may contain code in method bodies, which is not allowed in an interface.With abstract classes you have to inherit your class from the abstract one because Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.”

But starting from Java 8 this answer is wrong. This is how I’d answered it now:

1. The short answer: “Abstract classes can implement state in member variables, but interfaces can’t.  You can extend from one abstract class, but implement multiple interfaces.”

2. The long one: “Both abstract classes  and interfaces allow you to implement common static methods while leaving the implementation of some methods to other classes. Abstract classes can also have instance-specific methods and they enforce vertical inheritance hierarchy.  Interfaces can’t have instance methods, but they don’t enforce any class hierarchy, so your class can extend any other class while implementing multiple interfaces.

So abstract classes and interfaces have subtle differences now, which are good to recognize anyway. You can find more details on this subject in the draft of Chapter 5 of my upcoming Java for Kids book.

Written by Yakov Fain

August 18, 2014 at 4:56 pm

Posted in java

JavaFX Event handling and Property Binding

with 6 comments

Some time ago I blogged that Java Swing should be deprecated and replaced with JavaFX. In this blog I’ll show a piece of JavaFX namely event handlers and binding. I’ve created a simple Sign In window with a GridPane layout (it’s JavaFX equivalent of Swing’s GridBagLayout). I’m not going to spend much time on the GridPane itself, but will show you a basic event handling and a binding.

In JavaFX an event object is represented by the instance of the class javafx.event.Event. There are different ways of handling events. Depending on how you structured your application you can handle events either in Java or in FXML. In this blog I’ll do everything in Java, where you can process events using one of the following techniques:

1. Create an instance of an anonymous class overriding its handle() callback method. Pass it to the the event handler for a specific event.

2. Use lambda expressions.

3. Use Java method references introduced in Java 8.

The Sign In window, will have the buttons Sign In, Cancel, and the hyperlink Forgot password. Each of these controls will use different way of handling click event. The Sign In window will look as follows:

f1807

The event handler for the button Sign In will be implemented using an anonymous inner class. The event handler for the Cancel button will be implemented using a lambda expression. I’ll implement the click handler for the hyperlink Forgot password using a method reference. The code of the class GridPaneSampleEvents is shown next (see the section marked as event handlers).

public class GridPaneSampleEvents extends Application {

    public void start(Stage primaryStage) {
        
        Label userIdLbl = new Label("User ID:");
        TextField userIdTxt = new TextField();
        Label userPwdLbl = new Label("Password:");
        PasswordField userPwdTxt = new PasswordField();
        Button signInBtn = new Button ("Sign In");
        Button cancelBtn = new Button ("Cancel");
        Hyperlink forgotPwdLink = new Hyperlink("Forgot password");

        GridPane root = new GridPane();
        root.setVgap(20);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);
        
        // Using static methods for setting node constraints 
        GridPane.setConstraints(userIdLbl, 0, 0);
        GridPane.setConstraints(userIdTxt, 1, 0);
        GridPane.setConstraints(userPwdLbl, 0, 1);
        GridPane.setConstraints(userPwdTxt, 1, 1);
        GridPane.setConstraints(signInBtn, 0, 2);
        //Cancel button: span 1, right aligned
        GridPane.setConstraints(cancelBtn, 1,2, 1, 1, HPos.RIGHT,
                                                      VPos.CENTER);
        GridPane.setConstraints(forgotPwdLink, 0, 3,2,1);

        root.getChildren().addAll(userIdLbl, userIdTxt, userPwdLbl, 
                     userPwdTxt,signInBtn, cancelBtn, forgotPwdLink);
            
        // event handlers
        //1. Anonymous class 
        signInBtn.setOnAction(new EventHandler(){
            public void handle(ActionEvent evt){
              System.out.println(
                      "Anonymous class handler. Sign in clicked.");   
            }
        });
        
        // lambda expression
        cancelBtn.setOnAction(evt -> 
            System.out.println("Lambda handler. Cancel clicked.")
        );
        
        // method reference
        forgotPwdLink.setOnAction(this::forgotPwdHandler);
        
        // Show the window
        Scene scene = new Scene(root,250,200);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
    
    private void forgotPwdHandler(ActionEvent evt){
        System.out.println(
              "Method reference handler. Forgot password clicked");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

If you run this program, and click on Sign In, Cancel, and Forgot password, the console output will show the following:

Anonymous class handler. Sign in clicked.
Lambda handler. Cancel clicked.
Method reference handler. Forgot password clicked

While each of the event handlers works the same, I prefer the lambda expression version as it’s concise and is easy to read. Each of the JavaFX GUI controls has a set of setOnXXX() methods (e.g. setOnAction(), setOnMouseMoved() et al) that should be called for the events you’re interested in handling.

Properties and Binding

While Java developers casually use the words properties referring to class attributes, JavaFX properties are more than just class attributes. JavaFX defines an interface javafx.beans.property.Property, which has a very useful functionality allowing to bind the GUI components (the view) with properties of the Java classes (the model) and automate notifications of the GUI components when the value in the model change or visa versa.

Imagine that you’re developing a financial application that receives notification from the server about the stock price changes. When a Java object receives the new price, you need to modify the content of the corresponding GUI component. With JavaFX you can simply bind a property price of a Java class to the property of, say Label component. No more coding required. As soon as the price value changes, the Label will be automatically updated. JavaFX properties greatly simplify the process of synchronization of the data and the GUI.

Existing implementations of the Property interface serve as wrappers to Java attributes adding the change notification functionality. The interface Property declares the following methods: bind(), unbind(), bindBidirectional() , unbindBidirctional(), and isBound(). Can you bind any value to a JavaFX property? No – the value has to be of an ObservableValue type.

JavaFx property classes are located in the package javafx.beans.property. For each property type there are two classes: read-only and read-write ones. For example, if you need a String property, use either SimpleStringProperty or ReadOnlyStringWrapper. Both of these implement StringProperty interface. Similarly named classes exist for other data types and some collections too.

Let’s modify the GridPaneSampleEvents class. I will place an additional Label components at the bottom of the Sign In window. It’ll display the messages about the events as the user clicks on the buttons and the hyperlink. Initially this label will not have any text:

Label messageLbl = new Label();

JavaFX properties are observables. Hence we can add a listener (observer) to the property to be notified when the property value changes. But it’s much easier to simply use property in a binding expressions. I’ll bind this label to the string property, and as soon as the value of this property changes, the label component messageLbl will display this value.

private StringProperty message = new SimpleStringProperty();
messageLbl.textProperty().bind(message);

The class GridePaneSampleEvents was just printing messages on the system console when the user clicked on the buttons or the hyperlink. The new class GridPaneSampleBinding will modify our property message instead, for example:

cancelBtn.setOnAction(evt -> message.set(“Cancel clicked.”));

The click on the cancelBtn changes the value of the the message property, which was bound to the label’s text – the GUI will change automatically! This is how our Sign In window will look like after pressing the Cancel button.

f1808

The complete code of the GridPaneSampleBinding class is shown next.

public class GridPaneSampleBinding extends Application {

    //Declaring a JavaFX property
    private StringProperty message = new SimpleStringProperty();
    
    public void start(Stage primaryStage) {
        
        Label userIdLbl = new Label("User ID:");
        TextField userIdTxt = new TextField();
        Label userPwdLbl = new Label("Password:");
        PasswordField userPwdTxt = new PasswordField();
        Button signInBtn = new Button ("Sign In");
        Button cancelBtn = new Button ("Cancel");
        Hyperlink forgotPwdLink = new Hyperlink("Forgot password");
        
        // A label to display messages using binding
        Label messageLbl = new Label();
        // binding the StringProperty to a GUI component
        messageLbl.textProperty().bind(message);
        
        GridPane root = new GridPane();
        root.setVgap(20);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);
        
        // Using static methods for setting node constraints 
        GridPane.setConstraints(userIdLbl, 0, 0);
        GridPane.setConstraints(userIdTxt, 1, 0);
        GridPane.setConstraints(userPwdLbl, 0, 1);
        GridPane.setConstraints(userPwdTxt, 1, 1);
        GridPane.setConstraints(signInBtn, 0, 2);
        
        //Cancel button: span 1, right aligned
        GridPane.setConstraints(cancelBtn, 1,2, 1, 1, 
                                      HPos.RIGHT, VPos.CENTER);
        GridPane.setConstraints(forgotPwdLink, 0, 3,2,1);
        
        // Message label: span 2
        GridPane.setConstraints(messageLbl, 0,4,2,1);

        root.getChildren().addAll(userIdLbl, userIdTxt, userPwdLbl,
          userPwdTxt,signInBtn, cancelBtn, forgotPwdLink, messageLbl);
        
        // event handlers
        //1. Anonymous class 
        signInBtn.setOnAction(new EventHandler(){
            public void handle(ActionEvent evt){
                  message.set("Sign in clicked.");   
            }
        });
        
        // lambda expression
        cancelBtn.setOnAction(evt -> 
           message.set("Cancel clicked.")
        );
        
        // method reference
        forgotPwdLink.setOnAction(this::forgotPwdHandler);
        
        // Show the window
        Scene scene = new Scene(root,250,220);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void forgotPwdHandler(ActionEvent evt){
        message.set("Forgot password clicked");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The binding can be bidirectional. If the value of the GUI component changes it can change the value of the underlying model (remember MVC?), and if the value of the model changes the GUI is updated too. If you want to stop binding at any time, use the method unbind().

That’s all folks for now.

Written by Yakov Fain

August 11, 2014 at 2:32 am

Posted in java

Why Java Applets should be banned

with 4 comments

During my training classes on Web development with Java I used to ask students this question, “Do you think it would be a smart move if Amazon.com would create their online store using Java Applets?” The right answer is “Yes, if their goal is to go out of business.”

While back in 1995 Java was introduced to the world with the help of Applets (remember that dancing Duke?) this technology was buried by Microsoft (remember that law suit between Sun and Microsoft back in 1998)?

Web applications written with Java Applets would require the user to have the Web browser supporting specific JRE required by the applet. If in the past I was joking that a truck driver from Alabama would not be able to install the proper JRE on his computer, now I have to admit that I ran into a serious issues with this as well. Here’s my story.

I need to run a Webinar for O’Reilly on Tuesday. This will be a second one. The first one was three months ago. When I was testing the software for presenters, I was told not to use Chrome browser, cause they use Web conference software from on24.com, which doesn’t support Chrome. Why? Because they use Java for the Web client. Fine. I started the Firefox, installed the Java plugin and was able to run the Webinar. Disclaimer: three months ago I was using Java 7 under MAC OS X.

Today I use Java 8. I decided to practice with On24 software for the upcoming seminar and got Java errors. Sent a stack trace to O’Reilly, they contacted On24, and I got an answer: uninstall Java 8 from your computer and install Java 7. On24 Webinar platform doesn’t support Java 8, which was released six months ago. WAT?

But I don’t want to uninstall/reinstall Java just to run a Web conference. It’s 2014 for crying out loud! As a matter of fact I have both Java 7 and 8 installed on my MAC OS 1.9.4. Can I just configure my MAC OS to use Java 7? In the past, MAC System Preferences would allow to select the Java version – not anymore. Found a script to run during the boot of my Macbook to make it use Java 1.7. Great, it worked, but only for applications – typing java -version in the Terminal window properly reported Java 7. Unfortunately Firefox uses its own Java plugin that still sees my Java 8, and the On24 applet still crashes. Firefox on MAC doesn’t offer installing older versions of the Java plugins for security reasons. Actually, replacing Java 8 JavaAppletPlugin.plugin from the Internet Plugins folder with the one from Java 7 will make Firefox happy, but the applet from On24 still doesn’t work properly.

I’m not a truck driver from Alabama, but I’m about to give up and will uninstall Java 8 just to run this Webinar. Hope this will help.

Amazon is not stupid. They would never develop a Web front end with Java applets. But On24.com should seriously consider modernizing their Web conferencing platform unless their goal is to go out of business.

Written by Yakov Fain

August 1, 2014 at 2:12 am

Posted in java, Web

Closures in Java With Lambdas

with 2 comments

While working on the second edition for my Java 24-hour Trainer book I’m re-writing some of the code samples to use lambda expressions. Today I was re-writing an example for wait/notify for the chapter on multi-threading. Beside illustrating the wait/notify, I used a closure in this example. Since Java closures are not well presented in the blogosphere, I decided to write a quick blog on the subject.

My goal was to write a program that starts a thread and waits for the notification from that thread until its execution is completed. When the main thread receives the notification from the second thread it continues processing.

In the traditional object oriented programming you’d create a class that implements Runnable with a constructor receiving an object to send notification to, for example:

class MarketNews implements Runnable throws InterruptedException{
    Object parent;
    
    MarketNews(Object whoToNotify){
        parent=whoToNotify;
    }

    public void run(){
       // Do something
       synchronized(parent){
           parent.notify();
       }
    }
}

Turning Runnable into a lambda expression and creating a thread is simple:

  Runnable mNews = () -> {
	// Do something
        // But who to notify????
  }; 
   
  Thread marketNews = new Thread(mNews, "Market News");
  marketNews.start();

But how to let the mNews know who to notify when this “Do something is done?” How to pass the reference to that parent thread? It’s not overly difficult if you understand the concept of closures and know that a method can return lambda expression.

In functional languages like JavaScript a closure is a nested function that knows the context where it was declared (I know, it’s fuzzy, just bear with me). In Java there is not nested function but you can return a lambda expression from a method:

private Runnable getMktNewsRunnable(){
   return () -> {//Do something};
}

Runnable mktNewsRunnable = getMktNewsRunnable();

After running the above code the variable mktNewsRunnable will store the text of the lambda expression that can be used for creating a thread:

new Thread(mktNewsRunnable, “Market News”).start();

But this version of lambda expression still doesn’t know who to notify. Let’s fix it:

private Runnable getMktNewsRunnable(Object whoToNotify){
   return () -> {
      // Do something
      // whoToNotify.notify();
   };
}

Runnable mktNewsRunnable = getMktNewsRunnable(this);

I’m using the fact that when the closure was created by the above return statement, it knew about the existence of the variable whoToNotify in the neighborhood! The rest is easy – just put the call to notify() into the synchronized block to make the Java compiler happy. Here’s the complete working example.

public class TestLambdaWaitNotify {

  private static Runnable getMktNewsRunnable(Object whoToNotify){
      
	return  () -> {
		 try{
	      for (int i=0; i<10;i++){
	       Thread.sleep (1000);  // sleep for 1 second
	       System.out.println( "The market is improving " + i);
	      } 
	      
	      synchronized(whoToNotify){
	          whoToNotify.notify(); // send notification to the calling thread	   
	       }
	    }catch(InterruptedException e ){
	       System.out.println(Thread.currentThread().getName() 
	                                        + e.toString());
	    }  
	};    
  }
	
	
  public static void main(String args[]){
    	
	 TestLambdaWaitNotify thisInstance = new TestLambdaWaitNotify();
	 
	 Runnable mktNewsRunnable = getMktNewsRunnable(thisInstance);
	 Thread marketNews = new Thread(mktNewsRunnable,"");
	 marketNews.start();
   
     
     synchronized (thisInstance) {
    	   try{
    		   thisInstance.wait(20000);  // wait for up to 20 sec
    	   } catch (InterruptedException e){ 
    		   e.printStackTrace();
    	   }
    	 }
     
        System.out.println( "The main method of TestLambdaWaitNotify is finished");
  }
}

The main thread will print the message “The main method of TestLambdaWaitNotify is finished” only after receiving the notification from Runnable or when 20 sec expires. Easy?

If you want to see how to write closures in JavaScript, I’ll be running a JavaScript Webinar for O’Reilly on Aug 5th.

Written by Yakov Fain

July 21, 2014 at 9:29 pm

Posted in java

Follow

Get every new post delivered to your Inbox.

Join 142 other followers