Mixins in CoffeeScript

So, we’ve finally achieved a small utility function that implements mixins in Coffee. In Javascript too :-)

Implementation might not yet be super clean, but it allows us to:

  • mix all methods of mixin class into a base class
  • mix many classes into a base class
  • a mixin has a place for constructing its data if necessary
  • a mixin can read&modify “base” class’ data
  • a base class can read&modify mixin data (awkward, but works)

In short:

and implementation is bit hacky, but works. Uses underscore.js and modified hashmal’s Mixin.
One small requirement of this solution is all roles must extend Mixin class.

A longer example of what’s possible along with part of our Jasmine test:

class Nothing
constructor: (@name=”nothing”) ->

doSomething: =>
“I cannot do anything”

class Presenter extends Mixin
presentYourself: =>
“My name is #{@name}”

doIPlay: =>
“I play with many #{@players}”

class Game extends Mixin
setup: =>
@name = ‘Monopoly’
@players = []
@players.push “David”

sayHello: =>
@presentYourself()

something = new Nothing()
expect(something.name).toBe(‘nothing’)

ObjectHelper.addRole(something, Presenter)
expect(something.presentYourself()).toBe(‘My name is nothing’)
ObjectHelper.addRole(something, Game)
expect(something.sayHello()).toBe(‘My name is Monopoly’)
expect(something.doIPlay()).toBe(‘I play with many David’)

something.name = ‘something’
expect(something.presentYourself()).toBe(‘My name is something’)

something.players.push “John”
expect(something.doIPlay()).toBe(‘I play with many David,John’)

</pre>

And stripped down whole Mixin.coffee

 So, happy mixxxxing to you :-)
Share on Twitter

Rails and real OO Java Petstore .. my turn now

So after great #wroclove.rb conference I’ve finally decided to roll my own design and implementation of Java Pet Store app.

It’s a well specified app with known functional requirements. It’s quite real world scenario app. Its implementation can be compared to other platforms. So it’s a good showcase for what I’m going to present.

Will Include (not limited to)..

  • aop
  • domain usecases
  • facade
  • roles
  • coffeescript mixins
  • rails
  • rails ActiveRecord .. not
  • … ? who knows..

Empty github repo waiting..

Share on Twitter

Do you still use JavaScript?!

Do you use JavaScript and still write your CODE in Javascript?

There are 3 answers really..

  • “yes I code in JavaScript, I’m not aware of CoffeeScript yet..”
    …so now you know, go and try next answer
  • “yes I code in JavaScript, though I am aware of CoffeeScript”
    • … maybe you “fear of the unknown“? I was for few months and regret now. Do not hesitate and learn CoffeeScript now, try it, spike, really.. use it
    • or you think JavaScript is still better…
      do you?.. really? .. but are you sure?
      … good luck! Because it’s only luck that you need now
  • “no, JavaScript is unmaintainable, I love CoffeeScript”
    .. and that’s good…
    .. CoffeeScript code is clean and elegant ..
    .. you can at last do Object Oriented programming without hacks…
    .. your code is readable
    .. and hence prone to less bugs.. or bugs are easier to find..
    .. and you can run it in the browser the same way as plain old javascript..

 http://coffeescript.org/

Share on Twitter

View is thin, dumb and a servant of domain model.. not the other way

I’ve came across this twice lately…

Two independent developers were trying to explain to me why their model is not readable (and maintenable) and doesn’t reflect real domain. Objects were wrongly associated together, and real world classes – were missing and not defined!

The wrong explanation of a dev was:

..because, a user using “admin UI” selects a card text and assigns bunch of NUMBERS (cell ids) it appears on .. No need for “board cell class”

Who cares how an user is using an app.. It should not have any, or minimal impact on how business domain is modelled, even db is designed..
Should it be a browser, a console app, fully dynamic Ajax website or an iPad app.. It’s only how the app looks, and how presents its information about data.

Views change, user preference change. View is a thin layer, only a presenter. Should not drive the design of domain layer.

An app was…

…I was looking at how some game was designed, a board game, and it was missing representation of a BoardCell

In this small OO world:

  • a game has a board
  • a board contains many board cells (hexes)
  • a board cell contains many cards, of which some give special powers

So I was expecting at least this below: (you can copy&run example)..
…But what I found something different

There was NO BOARD CELL REPRESENTED AS A CLASS.

I need real world model, close to business, close to the domain. I want ALL THE REAL CLASSES BE THERE. So I can touch them and work on them.

So, try to cut any class from above code. It will make no sense, as all the objects make sense there. It’s the pure model.

Now, based on this code you CAN start building views. Views may or may not present all the data. May only show final bonus of a player, of a whole board.. who cares.. that is what is view for.. to present, not to define!

Try to imagine your view changes, what then.. would you really want to have model not being a mirror of a real world, and change both: the view, the model … thought so :-) So don’t

 

 

Share on Twitter

My Law #2: Business Logic .. and forming Use Cases

As far as I remember I was always very eager on trying to transform natural world – it’s behaviour and information – into a computer. I always wanted to achieve two things:

  • tell the computer how real world is modelled
  • tell it in a way that a human can read and understand it

So the effect is

Implementing Business Logic of a domain is the most important and funny part to do

It has many impacts

  • what is the model?
  • what are the classes and how they look?
  • how to persist data to a storage?
  • how to deliver data to an app user and how to interact with the code
  • where business logic should be implemented
So I always wanted to be closer to the model part more than to other. It looked easy at first to create a Player class, it’s behaviour and data. And I was implementing high level business operations into the model, not the controllers layer nor the view.
class Player
  constructor: (@name, @age) ->

  throw_a_dice: () =>
    ...

  pick_a_card: () =>
    ...
.. but something was missing.. I discovered it just lately thanks to friends and possibly by waking up from a long 4-year hibernation that I was missing a representation of use cases in the system. TA daAA!

It is very important to split your models into at least 2 parts, maybe even 3.

  • “use cases”: a part where high level use cases, scenarios, algorithms, business logic is implemented (SelectingFirstPlayer, RollingADice, MakingAMove, PlayingMonopolyGame, ..)
  • “base models”: a part where base classes are implemented with their basic methods attributes that can be reused in use cases (Game, Board, Player, Pawn, Dice, Move, ..)
  • “storage”: a storage part (maybe, if it’s configuration would be too tight into base model classes, that it needs a separation)
It’s so obvious a use case must have a shape in the system somehow

I’m not yet sure what should be the scope of a use case. Using an example of a popular Monopoly board game, should a use case be “playing monopoly board game”, or only “throwing a dice and making a move”..

But I’m pretty sure all use cases must have a form in the system by being at least a class, that has this single responsibility: telling the whole algorithm of a use case in one place.

 

Share on Twitter

My Law #1: DRY .. or not

I think that’s the rule that drives my programming style the most is:

#1: Keep it DRY: Don’t Repeat Yourself

more here http://media.pragprog.com/articles/may_04_oo1.pdf from smarter than me

But there are 2 very different parts of the code where DRY might and might not be applied: programmer’s code and “high level” domain logic

A) Programmer’s code

I say, it’s a place quite deep in your code, rather related to implementation and less to design. Those are rather controllers and views, and deep parts of model implementation, every helper and utility class. Details far away from actual functional requirements, use cases, business logic.

So there – in programmer’s world – I spend lots of time thinking how not to write the same/similar code twice. Really, I can even spend 10 hours or few days of thinking and writing ZERO code..
.. because I’d rather shoot myself in a foot with a railgun than used dumb-COPY-&-PASTE. We nowadays know it’s PLAIN WRONG, do we..

  • copy & paste exact parts of code is lazy (“lazy” the wrong way as opposite to being positively lazy as in yagni)
  • copy & paste similar parts of code might look and is quick solution at first, but it’s not. Someone WILL try to understand and refactor it later and WTF you
  • it’s about respect to other developers
  • it’s about write-once, read-100-times. It’s been always this way. So why bothering people after you with your unsolved problems
  • copy & paste is a smell for immediate action: extract similar code into a method, pass parameters, whatever.. make duplication gone
  • and obviously if a change must be later done to all duplicates, it must be made in X places instead of ONE

So this is BAD code. Effect of dumb COPY&PASTE&CHANGE-ONE-VAR

B) Domain Code

.. we must make distinction on the “A)” code made for programmers and “B)” code meant to describe our natural world, our domain specific requirements.
This is a “high-level” domain code, possibly close to what customer say and do; to what their use cases describe. So it should really tell what it does.

Such code should not distract reader by forcing the reader to jump & dive around in & out of methods to see how things are implemented. I’d have all code served in front of me instead

In my opinion in domain code it is more important to see the intent of the code than to be pure DRY. I must admit I don’t make some strict rule on that yet. It’s rather my intuition today.

Applying all the fancy programming patters and refactorings just to make code few lines shorter does opposite and makes intent, the flow, the algorithm not clearly visible.

I say there are 2 cases where I’m not always fully DRY

  • acceptance tests – simulating user interacting directly with application (via browser mouse clicks, keyboard, triggering events, etc)
  • use cases – the most important domain code, domain algorigthms, business logic

So I would live with such code..

.. and I think I would also live with this one, but maybe bit less comfortable in the long term..
..although at first those 3 cases now look shorter and more readable, but they also PRETEND that they do something by a nice method naming. In fact those methods might do sth completely different. They hide details, but those details ARE IMPORTANT, so should not be hidden

PS. .. methods longer than 10-20 lines might be OK for domain code, but they’re rather on the longer side

Share on Twitter

Best technology of 2011: CoffeeScript

Goodbye Javascript, say “Hello” to new sheriff in town “CoffeeScript”

http://jashkenas.github.com/coffee-script/

Refactoring

On refactoring.. again :)

Am I a GOOD programmer when I write A LOT of CODE?No! I’m certainly not.

Tonight I found myself again very happy because I removed a lot of lines from my code. There was really a lot of redundancy in the code. And when the moment comes that I just CTRL+Y (IDEA‘s “delete line”) then I’m happy.
I’m really, really happy.

The first such moment in life was then when Andrew and I sat down in front of the hopeless app and thrown away a lot of generic code which just existed there. That was a custom database layer, and we changed it to another one, based on Prevayler. And during one afternoon we deleted a lot of someone’s else work – I remember that was more than 120 classes. Trash.

Tonight I did once more big refactoring. I didn’t deleted so many files this time, but removed a lot of redundancy in the code. And that’s really good.

Share on Twitter

Testing those aspect creatures

On testing aspect oriented systems

I was always wondering what is this hype about testing aspects or aspect oriented systems about? Or maybe there isn’t any..
Here you’ll find concrete examples of my JUnit tests of my Ilybra application and PAT framework.Test type no. 1: test of PAT aspect itselfWhen applying PAT to any software I have to test if PAT works correctly. To do that I need to write tests of the aspect. And PAT is a persistence aspect, so what I need to test is if the data is really stored on the disk after some persistent operation. And it means I must check if the database I use (Prevayler) behaves correctly: stores data changes on the file system, when recovering does it read files in correct order and does it restore the data after failure.
And persistent operation is PAT’s transaction invoked on business object (BO).So here is sample code, of how do I test my PAT aspect:

Test type no. 2 – testing Ilybra‘s business methods

It is trivial to test Ilybra application and its business methods. Due to the fact, any business class created with PAT is plain Java class without any dependency on external resources, I can just invoke the object’s methods without any “mocks fun”.
Below you’ll find one of my JUnit tests for testing user story: “Lend book to a reader”. The expected behaviour after invoking method lendBook is the copy must be located within reader’s current list of lend copies and the copy itself has to have a reference to the reader.

Code of the test case – again – without any cheating it is one of my original test cases. There is no external configuration to the test case. None of aspects is applied here, no custom advice here except PAT‘s annotations on transaction methods (lendCopy).

Note: I test core functionality here. And this is the most unique. PAT lets you create such simple test cases for an application

With those two examples above I think I’ve just exhausted my “AOP & testing” topic.

Have fun and take care!

Share on Twitter