/blog/posts
Thursday, August 28, 2014
Thursday, August 4, 2011
Semicolon cancer
via Let's build a compiler
Tuesday, June 28, 2011
How to set alarm for console task
To set alarm for end of a task just put echo ^G after the command, for example try the following line in your terminal:
sleep 2; echo ^GSimilar technique works with the cat command.
Wednesday, May 25, 2011
Integer.valueOf(1) != 1
public class Bar {
static {
// ...
}
public static final Integer DEFINITELY_ONE = 1;
public static void main(String[] args) {
int zero = DEFINITELY_ONE;
System.out.println(one == 1);
}
}
Should be true, right? Not exactly, depends on what you have in the static block. Try to place this code for example:
try {
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
field.setInt(Integer.valueOf(1), 0);
} catch (Exception t) {
//...
}
Now because of autoboxing and integer cache we get false as Integer.valueOf(1) now returns zero.
Wednesday, April 27, 2011
Does overriding violate Liskov substitution principle?
Overriding breaks Liskov Substitution Principle (LSP) if you change any behavior defined by a base method. Which means that:
1. The weakest precondition for a child method should be not stronger than for the base method.
2. A postcondition for the child method implies a postcondition for the parent method. Where a postcondition is formed by: a) all side effects caused by a method execution and b) type and value of a returned expression.
From these two requirements you can imply that any new functionality in a child method that does not affect what is expected from a super method does not violate the principle. These conditions allow you to use a subclass instance where a superclass instance is required.
If these rules are not obeyed a class violates LSP. A classic example is when you have the following hierarchy: class Point(x,y), class ColoredPoint(x,y,color) that extends Point(x,y) and overridden method equals(obj) in ColoredPoint that reflects equality by color. Now if one have an instance of Set<Point> he can assume that two points with the same coordinates are equal in this set, which is not the case with the overridden method equals. Actually, there is just no way to extend an instantiable class and add an aspect used in equals/hashCode methods without breaking LSP.
Thus every time you break the principle you implicitly introduce a potential bug that reveals when invariant for a parent class that is expected by a client code is not satisfied. However, in real world often there is no obvious design solution that does not violate LSP, so one can use @ViolatesLSP annotation to warn a client that it is unsafe to use class instances in a polymorphic set or in any other cases that rely on the Liskov substitution principle.
Monday, March 21, 2011
Scala for Java developers
- Using own getters/setters instead of methods kindly generated by Scala
- Using null instead of Option
- For me it is easier to follow functional programming style encouraged by Scala when functions return non-Unit type
- It is also a good practice to use var only when it is really needed, for example for counters or some other store of the current program state
- Finally, Vector in Scala is not an outdated class as java.collections.Vector is, but a powerful Scala collection with "effectively" random access time and "effectively" constant time for insert/delete operations