Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

2007-10-15

The naked object

Consider a forest with birds singing in the trees and flowers covering its floor. We caneasily walk
along its paths or you can be adventurous an make your own paths. We can
select any aspect of its complex
ecosystem and study it for your doctoral thesis. There is
unlimited complexity, yet any human can
master it to suit his or her purposes. There is no
reason why a computer system should be more
complex than a forest. I believe that the
current complexity is man-made, and that we can
resolve it by changing our approach to
software development. We merely need to get our priorities right and create
the appropriate
tools. If we decide to build systems for people, then we will get systems tha can be mastered by people.
Trygve Reenskaug In the foreword to Naked objects R. Pawson Phd Thesis


An approach offered by creators of Naked objects framework looks very interesting, at least for the quick prototyping. Refuse, auto generation of UI, return to the OO basis - full-fledged objects with considerable behavior.

A big advantage is that the interaction with user happens by the most usual noun-verb script. IMHO, only Raskin zoom interface is required to be absolutelly happy. And for the convenient navigation on great number of open objects something like Leap is needed.

In web implementation from the outside it's very similar to like Django admin interface interface, in full-fledged GUI clients interface is much more interesting.

Update 18.10.2007: Naked objects discussing at ltu
Update 29.10.2007: Martin Fowler's article with the critique of objects without behavior and a critical response to it in some blog.

2007-10-05

I believe

With simple, consistent code, change analysis is a breeze. A simple trace through the layers of code with a text-search tool will generally reveal exactly what needs to be changed, and where. A solid test plan will identify what changes broke the system and how, and allow developers to respond appropriately.

No, it’s not fun and not challenging. It’s just good software


http://de.worsethanfailure.com/Articles/The-Mythical-Business-Layer.aspx

2007-08-29

What the fun??

I do not understand.

Why in NUnit and in new Junt instead of simplest agreements of tests naming attributes and annotations are used. All the same, it's very difficult to give more suitable names to SetUp, TearDown and TestSomething methods.

It's hard to agree with an opinion that it's more clear in such a way
[SetUp]
public void SetUp() {}


Class attribute instead of the inheritance from TestCase is great. Now the free place of ancestor a class being tested can get. This is for a case of protected stuff is desired to be tested.

Yes, if in a base class suddenly there are methods which names start with Test and which are not tests themselves then indeed needed things can be selected only with explicit attributes. But this is such a rare case...

P.S. Many other things related to testing I consider are better to implement with attributed.

2007-04-06

Generic Visitor Combinators

It's simple and practical - Visitor Combination and Traversal Control.

The main idea is to separate specific code that executes concrete actions from the code that effects visits check.








IdentyDo nothing
Sequence(v1,v2)Sequentially perform visitor v2 after v1.
FailRaise exception.
Choice(v1,v2)Try visitor v1. If v1 fails, try v2 (left-biased choice).
All(v)Apply visitor v sequentially to every immediate subtree.
One(v)Apply visitor v sequentially to the immediate subtrees until it succeeds.


So, it's rather flexible to implement different actions by combine it and inherit from it.


TopDown(v) = def Sequence(v,All(TopDown(v)))
BottomUp(v) = def Sequence(All(BottomUp(v)),v)
IfZeroAddOne = def TopDown(Try(Sequence(IsZero,AddOne)))

2006-07-25

MFC vs WinForms vs SWT

All that you need to know to embed an Office application into:

MFC
http://www.rsdn.ru/article/com/xoffice.xml and http://www.rsdn.ru/article/com/autoatl.xml
two articles in about 10-12 pages - beat in a drum to adjust a project


WinForms
http://blogs.gotdotnet.ru/personal/k_savelev/PermaLink.aspx?guid=6d39ad7d-d1ae-4daf-8ce2-9b881ca7091a
A heap of links, ready external component and therefore a little headache when installing application to client


SWT
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/WordOLE.htm
a couple strings of code and everithing works from the box

2006-06-08

Setter returns this

I've spied in new Eclipse a new feature or even pattern of beautiful code.

Void is useless. When every expression has a value it's cool.

/**
* This class provides a convienient shorthand for creating and initializing
* GridData. This offers several benefits over creating GridData normal way:
* The same factory can be used many times to create several GridData instances
* The setters on GridDataFactory all return "this", allowing them to be chained
*/


For example:
// Example : Typical grid data for a wrapping label
// GridDataFactory version
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER)
.hint(150, SWT.DEFAULT)
.grab(true, false)
.applyTo(wrappingLabel);

// Equivalent SWT version
GridData wrappingLabelData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
wrappingLabelData.minimumWidth = 1;
wrappingLabelData.widthHint = 150;
wrappingLabel.setLayoutData(wrappingLabelData);

2006-05-13

Java: Memory leaks

For all C++ monstrosity it brainwashes yet.

About six months ago guys had a problem with j2me app. Run tests: 1st passed, 2nd raises OutOfMemory error. Change the tests running order — now the ex-first test raises OutOfMemory. I looked at this, but could not tell anything reasonable.

I've just remembered about it and it seems to me I've understood what the kind of trouble was that. The problem is exactly between the two lines:

class MemoryLeak {
static int[] buffer = new int[0];

public static void reinitialise(int maxsize) {
if(buffer.length < maxsize)
buffer = new int[maxsize];
}
}


The problem is that Java machine acts too straightforward. The first call of reinitialise is normally, during the second call at first it tries to allocate memory for a new massive and in the case of success the result is assigned to a variable. And only after that the memory to which buffer was referred will be available to Garbage Collector. As a result for a normal work it is required 2*maxsize of memory. The solution is to set buffer to null before the memory allocation.

But IMHO Java machine could take care of it. And in rare cases when double memory allocation is required it is better to be described explicitly.

public static void reinitialise(int maxsize) {
if(buffer.length < maxsize) {
int tmpbuf[] = new int[maxsize];
buffer = tmpbuf;
}

2006-03-04

The programming: 99 bottles in Drools

I've posted to www.99-bottles-of-beer.net my solution in Drools. It was accepted.
You can see it here

2006-01-21

Drools: Experimenting with «Jackal» game.

There is an old child's table game. Here http://zhurnal.lib.ru/g/glebowa_u_w/statja1.shtml an inspiration introduction can be read. Unfortunately, I have not found detail description of the game rules in the internet, but this is not so important — now I'm interested in its small subset:

The game takes place on a field consisting from 11x11 small square cells with turned over images. A player can sail his ship to any field edge and put ashore a sailor. A cell is turned over (is the image was hidden) as the sailor stand at it. Further actions depend on the cell image. I will not describe all cell types, I'll tell about 3 types, one of which roused me to program this game.

1. Empty cell — nothing happens
2. Arrow — a sailor automatically moves to a cell which the arrow is pointing at.
3. Reflection — a sailor moves to a cell, which is symmetric to its initial state.

For example:

2 3'
1 -> @ 4 1'
3 2'

where numbers are empty cells, «@» is the reflection card, well, the arrow is the arrow.

Sailor comed from cell 1 moved by the arrow to the reflection cells, which moves him to the cell 1'. Similarly for 2-2' and 3-3'. This is all rules for this cell kind, which were familiar to me, the rest special cases should be invented ;) . Since everybody has different ideas, the rules shold be programed so that they can be easyly changed later.

Here is my variant:
1. Arrows are not reflected, i. e. even if a sailor passed several arrows during the moving to a reflection cell, his further moving is symmetrical to the last empty cell.
2. But after the reflection further reflections will be relative to the result of the first one. E. g. when coming from cell 4 to the reflection cell, the sailor will be moved to the arrow, which will be reflected this time, and the sailor will be back at 4.
3. And a little exception — a sailor reflected to the reflection second time, will be reflected related to the same cell as first time.

It seems to be enough rules for this time. You can already see that this game is not like many other children's table games with fixed map representable as plane graph.

In the beginning I wanted just to set all rules and associated actions by using some rule-based engine. But I've discovered that the game field is not plane oriented graph, though it can be represented as 3-D graph. Each path containing a reflection cell can be passed through vertices located at some level. Thereby, if desired, any existing engine of a plane table game can be adapted to «Jackal» without considerable problems.

But to solve the problem of conversion Jackal field to graph I decided to try Drools engine.

2006-01-19

Link after link...

My GUIbuilding googling turned into rule-based engines googling which gradually turned into studying one of them. Mostly it was provoked by beautiful design and the presence of «Tutorials and examples» section at http://www.drools.org.

Now my task is to write it all down in order to remember the new information and, besides, to be able to recollect this in case of need.

I suppose, it will be about four posts in one and a half screen :)

Subtotals can be sumed up just now:

1) Concentration on a task is amazing, the main thing is do not forget constatly repeat the mantra: «everithing is a fact, I can operate facts only». If to use such an approach, most inefficient solutions are fall away. And you can wink at memory overhead.

2) «Rete network + OOP» is equivalent to «a fish + a bicycle». Probably, I will not rush to assert that rete + OOP is useless, it's only advantage is easy integration with existing systems. But during development it's only prevent thinking. IMHO duck typing is a good compromise in this case.

2006-01-14

Links: a story of a small optimization

Here and here is told how a profiler sometimes may help. Who could have imagined that it is possible to reduce program run time from 78 minutes to... 5 seconds

2005-12-12

Eclipse: Testing the multi-thread code.

A short snippet:

public void setUp() throws Exception {
finished = false;
}

public void testSomething() throws Exception{
new Thread(){
try{
doSomethingLongAndLogIt();
} finally { finished = true; }
}.start();

while(!finished && !Display.getCurrent().readAndDispatch()){
display.sleep();
}
}


There, it seems to be the full Eclipse emulation. Now one can be sure that inside doSomethingLongAndLogIt() method all communications with UI are going within Display thread.

Can't do without 'while' because the workbench might be already closed before it comes to UI calls.

2005-11-23

Macro in Ant

When you write an ant macro, the main problem is inabillity to define temporary property.
    <macrodef name="sample">
<sequential>
<java resultproperty="java.result">
</sequential>
</macrodef>
Property can be set only once, and later nobody can change its value. It is good behaviour,
elsewere we can run into troubles depends on order of target calls.

So, if we call the above macro second time, the value of ${java.result} stays unchanged.

But we can overcome this:
   <macrodef name="sample">
<attribute name="workDir">
<sequential>
<antcall target="internal.java">
<param name="dir" value="@{workDir}">
</antcall>
</sequential>
</macrodef>

<target name="internal.java">
<java dir="${dir}" resultproperty="java.result"/>
</target>
That macro can be called more than one time, and will be works correctly.