Category: Programming

Why you shouldn't be concerned about optimization

When you were at the university or in any programming course, you–like me–were probably thought of the importance of optimization. In the real world though, you should strive for writing good code–possibly using bullet-proof Design Patterns–and defer optimization until later. Actually, you should optimize your code only if, after testing, you realize that optimization is strongly necessary. There are three aphorisms concerning optimization that you cannot but know:

  1. More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason, including blind stupidity. William A. Wulf

    Read more...

How to select any character across multiple lines in Java

You can do that using the following pattern in the compile static method of the java.util.regex.Pattern class. The pattern is (.|n|r)*? which means: any character (the .) or (the |) n or r. Zero or more times (the *) of the whole stuff.

Example: The following method strips the multiline comments (those between /* and */) from a string passed in and returns the resulting string:

 
import java.util.regex;

// ... other code 

// Strip multiline comments
public void deleteMultilineComments(String subject) {
  Pattern pattern = Pattern.compile("(/\\*(.|n|r)*?\\*/)");
  Matcher matcher = pattern.matcher(subject);
  return matcher.replaceAll("");
}

Note: rn works for Windows systems. n works for Unix-like systems. r works for Mac systems.

Read more...

How to include a .js file (a JavaScript file) within another .js file

Actually, there is no function you can call to include a JavaScript file within another one. This is the bad news. The good news is that you can use a trick to accomplish this task. Suppose you have two JavaScript files, say file1.js and file2.js. You want to include file1.js within file2.js because you want to use file1’s functions. Here is the code you can use:

 
//this is file2.js
document.write("<script src='file1.js' type='text/javascript'></script>");

// here you can use functions defined in file1.js

As you can see it suffices “to write” in the browser the code you generally use to include a JavaScript file within an HTML file and you’re done!

Read more...

Java passes EVERYTHING by value!

It’s a common misconception thinking (or worse teaching!) that, in Java, primitives are passed by value and objects by reference. Actually, everything in Java is passed by value as well as object references.

When a parameter is passed by value, an actual copy of it is really passed so that any change made will have only a local effect. For example:

   
public static void main(String[] args) {
  int a = 0;
  increment(a);
  System.out.println(a); //it prints 0 so increment didn't work as expected
}

public static int increment(int a) {
  ++a;
  System.out.println(a); //it prints 1
  return a;
}

As you can see the change made to the parameter passed to increment, that is a, affects only the local copy of it. This proves that Java passes primitives by value.

Read more...