Showing posts with label software. Show all posts
Showing posts with label software. Show all posts

Thursday, April 3, 2014

Learning Clojure by doing a simple things

I usually tinker around different languages or technology.  I have been learning a bit of Clojure for the last couple of weeks.  Tried it at the office, by making a simple REST client.  Now just making a simple web app, just to get a feel of the language and ecosystem around it.

Clojure reminds me of Tcl, I know its LISP and it should remind more of emacs.  It reminds me of Tcl as I done a lot of Tcl programming using aolserver and OpenACS before.  Unlike the first few times I used Ruby or Scala, where the first wow factor is the language.  Tcl and Clojure the wow factor is getting to do something really quick.  They both have quirky syntax, but simple and uniform.

Here is why I am learning Clojure:
  • Learning a new languages makes me better at the languages I already know.  Getting the nice feeling of being new, and learning a lot on the way... mistakes and aha moments.
  • Still on my journey to be a better Functional Oriented hipster.  Not that I am leaving Object Oriented or even procedural away, just learning a another way of thinking and solving computing issues.
  • Getting my son to learn something.  Clojure seems to have nice abstraction for sql, web and js.  Start with 1 language will be less intimidating, instead of learning a lot in 1 bang.
As a newbie, I started with a framework first.  I like get to know other people's opinion first before making my own.  I picked Luminus as it seem to be using some of things that the clojure community is using.  Here are some code snippets from bottom layer to top.  Its simple and lacks any real world complications yet, learn the ropes on the basic things first.

Data using Korma


It starts with a macro for defining an entity and I would guess the convention to tie with the backing rdbms tables.  Then you define some functions using Korma's sql dsl.  You deal with primarily 2 data structure "id" which is a string and "product" which is a map.

Web using Ring and Compojure


The above is defining the routes, which you tie to the application handler.  Real nice bits are the ability to define a context.  Which would mean enclosing routes will start with that path first.  Easy way to extract the request body from a key ":body-params", and to respond with body by setting the key ":body".

Finally the front end using Clojurescript, Gyr and Angular.js


It looks less Clojure idiomatic, however I wanted to start somewhere.  Angular.js is familiar to me, just realised that its been about 2 years ago I started looking at Angular.js.  We have been using it ever since, I am actually rusty on it already.  I saw the Gyr and Putnam project had pretty good documentation.  Gyr gives some macros such as def.factory, def.controller, etc.  which looks similar to normal angular.  Then the ability to use $scope.foo.bar which is pretty nice.  You will need to use "obj" and "arr" to interact with normal Javascript objects and arrays.

Clojure is nice and simple (still on first impression).  Gets you up and running quick.  If its good for long big projects, I don't know yet.  However I will be happy if I can meet 2 out of my 3 objectives in learning Clojure.  Complete source code here for the curious.

Sunday, August 26, 2012

Angular.js for ajax CRUD

Its been a busy weekend, not in terms of coding though.  Just did some quick coding now.  This is now a book-markable ajax CRUD talking to conventional REST/JSON backend.

//app.js

angular.module('ratewatcher', ['ngResource']).
 config(function($routeProvider) {
  $routeProvider.
   when('/lenders', {controller:LenderListController, templateUrl:'lender-list.html'}).
   when('/lenders/new', {controller:LenderCreateController, templateUrl:'lender-detail.html'}).
   when('/lenders/:id', {controller:LenderEditController, templateUrl:'lender-detail.html'}).
   otherwise({redirectTo:'/lenders'});
 }).
 factory('Lender', function($resource) {
  return $resource('/lenders/:id', {id: '@id'}, {});
 });

//controllers.js

function LenderListController($scope, Lender) {
  $scope.lenders = Lender.query();
  
  $scope.remove = function(lender) {
   lender.$remove();
  }
}
  
function LenderCreateController($scope, Lender) {
  $scope.save = function() {
    var lender = new Lender({id: 0,
     name: $scope.lender.name, 
     category: $scope.lender.category, 
     url: $scope.lender.url
    });
    
    lender.$save();
  };
}

function LenderEditController($scope, $location, $routeParams, Lender) {
 $scope.lender = Lender.get({id: $routeParams.id});
 
  $scope.save = function() {
   $scope.lender.$save();
   $location.path('/');
  };
}

I thought the scala backend was really concise, but it turns you can also make the front end javascript just as concise.  Coupled with bootstrap css and basic html, this seems pretty good.

Suggested readings:
http://www.angularjs.org/ - Wire Up a Backend example
http://docs.angularjs.org/api/ngResource.$resource
http://jsfiddle.net/johnlindquist/qmNvq/
http://docs.angularjs.org/api/ng.$route

Thursday, August 23, 2012

Angular.js my first few mins impression

I have been looking at Javascript frameworks.  I have experience in GWT and lately trying to learn ember.js.  So far ember.js is really powerful, but the docs is not that great.  Ember.js has made a lot of changes and the only way to learn it seems to be to read the code/api.  Since my javascript is not that good, it was taking me a bit of time to learn the magic of ember.js.  This lead to me look again a bit, I found angular.js because it was in google's CDN api.

Having read the watch and read the front page of angular.js it seems to be just as powerful as ember.js.  Its got bi-directional binding, a template engine, name spaces.  The terminology they have is FP (ala Scala).  They have apply, future promise.  So gave a try.

function LenderController($scope, $http) {
 $scope.lenders = [];
 
  $scope.all = function() {
    $http.get('/lenders').success(function(data) {
      $scope.lenders = data;
    });
  };
}

<div class="span10" ng-controller="LenderController">
    <table class="table striped">
        <tr> 
            <th>Name</th><th>Category</th><th>Url</th> 
        </tr>
        <tr ng-repeat="lender in lenders"> 
            <td>{{lender.name}}</td> <td>{{lender.category}}</td> <td>{{lender.url}}</td> 
        </tr>
    </table>
    <a href="" ng-click="all()">fetch all</a>
</div>
Pretty short and direct code for fetching from json/rest backend and displaying it on the view.  I think Angular.js is powerful and yet easy for a newbie to pickup.  That is my first few mins impression of it.

Thursday, June 28, 2012

Explaining C++, Java and Scala to my son

Last night my son was asking about computers again.  I think the conversation came about, what language is on my screen.  I told him its Java and Scala.  I talked about what is assembly, C, C++ and Objective-C and how it relates to Scratch and Kojo.

He asked about why it was named as C++, then I showed him a Java typical loop which is based C where we increment a counter.

int c = 0;
while (c <= 10) {
 System.out.println(c);
 c++;
}



He asked how the loop will be done in Scala.  I started w/ a for comprehension, as he is familiar w/ loops on Scratch and Kojo.

for (c <- 0 to 10) {
 println(c)
}
Then he asked me about the for, <-, etc.

I then changed the code into

0 to 10 foreach println


His reaction "that is so awesome it's like english!"

Sunday, May 27, 2012

Short comparison of Ember.js and GWT

Its been a while since I posted anything here, really hard to make continuous post.  Well try again for the n-th time.

I have been using Google Web Toolkit (GWT) for while, as this is what we use at work.  Its pretty good and has lots of positives, however it also has it's own negatives.

Lets go with the positives first:
  • For some people that knows Java they don't have to learn JS to develop full featured web apps
  • Existing java tooling works on GWT projects.  Re-factoring, IDE support, etc.
  • Performance of GWT is still the best among any JS frameworks
  • Pretty good documentation and examples
Now for the negatives:
  • Not learning JS, or some people will need to learn Java to develop JS code  
  • GWT seems to be on its own eco system outside of the wider JS eco system
  • Sometimes hard to control the html, making iterative changes with a web designer significantly more harder.  This is even using ui binder
  • REST/JSON still seems to be 2nd citizen in GWT.  There is autobeans in GWT now, however still lots of plumbing to do or use 3rd party library.
Using GWT to architect a full blown web app is great.  Using established patterns such as Model View Presenter (MVP), a cousin of the more popular Model View Controller (MVC) pattern.  For the past few years there has been a lot of JS MVC frameworks coming out.  A quick look of the Todo MVC project will give us a lot of different implementation.  (The GWT implementation hasn't been pulled yet).  One of the JS MVC framework that's been gathering a bit of attention is Ember.js.

I gave ember.js a try over the weekend, its been initially a bit frustrating.  Like always when you learn something new, sometimes we just need to unlearn our own old learnings.  Ember documentation is a bit confusing, as its formatted on how to use the different features.  Some of the materials I have found to helpful in getting me running are:


After things started to fall in place, ember.js seems to be pretty straight forward.  Compared to GWT there is really significantly less abstractions.  As ember.js really tries get you in the middle of JS, HTML and CSS.  I am still unsure on how unit testing will be done, haven't look at ember data yet, etc.  However with this initial code:

var App = Em.Application.create();

App.Plant = Ember.Object.extend({
 id: 0,
 name: null,
 description: null
});

App.PlantsView = Em.View.extend({
 plantsBinding: 'App.PlantsController.content'
});


App.PlantsController = Em.ArrayProxy.create({
 content: [],
 
 init: function() {
  var me = this;
  var url = 'http://localhost/plants';
  $.getJSON(url,function(data){
    me.set('content', []);
    $(data.plants).each(function(index,value){
        var plant = App.Plant.create({
            id: value.id,
            name: value.name,
            description: value.description,
        });
        me.pushObject(plant);
    });
  });
 }
 
});

  

As you can see its really straight forward to get data from a rest/json server.  I have an equivalent GWT project, but its too big to post here.  However the equivalent GWT functionality would include.  A model class, a ui binder file, an entry point class, a presenter interface, a view interface, a presenter implementation, a view implementation, http get code, etc.

This doesn't mean longer code is worse code, I am not in the camp of Java is bad too much typing.  I still use Java, although less these days as Scala has pretty much filled the space.  Even with the above code, one can see JS short coming of not having name spaces and no real support for OOP.  However one can see also there is a lot less boiler plate and more natural integration with JS and HTML.  When I mean less boiler, I do not mean about Java's verbose code but the bindings are straight forward.  More natural integration, I just used jQuery's getJSON function straight out of the box and handlebars' template inside html.  GWT has JSNI to call jQuery, but that is less natural and not used often.

Ember.js seems to be promising for organizing web apps.  Then coupled with other mature JS frameworks like jQuery, I think it may make GWT a bit heavy for some use cases.  I am still thinking of looking at Scala+GWT, aside from getting to use Scala to do JS I don't see a lot of difference from normal GWT.

Wednesday, August 18, 2010

Looking at Scala again

I have been look at scala and lift (web framework built on scala), on and off about a year now. I have revisited it again for the past few weeks. Scala as a language seems to be promising. For me it adds something new to the main stream languages today, like pattern matching (not regexp), functional programming, actors library. There are some languages that have these properties but I think scala has got some transitional properties giving some balance. Using scala you get to access java libraries natively, despite its terse syntax it is still a static language, this would mean tool developers will have an easier.

Scala
Lift

I have also been re-using emacs again. Some interesting scala emacs some stuff.

Scala emacs tools

Wednesday, October 31, 2007

Model software in color

Today and tomorrow I am attending a workshop on "How To Design Better Software Using FDD", which is essentially about color modeling. Color modeling uses the 4 archetype, Moment Interval (MI) (something happening in time like transaction, business process), Person/Place/Thing (PPT) (something in space like customer, site, product), Role (how are PPT interacting with MI) and Description (catalog /metadata for PPT like list of video titles). Each one of the archetype has a color to make things easier to read UML w/o adding more clutter: MI - pink, PPT - green, Role - yellow and description - blue. Here is like a reference domain neutral model called Archetypal Domain Shape. Your model will almost end up with pink - MI being like the center.

For me color modeling is a nice concept and puts some fun and simplicity in modeling the problem domain. Whether or not its effective and useful is another question, others and the creators have claimed its effectiveness in real world projects... so maybe it does have some benefit. I do think I will give it a try as I see it as something useful to have in communicating ideas of a project. The main lesson was after all is not the colors and model, but ask the business and do not assume. I wish there was some better model similar to a state diagram model which conveys a point in time, UML class diagrams does not convey very well non static stuff. How can you model properly dynamic languages such as Ruby that can redefine classes in run time?

Monday, July 30, 2007

Why do some business software fail?

Some business software fail because the tech guy fails to understand the business needs. So we have software that runs but does not work. On the other end of the scale, business people keep pushing to cut corners. So we now have a business software that is perceived to work, but it actually does not run properly. I think we all know about this classic tug of war. However I am thinking maybe business software fail because people do not care. In the software business people move from one place to another in just a few months or years. The life of the software is longer than the average tenure of a business or tech staff. I see people only caring about their current deliverable, or what will make him more to his/her next job.

In a way having a open source base may help (not the silver bullet) in getting the software to live beyond the people who handles it. However its not possible to open source all software, and a more important consideration is open sourcing will not guarantee a community will evolve around it. Until then those people who cares about software that works will have to work with those people who just cares for the bucks. Sadly there are more people who is in it for the buck, which also makes it hard to make software that works.

Tuesday, July 3, 2007

Eclipse Crash

I had one of those bad eclipse crash, I am glad someone had a similar problem. I just deleted the files:

/.metadata/.plugins/org.eclipse.core.resources/.snap
and
/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap

I got the tip here

Long live vim and emacs!

Thursday, May 31, 2007

Developers like to create square wheels

I am helping in fixing the scalability of a web app. While digging through the code looking for the problem spots. I saw a piece of code and a comment saying:

# One common problem with login is that people can hit the back button
# after a user logs out and relogin by using the cached password in
# the browser. We generate a unique hashed timestamp so that users
# cannot use the back button.

Wow that is an amazing feature! But it clearly shows the previous developer did not understand the framework he/she was working on. The framework provided the login page to timeout, I guess this feature doesn't exists anymore along with the other security features, since he/she heavily changed it and erased the code to secure the login page.

Hopefully I don't become a developer that looks at a wheel, not able to understand the wheel and create my own wheel which is square.

Wednesday, May 30, 2007

Cut and Paste software development nightmare

I think Cut and Paste is one of the worse things for software development. It seems to more convenient for developers to Cut and Paste rather than thinking of making the code modular. Its really sad how Cut and Paste really destroys code.

Sunday, May 27, 2007

What most software developer think?

I have been involved with different software projects and teams. There is one thing that I always encounter, especially from the less experienced developers. Most of them always think the current code is crap, they can do better, what the hell was the last developer doing, yada... yada... yada...

Little did they know that the previous developer may be a in different circumstance and usually its the time pressure that makes developers code like crap. Yet most of the yappy developers who think they can do better often do the same crappy job or even worse development once they are the one under the time pressure.

Good software developers can write good design and code, as well as understand and tries to understand existing code and history.... but sometimes people do code like crap :)

Wednesday, May 16, 2007

Developing on Spring

Its been a week now with my new job. Doing primarily Java and using Spring framework. Spring is pretty good, although not as Spring to me. It maybe Spring for those who came from EJB winter, but not for me where I had some experience with RedHat CCM, OpenACS and a bit of Rails.

I am still holding my verdict for Spring. What I do like is that the framework encourages layering through IoC and interfaces. It surely does have advantages, however like all advantages there will be some drawbacks. So is the advantage big enough over the drawback, or is it just better than EJB winter times?

Wednesday, April 18, 2007

Javascript revisited

Its been years since I did some Javascript. Been doing it since Netscape 3.x. I had to do some Javascript again for a web app. I found this great Javascript Reference

It really helped me since the date functions setMonth and getMonth is Month minus 1 (MM-1). Their bomb icon really helped me that its a known gotcha. I might have known this before, but it has fallen out of my memory.

I have always used this Netscape Navigator JavaScript Guide before. How things have changed, Navigator is now a minority. And how things are also the same, Firefox/Mozilla is still the best browser to develop on. Firebug, Web dev toolbar, etc.

Friday, March 23, 2007

Reading a book because someone told me so.

Today I started to read a book by Martin Fowler, Patterns of Enterprise Application Architecture. Primarily because I flunked 1 job interview, since they where asking about mostly about "what" questions, not much "how" and "why". I had a hard time answering them, even though I did a lot of big systems before. I even goofed up some simple questions. However my next job interview went well, even gave me a better offer. The panel from this interview where more interested on the "how" and "why" things. They didn't look for me to answer all the terminologies correctly, I was even given a chance to use the whiteboard to further explain.

I was however curious to read the book, as one of the guy in the first panel told me to read it. As I have discovered I knew a lot of the patterns, however I was not aware of them. The book is pretty ok, especially the fact regarding to identify patterns so it can easily be communicated with other developers.

First thing that I learned about being a good architect. There is no concrete architect role, however one is needed to communicate to people outside software development that role is for a person that thinks beyond the code.

Monday, March 19, 2007

What does it take to be a Software Architect?

I am not sure how this is becoming a fad now. foo + Architect = big bucks. I know there are some good people out there that have the Architect title, but some are just jargon heads. I think the best combination is to be a good decision maker and broad technical knowledge that knows how to speak the jargon.

I think I have worked on the technical and decision making side of things, however I think I got my head too deep into making things work that I got left behind with all this jargon stuff. I think I better read some more books, rather than reading more manuals and code.

I wonder what would I need to be a REAL good foo Architect...

Friday, March 16, 2007

Keylogger shown on TV news, gives cut and paste as security solutions... how secure.

I was watching some TV news show, TV NZ's Close Up. It was talking about keylogger, what news I thought. Anyway its good for folks not in computers to get them more aware. It wasn't really bad, its telling how do keylogger works and how easy for net cafes to have one installed. Then it came to the part on how to secure yourself from it... you bring a flash drive with a file containing your username and password. Then cut and paste your username and password. Wow what a secure and great solution, how the heck this got passed the editors or even the so called security expert they interviewed.

The special should appear below, on the March 16 show.

http://tvnz.co.nz/view/page/497100/1025745

If there one thing that is worse about security problems its proposing solutions that are false and gives false security sense.