2012-06-30
http://www.noop.nl/2012/06/managers-should-not-be-coaching-developers.html
Good hint about what managers should actually manage and what they shouldn't.
http://www.forbes.com/sites/frederickallen/2012/06/27/inside-the-new-deskless-office/
Old, but great idea for better office rooms.
http://www.javacodegeeks.com/2012/06/demeter-law.html
A simple, but powerful law. Hopefully well known and widely used :-)
http://css.dzone.com/articles/no-browser-left-behind-html5-0
Really good article about a cautious switch to HTML 5.
http://glazkov.com/2011/01/14/what-the-heck-is-shadow-dom/
An older article, but I needed it for a different topic, and this article is still excellent, so give it a try, please.
http://css.dzone.com/articles/top-10-css3-forms-tutorials
http://css.dzone.com/articles/mastering-css3-7-cool-text
Some CSS 3 tutorials and samples.
2012-02-04
Real Story: One of my favorite pieces of code (including advanced tutorial)
This is a real piece of code I found some time ago and I a still think, it has some great internal greatness, so I decided to publish some kind of a special tutorial about it:
1 public boolean falseBoolean( boolean trueValue ) { 2 boolean returnValue = false; 3 if ( trueValue == true ) { 4 returnValue = false; 5 } 6 else { 7 returnValue = true; 8 } 9 return returnValue;10 }
So why we don't take the code and analyze the important Java idioms and rules one can find there, and maybe we can also improve it a little bit.
First it is a good idea to change names accordingly: the name of the method variable 'trueValue' is probably not so good, because one might think that this variable always contains a true value, or if there are some 'wrong variable' somewhere, so I will change this to 'value' which is quite neutral.
Also 'returnValue' is a little bit long and it might confuse to have all variable names containing 'value', so I change this to the shorter name 'result'.
Then there is the idea that variables should be final, what actually means that they are not real variable, but fixed, but let us ignore this tiny point now.
If we change the local variable to final, we are not allowed to assign anything to it in line 2, because otherwise we would not be able to assign a new value to it in lines 4 and 6.
So here is the slightly improved code:
1 public boolean falseBoolean( final boolean value ) { 2 final boolean result; 3 if ( value == true ) { 4 result = false; 5 } 6 else { 7 result = true; 8 } 9 return result;10 }
You see the code confirms to an old rule: "Not more than 1 return statement in a method". Well, to be quite direct, let me say that I don't like that rule. It does not make sense for me, because there might be of course some situations where it is OK, to return from a method in the middle of the method. I really think that it depends on the context or on the coding conventions you have to use at your office.
Let's assume that I can do what I want..., so I will use two return statements here, so that the method is quite shorter. In order to do so, we will remove the local variable.
1 public boolean falseBoolean( final boolean value ) {2 if ( value == true ) {3 return false;4 }5 else {6 return true;7 }8 }
Another small issue is that I do not like comparisons of the kind 'a == true', where a is a boolean variable. So I change this, too.
1 public boolean falseBoolean( final boolean value ) {2 if ( value ) {3 return false;4 }5 else {6 return true;7 }8 }
As I like to use what Java offers, I also like the ternary (conditional) operator. Some people do not like it, because the code might be more difficult to read then, but I think one can use it for short statements, as follows:
1 public boolean falseBoolean( final boolean value ) {2 return value ? false : true;3 }
I hope, everybody got that last change...
Now we should check how this method is actually called, maybe somehow like as follows (ignoring that it is a static method and should perhaps have the class name before the method name):
1 boolean dump = ...;2 3 boolean garb = falseBoolean( dump );
Here dump gets a boolean value somehow and later the new value is assigned to garb by calling our optimized method.
As the method is quite short, one might consider inlining the contents of the method. Then one would have:
1 boolean dump = ...;2 3 boolean garb = dump ? false : true;
Yes, this is it...
This is my final version for all the programmers who have never heard of the ! operator.
I thank you for your attention.
In my next post I will show you how to convert a long value to an int value. Stay tuned.
</sarcasm>
2012-02-01
I don't like NullPointerExceptions
Actually I don't like the message of NullPointer Exceptions, i. e. "null". Such a message is usually not very helpful.
I created a few rules for myself how to avoid certain exceptions and actually define pre- and post-conditions for methods. Let me please give you a few examples.
In the beginning of a non-private method I usually check all method arguments.
public void foo( String x, int a, Person p ) {
if ( x == null ) throw new IllegalArgumentException( "Argument 'x' must not be null." );
if ( a < 0 ) throw new IllegalArgumentException( "Argument 'a' must not be less than 0." );
if ( p == null ) throw new IllegalArgumentException( "Argument 'p' must not be null." );
A few hints:
There are of course libraries which do such checks, i. e. they can check if a String is not null and not empty, and if it contains only white spaces and so on.
I would recommend to choose one of those libraries and use them intensively.
Otherwise some checks like in the example might be helpful.
There are also libraries which offer annotations for methods or method arguments to check if the method arguments are valid. Those libraries are great in my opinion, but often companies do not allow the use of some libraries for whatever reasons.
You can create your own Exception type or use an existing one. I have seen very often that developers use IllegalArgumentException, IllegalStateException, or NullPointerException. In the later case with a good error message...
These checks should not only be done in methods, but also in constructors, and whereever it makes sense for you.
For private methods I use assert statements. Remember assertion checking can be enabled and disabled by the command line options -ea and -da.
private void gee( Person p ) {
assert p != null;
I usually do not add messages (too much work) to assertions. As we usually compile everything with the debug flag it is easy to find the line where the assertion error happened.
In the if statements from above or in such assert statements I always check only one thing.
That means that the following code is not OK for me:
assert p != null && p.getName() != null && !list.contains( p );
I prefer:
assert p != null;
assert p.getName();
assert !list.contains( p );
Before I return a value in a method, I check the value (post-condition) like in:
public String concat( String a, String b ) {
...
final String result = a + b;
assert result != null;
return result;
You can also add assert statements to various places within a method like:
public void addPerson( Person p ) {
...
final int previousSize = list.size();
list.add( p );
assert list.size() == previousSize + 1;
assert list.contains( p );
Remember that this code is not included in the class files, if you use the -da option, so there is no performance problem in production software, but those assertions can be great during development to find problems.
And they are a very good way to document assumptions. That is actually the most important point for me: I like executable documentation...
In addition I always check a reference before I call a method of if:
y = x.getName().toUpperCase();
will become:
if ( x != null and x.getName() != null ) {
y = ...
Or one can also use assertions here, that depends on the context.
For me it is not so important which technology is used for all those checks, but that those checks are done at all.
I think it is a very important and actually very simple step toward increasing quality of software.
2012-01-31
Most software developers seem to use a directory called 'trunk' in Subversion. I always wondered what trunk is good for.
In real life software development in a team of, let's say, about 20-30 developers some coordination is needed when making a release version.
Often this happens as follows (and I am quite simplifying):
- All developers commit their stuff to the trunk.
- The trunk is copied to a release branch, e. g. RB-2.
- The new release (with 3) starts on the trunk.
- Every developer has to check out the version he needs for further programming, either the trunk for the new release, or the release branch.
"Where can I commit my stuff for the current release? On the trunk or...?"
Usually another developer answers slightly irritated: "On the relase branch, of course, we created it yesterday."
- "Ah, sorry, I missed that somehow."
2011-12-30
A good hint is
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6538311
Just search for the part with system-config-securitylevel.

You can have a FREE account which gives you 10 GB of FREE storage which is twice as much as on Dropbox. The disadvantage: the background synchronization seems to be slower than Dropbox, but I can life with that. I don't care if it takes 2 or 2.5 days...
I use such free online services (there are many more than mentioned here) for transferring my files between several computers, and for backup of certain, incredibly important files.
I can recommend both :-)
My opinion about 'The Clean Coder'
It is a very important book for software developers, even if it does not contain any new ideas at all.
The important point is that Mr. Martin emphasizes "professional" software development, and he wants us to behave and sell ourselves as professionals. That's quite OK for me, I like that, but I don't need to read a complete book for figuring this out:-)
Mr. Martin touches several topics about so-called professional software development like testing, estimating, collaborating, and so on.
A typical chapter consists of some stories of Mr. Martin's own experience as a programmer. Those stories usually show what went wrong, so that one can learn from them. After having read so many bad stories I started to wonder, if Mr. Martin ever had a successful software project, but let's assume so...
Then there are some instructions for developers what to do and what not.
If you are a not so much experienced software developer:
- Read it, learn it, try it, code it, quote it.
If you are an experienced software developer:
- This book probably does not help you, it does not contain anything new, just old stuff, extended by unnecessary personal stories by Mr. Martin.
A ridiculously poor (aka uninformative) appendix mentions some 'tools' like continuous integration software.
Some hints for calculations with floating point numbers
Here are some (slightly simplifying) hints or rules for the usage of BigDecimal.
- Use
BigDecimal.valueOf(double)ornew BigDecimal(String)to create BigDecimal objects.
Never usenew BigDecimal(double), unless you know what you are doing. Be aware that 0.1 is an infinite decimal number, 0.5 is not, sonew BigDecimal(0.5)is fine, but notnew BigDecimal(0.1). And if you do not want to think about the difference every time, do not usenew BigDecimal(...)at all. - If you want to compare BigDecimal objects, be aware: equals() compares the value and the scale, that means 1.001 is not equal to 1.00100. Use signum() and compareTo().
- Use setScale to round the BigDecimal value.
- Use a Context for rounding.
- Be aware that setScale or a division may throw an ArithmeticException, so always use a RoundingMode or a RoundingContext.
package testdrive;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import org.junit.Test;
public class DoubleCalcTest
{
@Test
public void addDouble()
{
/**
* This is actually a very old example which shows that calculations with double values can lead to unexpected
* results.
*/
double a = 0.1;
double sum = 0;
for ( int i = 0; i < 10; i++ )
{
sum += a;
}
// sum should be 1, but it isn't
assertTrue( sum != 1 );
/**
* There is no exact binary representation for 0.1, so repeated calculations with 0.1 will probably have wrong
* results.
*/
}
@Test
public void createBigDecimalCorrectly()
{
/**
* This shows the usage of the static valueOf method. In such a way, a BigDecimal object can be created from a
* double value. Another possibility is the constructor with the String which is shown here just for fun. I would
* recommend to always use the static valueOf method.
*/
BigDecimal a = BigDecimal.valueOf( 0.1 );
BigDecimal sum = new BigDecimal( "0" ); // Just to show this possibility.
// OR: BigDecimal sum = BigDecimal.ZERO;
for ( int i = 0; i < 10; i++ )
{
sum = sum.add( a );
}
BigDecimal expected = BigDecimal.valueOf( 1.0 );
/**
* If the two BigDecimal objects may have different scale, then use signum() and compareTo().
*/
assertTrue( expected.signum() == sum.signum() );
assertTrue( expected.compareTo( sum ) == 0 ); // == 0 means, they are equal.
}
@Test
public void createBigDecimalInAWrongWay()
{
/**
* Never use the constructor with double values, unless the binary representation of the double values is what you
* want. So actually it is better to use the constructor with a String or the static value of method.
* This example shows the problem with the constructor which has a double as parameter.
*/
BigDecimal a = new BigDecimal( 0.1 );
BigDecimal sum = new BigDecimal( 0.0 );
for ( int i = 0; i < 10; i++ )
{
sum = sum.add( a );
}
BigDecimal expected = new BigDecimal( 1.0 );
/**
* The value of the expected variable (i.e. 1.0) is not the same as the value of the sum variable. The reason is
* that there is no exact representation of 0.1 in the binary system, so the BigDecimal does not represent 0.1 but
* something similar.
*/
assertFalse( expected.equals( sum ) );
/**
* If the two BigDecimal objects may have different scale, then use signum() and compareTo().
*/
assertTrue( expected.signum() == sum.signum() );
assertTrue( expected.compareTo( sum ) != 0 ); // != 0 means, they are different.
/**
* A hint to JUnit:
*
* If you use JUnit, then be aware that assertEquals does use the Number.longValue() method to compare BigDecimal
* objects, so the result is often wrong. So please use the assertTrue or assertFalse methods.
*/
assertEquals( expected, sum );
}
}
2011-07-01
2011-06-29
The Cutter Blog » Blog Archive » The Truth About Technology Management
2011-05-13
I became digitally distinct
Try it, it's nice :-)
.
2011-04-13
JSR-346 for new CDI 1.1
It contains improvements for CDI 1.0, and is lead by Red Hat.
CDI might be the most helpful new piece of technology in Java EE 6. If you haven't heard of it or didn't use it before, then try Adam Bien's articles or the documentation of CDI's reference implementation, called Weld, which is actuaaly quite easy to read.
So what are the suggestions for CDI 1.1? Let's look what the JSR says:
- "Global ordering of interceptors and decorators, as well as global enablement of alternatives [CDI-48]
- An API for managing built in contexts, allowing the built in implementation of the conversation context to be used outside of JSF [CDI-30]
- An embedded mode allowing startup outside of a Java EE container [CDI-26]
- Declarative control over which packages/classes are scanned in a bean archive [CDI-87]
- Bean declaration at constructor level [CDI-55]
- Static injection [CDI-51]
- Inclusion of @Unwraps from Seam Solder [CDI-89]
- Align with current version of @Inject [CDI-51]
- Numerous minor enhancements to the Portable Extensions SPI
- Client controlled contexts allowing for SaaS style multi-tennancy [CDI-103]
- Better support for CDI in libraries when used in the Java EE platform [CDI-84]
- Send CDI events for Servlet events [CDI-38]
- Application lifecycle events [CDI-86]"
The final release of CDI 1.1 is currently scheduled for Q3 2012.
.
2011-04-07
NMO
It nearly killed me within the first hours, but some good people in the hospital saved my life. I was lucky, because I was in a hospital at that time (for some other reasons).
I spent a month in the intensive care unit of the hospial, sleeping a lot, dreaming bad dreams, and fighting for my life.
Then I spent two long months in the monitoring station, struggeling with my blood circuit and other things. After another month in a regular station, I finally moved to the rehabilation center called Weisser Hof.
I cannot feel and move my legs anymore, so I am learning to use a wheel chair.
My body becomes stronger now, and I feel more energy, so I try to come back to a regular life.
.
2010-01-17
How to change the Maven directory structure for compatibility with MyEclipse web projects
<build>
<sourcedirectory>${basedir}/src</sourcedirectory>
<outputdirectory>${basedir}/WebRoot/WEB-INF/classes</outputdirectory>
<resources>
<resource>
<directory>${basedir}/src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<webappdirectory>${basedir}/WebRoot</webappdirectory>
<warsourcedirectory>${basedir}/WebRoot</warsourcedirectory>
</configuration>
</plugin>
This does not take into account special resources directories, but they can easily be added by the resources element.
2009-08-23
JavaScript: Introduction to closures
This is an introduction to closures in JavaScript. It is intended for JavaScript beginners and belongs to a small series of (future) blogs for JavaScript beginners.
Closures are an important concept in modern programming languages, and one should be able to understand and use them well.
Closures are functions which reference (i.e. use) variables that are defined outside of those functions. This is not an exact definition, but it will help for the following examples.
In JavaScript, closures are usually realized as functions which are defined of other functions.
Have a look at the following code:
1: function foo() {2:3: function gee() {4: return 1;5: };6:7: return gee();8: }9:10: print( foo() );
The function foo contains an inner function gee. The ability of a function to have inner functions is an interesting point. In other languages like in Java that is not possible. gee is our first closure here.
The function – or the closure - gee returns the number 1, and the method foo returns the value, that gee returns, i.e. 1. So far nothing special…
By the way, the print method at the bottom of the code, is a method of the Aptana JavaScript library. You can download the Aptana Studio here.
The output of the code above is
| 1 |
Now check this code:
1: function foo() {2:3: var a = 2;4:5: function gee() {6: return a;7: };8:9: return gee();10: }11:12: print( foo() );
| 2 |
This time the method gee uses a variable which has been defined outside of gee, but inside of foo. This is an important feature. The variable a is not a global variable, because it has been defined by using the keyword “var”, but the scope of visibility of this variable contains everything inside of the foo method, and sowith the function gee.
Please keep in mind that from within an function which is defined inside of another function, all variables which are defined in the outer function can be used.
1: function foo() {2:3: var a = 2;4:5: function gee() {6: return a;7: };8:9: a = 10;10:11: return gee();12: }13:14: print( foo() );
| 10 |
This time the variable a is defined, then gee, then the value of a is changed. What happens here, is that gee gets a reference to the variable a, so the value 2 is not copied somehow within the function definition of gee (then 2 would be printed), but gee gets a reference to the variable a, so 10 is printed.
1: function foo() {2:3: var a = 2;4:5: function gee() {6:7: var b = 3;8:9: function hii() {10: return a * b;11: }12:13: return hii();14: };15:16: a = 10;17:18: return gee();19: }20:21: print( foo() );22:
| 30 |
Now we have three functions which are somehow combined. The same principles as above work now, so the function foo and therefore hii return 3 times 10.
The following example shows that an inner function can also access the parameters of the outer function.
1: function foo( a ) {2: return function() {3: return a * 2;4: };5: }6:7: print( foo(1) );8: print( foo(1)() );
And one can see that foo(1) returns what comes after the return statement, i.e. the anonymous, inner function. As it is common in JavaScript, the toString method of a function returns the source code of that function. And as foo(1) is a function, we can call this function by adding (), so that we have foo(1)(), which generates the output “2”.
| function () { |
Now we have two inner functions (or closures), so that we can call the inner-most function with three parentheses: foo(1)()(). That means, 1 is given to a, then the first inner function is called which has a local variable b, and finally the inner-most function is called which returns the result 6.
1: function foo( a ) {2: return function(){3: var b = 3;4: return function(){5: return a * 2 * b;6: };7: };8: }9:10: print( foo(1)()() );11: var f = foo(1);12: var g = f();13: print( g() );
| 6 6 |
One can also see that one can assign a function to a variable, like f, then call the function by f(), and assign this to a variable, like g, which then can be called by g() - nothing very complicated, just a little bit unusual for people who come from other languages like Java.
The following code example shows a function foo which returns a closure. That closure is again returned to a variable with a more meaningful name as in the examples above.
1: function foo( a ) {2: return function( b ) {3: return a * b;4: };5: }6:7: print( foo(2)(5) );8:9: var doubleIt = foo(2);10:11: print( doubleIt( 1 ) );12: print( doubleIt( 2 ) );13: print( doubleIt( 3 ) );
This example returns the following numbers, i.e. twice the value of the input.
| 2 4 6 |
The interesting point is that one can give the closure foo(2) a new and more meaningful name, so that doubleIt seems to be like a new function which can used in the program later.
It is like converting a given API to something more specific and more useful in the current application or the current context.
The following example shows this again with a function that simply calculates the sum of two numbers, but implemented by a closure.
1: function add( summand1 ) {2: return function( summand2 ) {3: return summand1 + summand2;4: }5: }6:7: var threeAnd = add( 3 );8: var fourAnd = add( 4 );9: var fiveAnd = add( 5 );10:11: print( threeAnd( 2 ) ); // 512: print( fourAnd( 2 ) ); // 613: print( fiveAnd( 2 ) ); // 7
The output is of course:
| 5 6 7 |
The three closures have meaningful names which say what the closure is going to do. In such a way you can easily create new "functions" based on already existing functions or closures.
Technorati: javascript, closure, closures
2009-08-13
Java: char to int
Today I found some code (in a current project of a company), where they convert a Java char to an int like this:
int x = Integer.parseInt( String.valueOf( c ) );where c is a char like '0', '1', ... (i.e. a digit as a char).
I would suggest to make a conversion like:
int x = c - '0';
That's easier and probably faster...
2009-06-30
Top 200 blogs for developers
This is a good and helpful list, I'm going through it, making bookmarks, checking what would be helpful for me.
http://www.noop.nl/2009/06/top-200-blogs-for-developers-q2-2009.html
My love from Iran
My girl-friend is originally from Iran. She came to Austria when she was 18 years old. She came here alone, learnt German for one year, and then studied Information Technologies. I think that was a very hard and brave action. And it must have been some kind of a culture shock.
Today, after so many years, it is the first time that her parents come to Austria.
It was not so easy to invite them. I did not know about it, but it is quite difficult here in Austria to invite people from certain other countries, so that they are allowed to see Austria for a while. Actually the austrian embassy in Iran has to give the allowance to visitors, but they said no to the parents for my girl-friend. As I heard, the current people in the embassy suck a lot, so we thought we would have no chance, but my girl-friend called the Austrian ministry of foreign affairs and started to make a complaint. Suddenly, on the next day, the embassy called my girl-friend's parents and told them that they may go to Austria. So much about making complaints, sometimes they help.
For me my life changed very much because of my girl-friend. She is from another culture, and she sees many things in a different way than I do. And I learnt a lot about Iran.
Some time ago, I had thought that most people in Iran were muslims and convinced of their religion and some kind of fanatic. Then I was invited by so many iranian people living here in Austria, so that I had the chance to learn more.
First of all there seem to be two worlds in Iran: the official one of the government and the islam, and then the private one. One can see it on the photos. When women are walking on the streets, they have headscarfs, when they are in the house, maybe on a party, they have no headscarfs, but miniskirts. It's the opposite, very closed in the public, very open in the private area, somehow. In the beginning it confused me.
People from Iran are very hospitable, and they love their country, they are proud of it. I have the feeling, that it is sometimes difficult to find things where they can be proud of. They often talk about history, and how powerful Persia once was. But now it is not anymore.
And they seem not to like Turkish or Arabian people...
Iran nowadays is a lost country. It has a regime which surpresses the intelligent people of the country, so that more and more of them leave the country. And most of the people are very poor, and I read, that 70% of the people are younger than 30 years old.
This last fact gives me some hope. The young people like the Internet, they like Twitter, and so on. They are open-minded and curious to learn new things. Maybe there is a chance that they will change their regime some day.
We have to hope for this, because the Iran could be a wonderful country to live with a lot of great people.
2009-06-29
I'm not a fan of spending too much time on the computer, even though I SPEND a lot of time sitting in front of this strange device. Nevertheless I was curious what Twitter really is and registered myself there.
Well, mot of the talks are not interesting for me, but when I search for java, javascript, jquery, or similiar topics I get a huge number of interesting articles.
This actually changed my life.
I got more links to exciting and interesting home pages than ever before, and I'm reading and learning new things now all the time. I'm quite thankful for this possibility.
I use the Xmarks plugin for Firefox where one can share one's bookmarks between the PCs at the office and at home. I made a new structure of my bookmarks, so that I can easily find things again, and I add the most helpful links I found on Twitter to my bookmarks quite often.
I am also impressed about the tweets about the election in Iran (#iranelection), there were so many people who were shocked by the behaviour of the iranish government, it was good to hear or read them. My Twitter icon is still green as a sign against the current actions of the government of Iran.
