Pages

Thursday, November 27, 2014

How to Avoid Any Global Variables in JavaScript



In JavaScript, objects and functions, are also variables. And any variable declared outside a function has global scope. You probably heard that because JavaScript is a dynamically type language global variables are bad and that they easily become source of confusion and bugs. So how can we avoid of creating any global variable, but still have our code executed?
There is such technic, it's called Immediately Invoked Function Expression (IIFE).

Suppose we have this JavaScript code:

var func = function(){
  
  console.log("entering func");
  
  var counter = 0;
  
  var innerFunc1 = function(){
    counter += 1;
    console.log("innerFunc1: " + counter);
  }
  
  var innerFunc2 = function(){
    counter += 1;
    console.log("innerFunc2: " + counter);
  }
  
  return {
    inner1: innerFunc1,
    inner2: innerFunc2,
  };
}

var f = func();
f.inner1();
f.inner2();

So here we have two global variables - func and f.
Now I'll show how to run this code without creating ANY global variable by applying IIFE.

Monday, November 3, 2014

WPF Customized ComboBox with Non Existing Value



How do you show a non existing value inside a WPF ComboBox? Good question! And the sad truth is that you just cannot do it! WPF ComboBox shows only the values that are in its ItemSource. 

But don't worry, today I'll show you a workaround to this problem. In this particular example inside a ComboBox will be shown a phantom value and when user clicks on it and choose another value from the list he will be prompted to confirm in order to give him a chance not to loose this phantom value in case if he clicked on the ComboBox by mistake.

WPF Customized ComboBox with Phantom Value

The trick is that I create additional custom ComboBox with TextBlock, binding it to the original one and overlaying it.

Thursday, March 20, 2014

Java 8 Lambda Expressions Example

Finally Oracle officially presented Java 8. A key feature of Java 8 - Support for lambda expressions that allow developers to effectively apply the simultaneous computation and callback functions in programming, especially in the popular cloud applications. Function can be treated as method arguments, and code as data, which makes the final results more compact. 

Java Lambda syntax is pretty similar to what we know in C#. 
Let's see.

Starting a new Thread:


new Thread(() -> { System.out.println("Java 8 Lambda"); }).start();


Working with Collections:


List<Employee> employees = new ArrayList<>();
     employees.add(new Employee("John"));
     employees.add(new Employee("Robert"));
     employees.add(new Employee("Anna"));


Iteration:


employees.forEach(e -> e.setAge(35));


Filter:


List<Employee> filtered = employees.stream().parallel().filter
                (f -> f.getName().equals("Anna") || f.getName().equals("John"))
                .collect(Collectors.toList());

Optional<Employee> findFirst = employees.stream()
                .filter(e -> e.getName().equals("Anna")).findFirst();

Parallelism:


List<Employee> list = employees.stream()
                .parallel()
                .filter(p -> p.getAge() > 18).collect(Collectors.toList());

Happy codding friends! :)

Sunday, March 16, 2014

Best Logging for Java - Logback with SLF4J



If you're looking for good logging mechanism for Java I suggest you to use SLF4J Facade with Logback. For my opinion it's the best combination for logging in Java. Today I will show how to configure your Java project in order to use SLF4J Logback logging framework to print logs to console and file output.

Logback with SLF4J

First, download SLF4J files and Logback files.
Add these files to your CLASSPATH:
  • slf4j-api-X.X.X.jar
  • logback-core-X.X.X.jar
  • logback-classic-X.X.X.jar

Now look how simple is to use Logback! In the code all you need is this:

public class LogbackFun {
  final static Logger logback = LoggerFactory.getLogger(LogbackFun.class);

  public static void main(String[] args) {
    logback.info("Test info");
    logback.debug("Test debug");
    logback.trace("Test trace");
    logback.warn("Test warn");
    logback.error("Test error");
  }
}

SQLite4Java Select, Update Example


Recently I started to work on some small Java project where I need to read and update data on SQLite data base. I searched for some good SQLite wrapper for Java and finally decided to use SQLiteJava wrapper. I found it more intuitive and faster than JDBC driver. Today I'll show how to use it in your Java application. 
SQLite4Java

First of all you'll need to download SQLite4Java wrapper files from here
Once you're done do this:


  • sqlite4java.jar put it on your CLASSPATH
  • sqlite4java-win32-x64.dll put in the same directory with the jar file
  • sqlite4java-win32-x86.dll put in the same directory with the jar file

  • I created SQLite4Java.java class which handles all my activities via SQLite file: