Twitter on Scala Interview

There is a nice interview with the Twitter development team on Artima about using Scala in production on Twitter code. The team talks about some issues and facilities regarding the chose of Scala to develop Twitter’s queueing system and how Scala affected the team’s programming style.

My personal highlights:

And Ruby, like many scripting languages, has trouble being an environment for long lived processes. But the JVM is very good at that, because it’s been optimized for that over the last ten years..

So Scala provides a basis for writing long-lived servers…Another thing we really like about Scala is static typing that’s not painful. Sometimes it would be really nice in Ruby to say things like, here’s an optional type annotation

And because Ruby’s garbage collector is not quite as good as Java’s, each process uses up a lot of memory. We can’t really run very many Ruby daemon processes on a single machine without consuming large amounts of memory

In some cases we just decided to burrow down and use the Java collections from Scala, which is a nice advantage of Scala, that we have that option..

As I’ve learned more Scala I’ve started thinking more functionally than I did before. When I first started I would use the for expression, which is very much like Python’s. Now more often I find myself invoking map or foreach directly on iterators..

The reason you should care about immutability is that if you’re using threads and your objects are immutable, you don’t have to worry about things changing underneath you..

It’s very worth read. Post your comments here when you read it.

Tags: , , , ,

Twitter Ecosystem Tools

I’m very impressed of how many applications are being built on top of Twitter. This simple communication tool bas became so popular that people use it to expand their business relationships, promote themselves, discover new friends, talk about stuff they like, et cetera..

Here are some tools (maybe mashups?) which use Twitter:

Here you can find other 100 Twitter Tools

Tags: , ,

Bean Validation – Emmanuel Bernard on JSR 303

JavaLobby released an interesting interview with Emmanuel Bernard, the spec lead of of JSR-303: Bean Validation..

One of the important goals of the Bean Validation spec is, as Emmanuel Bernard says, is

to provide a default runtime engine that is used to validate the constraints around the domain model, in Java.

The development team decided to use annotations to express the constraints applied at the domain model. They argued that with annotations the constraints are put directly at the properties in the code, expressing the constraints associated with the property.

I highlighted some interesting parts of the interview:

Bean Validation lets you standardize the way you declare the constraints. So an application developer will just know one way to declare constraints, and just forget about all the different models. There’s no duplication around the validation on the layers.

If you think about a constraint, a constraint is really some kind of an extension of the type system.

Bean Validation lets you validate an object graph using the @Valid annotation. Basically saying, ‘Well, when you validate this object, make sure the associated object is validated as well.’ By the way, even if Bean Validation is primarily annotation focused, there is the equivalent in XML.

Any framework that is having a need to use validation, to apply validation, can either use the runtime engine of Bean Validation or go extract the metadata and play with that. So the user declares it once, in one place, and every framework in the Java space can actually go and either apply the validation routine …

You can listen the podcast of the interview also. It’s very noteworthy to read or listen the interview and see what’s next on validation in Java. Before the Bean Validation API become public to all of us, which framework do you use to validate constraints on the Java platform?

Tags: , , ,

Installing Vista..and restoring Grub from a live Kubuntu cd..

Somedays ago I installed Vista for the first time at my home desktop (I was very worried about the performance of this OS, so I took a long time to give it a try) and because of my disattention I erased my MBR, so my good old grub startup screen no longer appeared.
I couldn’t boot from my Kubuntu anymore ( I prefer KDE rather than Gnome 🙂 ). So, I googled a little bit and found at the Ubuntu forum how to restore grub from a Ubuntu live cd. I restored it with some steps and all went fine! I hope you enjoy this tip!

Tags: , , , , , ,

Joshua Block on How to Design a Good API & Why it Matters

In this talk (recorded at Javapolis), Joshua Block presents guidelines about how to design good APIs. I highlighted what i think are the most important parts of the talk:

  • Functionality should be easy to explain: If it’s hard to name, that’s generally a bad sign
  • Good names drive development
  • Be amenable to splitting and merging modules (If names are nasty, take a step back and make things easy to describe)
  • When in doubt, leave it out
  • You can always add, but tou cannot take it out
  • Implementation Should Not Impact API
  • Always omit implementation details
  • Inhibit freedom to change implementation
  • Don’t let implementation details “leak” into API (For example: Serializable, hash functions)
  • Minimize Accessibility of Everything (This maximizes information hiding)
  • Public classes should’nt have public fields
  • API should be easy to learn, read and use: It should be consistent, it’s a little language
  • Documentation matters (Example: Method contract between it and it’s clients)
  • Think of preconditions, postconditions, side-effects
  • Don’t Transliterate API’s
    • What’s the problem it solves?
    • What shoud abstractions did it use?
  • Don’t Make the Client Do Anything The Module Could do
  • Throw Exceptions to Indicate Exceptional Conditions

When you see the talk, post your comments here.

Tags: , , , ,

Erich Gamma Discusses Jazz, Eclipse, JUnit and Design Patterns

Good interview from Erich Gamma at QCon London 2008, where he discusses, among other things, the JUnit framework, the Gang of Four book about Design Patterns and the Jazz project. I’ll try to summarize some of his interesting advices and responses here:

Why Eclipse is so successful:

“We focus on stable APIs, so we understood it was a commitment up front and we maintained our APIs, so we tried to really avoid breaking our community..”

About JUnit:

“We always said it would just be as simple as writing System.out.println() but fully automated….I think the key was it makes writing tests as simple as writing this debug statement….”

About Design Patterns:

“In Design Patterns we talked a lot about abstract coupling, that you can couple things by an abstract class, that the reference is only to an abstract class interface..”

“Never use the class names we give in the pattern for it – that’s wrong. Use the domain-specific names, make it very specific to what your use..”

He also gives advices in how to identify design patterns:

  • Something non-obvious
  • The same kind of structure
  • Confidence on multiple uses..

About Dependency Injection:

“I think it could be captured as a pattern. There are a lot of tradeoffs in there, it would fit into the whole creational realm..”

Check the whole presentation on InfoQ here. It’s worthwhile to see.

Tags: , , , ,

My first impressions about LaTex

There’s been a good time I have wanted to try the http://en.wikipedia.org/wiki/LaTeX. So many times some masters / doctorate friends told about it for me very well. When I started doing my post-graduate course in Software Engineering at ITA, I knew it was time to test this powerful too. And this moment has come. For those who don’t know LaTex, it was created by Leslie Lamport in the 80’s and it’s built on top of the TeX tool, which allows the author of academic works obtain high-quality typeface. The TeX is a typography software developed by Donald Knuth in the 70’s for the publication of texts with great layout and typographical quality, particularly those with focus on mathematical equations. Knuth, at that time, developed the TeX to write his most famous books’ collection named The Art of Computer Programming, because he didn’t find a decent typesetting system at the time. Basically, the LaTeX adds a set of commands in the text that define how the system’s processing TeX will format it.
The text is entered within several LaTeX commands, as chunks of code in any programming language. These commands define the font type, the text formatting, chapters, sections, special characters, margin size, and so on. This makes the system different from the WYSIWYG methodology, in which the text is written exactly as it will be seen in the final result. All LaTex command begins with a backslash (\). LaTeX files are created with the .tex extension and the text should be compiled and the result file could be a binary DVI or PDF file. The fact of having to compile the text may represent a difficulty to the editor of the text, but the final result is very good, with high-quality typeface, as if the text had been edited by someone profession on the area.
The major advantages that I see in using LATEX instead of a WYSIWYG common editor, such as Word or OpenOffice, are:

  1. The LATEX lets you define how the text should be presented and formatted in the same way as the CSS works with HTML / XHTML. The idea is that you don’t worry about how the text should be formatted. Leave this work to TEX!
  2. Mathematical formulas, displayed by LATEX, are extremely elegant.
  3. LATEX manages all the numbering of chapters, sections, lists, figures and tables, footers, etcetera..
  4. TEX is portable and free, so that means it works in most existing hardware.
  5. You can tell the LATEX additional libraries to specify, for example, references for a second rule.
  6. Do not worry about the layout, only within the structure of the document!

As a quick example, we would have the following construction in LATEX:

\documentclass[a4paper]{article}
\begin{document}
Your text here….
\end{document}

The code sets up a minimum LaTex file, which is defined using the article class in a A4 size paper. The tags \begin{document} and \end{document} must contain the text to be entered. These commands, when compiled by Tex within the .pdf output document option already produce an excellent final result, very different from the Word one. You can find good references on the package here. And I leave here a challenge: even if you are a fan of Word, try to create a document in LaTex once and see the final result! I bet then you won’t stop writing your texts without it!

Portuguese version: http://log4dev.com/2008/07/29/minhas-primeiras-impressoes-sobre-o-latex/

Tags: , , ,

The Fan programming language

Cedric has showed us an interesting programming language called Fan, which has a lot of useful features. The ones I liked most are:

  • Familiar Syntax: Java and C# programmers will feel at home with Fan’s curly brace syntax.
  • Concurrency: Tackle concurrency with built-in immutability, message passing, and REST oriented transactional memory.
  • Static and Dynamic Typing: Don’t like the extremes – take the middle of the road.
  • Object Oriented: Everything is an object.
  • Functional: Functions and closures are baked in.

Here are some code chunks, showing its closures syntax:

// find files less than one day old
files := dir.list.findAll |File f->Bool|
{
    return DateTime.now – f.modified < 1day
}

// print the filenames to stdout
files.each |File f|
{
    echo("$f.modified.toLocale: $f.name")
}

I haven’t tried the Fan language a lot yet (I’ll post my comments here when I do it), but I congratulate the Fan authors for being possible to run it on both the JVM and .Net. I agree with Cedric when he said about the possibility to declare constructors with arbitrary names (although they must be prefixed with the new keyword) and invoke it as static methods of the class. From designer of the class point of view, it’s easy to identify the constructor (it’s highlighted by the new keyword), but from the client it’s difficult because there’s no way to differentiate a constructor from a static method call. It’s a bit odd to be able to do that. Just take a look at the code:

// Using an arbitrary name as a constructor
class Person
{
    new create(Int age) { this.age = age; }
    Int age
}
p = Person.create(20)

Anyway, I think it’s a good work to make the language have both object-oriented and functional constructs and be portable to both the Java VM and the .NET CLR. Good work guys!

Tags: , , ,

About CSS2 Selectors

Some days ago I was trying to customize my blog’s look and feel to remove my pic from each one of my entries.
At my blog’s theme, every <img> tag (which contains my pic for each entry) is the first child of a <td> tag.
I discovered a very handy CSS2 selector syntax to accomplish this task:

E:first-child: Matches element E when E is the firstchild of its parent (The :first-child pseudo-class)

So, the custom CSS ended up being td > img:first-child {display: none;}, which removes my user pic from every post, but keep the one on the left sidebar.

Just now you are seeing only one pic of mine 🙂

Tags: , , ,

DomainDrivenDesign: Domain Services or Method on an Entity?

There has been too much discussion on the DDD list regarding where to put the business logic control, whether in a service or entity. Being more specifically, in order to ship an order, the followthings should happen:

  1. Validate that the order can be shipped
  2. Update quantity
  3. Set the status to shipped
  4. Save the order
  5. Send an email to the customer that the order has been shipped

So, nickgieschen suggested the following C# implementations:

1. Everything in the domain:

 1:class Order
 2:{
 3:    IOrderShippedNotificationPolicy _notificationPolicy
 4:    IOrderRepository _orderRepository
 5:
 6:    void Ship()
 7:    {
 8:        if (!CheckIfOkayToShip()) {
 9:            throw new InvalidObjectException();
10:        }
11:        UpdateQuantity();
12:        _orderRepository.Add(this);
13:        _notificationPolicy.Notify(this);
14:    }
15:}
16:
17:class OrderShippedNotifyByEmailPolicy : INotificationPolicy
18:{
19:    // The object that gets injected is implemented
20:    // in the infrastructure layer
21:    IEmailGateway _emailGateway
22:    
23:    void Send(Order this)
24:    {
25:        // Create email here
26:        _emailGateway.Send(email);
27:    }
28:}

2. Or have an application service coordinate:

   1:class OrderService
   2:{
   3:    // _orderRepository and _orderShippedNotificationPolicy 
   4:    //  are injected dependencies
   5:
   6:    void ShipOrder(Order order)
   7:    {
   8:        order.Ship(); // in this case it only validates and updates quantity
   9:        _orderRepository.Save(order);
  10:        _orderShippedNotificationPolicy.Notify(order);
  11:    }
  12:}

There’s been a lot of replies also. I highlighted the interesting ones:

“The advantage of the latter scenario is that you’re calling _orderRepository. Save in the application layer, which I prefer since it’s easier to see the transactional control. The problem with the latter scenario is that it seems it’s putting things in the application layer which don’t need to be there. The action to Ship() seems to me an atomic, domain centered action and should therefore sit in the domain. I consider the application layer to be like a thin domain facade as defined by Fowler. That is, it is only there to direct/coordinate domain activities. Like I said, Ship() seems like it should be considered one activity, and therefore coordination from a service layer shouldn’t be necessary.”

“The way I look at it – what needs to go into Ship() is the stuff that _must_ happen before shipping can happen. And shipping can happen without the notification part. You only have a rule that says “send a notification to the customer upon shipping the order”. You don’t have a rule that says “make sure the customer gets the notification or there are no shipments”.

“So, perhaps as a rough, preliminary rule we can say anything which affects the state of the domain should go be placed in the domain. (Of course, the application layer can affect the state of the domain, but only by using domain items to do so maybe think of it as the Law of Demeter among layers.) The email doesn’t have any meaning within the domain – it’s simply a reflection of the domain.

“Notificitation of an order and the order itself is two separate concepts.”

“This could as easily be implemented using AOP.”

I think DDD advocates are a little bit extremists with some concep
ts, like repository. I wouldn’t have designed it on the domain layer, because I want transactional control on the application service layer. I think the domain has to deal with its particularities, not with sending email or adding things to a repository, even being decoupled of theirs implementations (the domain has a reference only to interfaces). So I prefer the latter approach. And you? What are yout thoughts about this design? How would u have designed it? Everything on the domain or have an application service coordinating the activities?

Tags: , , , ,