Yet another programmers blog

The new experiences of a new programmer

error: Value in [posts.slug] exceeds maximum length of 150 characters.

without comments

I tried to move my blog from WordPress to Ghost. I used the Ghost wordpress plugin to export everything as json, but when I tried to import the file in Ghost, I got the following message:

`error: Value in [posts.slug] exceeds maximum length of 150 characters.`

It seems that sometimes the sluglines exceed 150 chars, and due to some reason, ghost can’t handle it. So I wrote the following little script that you can run on the json file you recieved from the Ghost export plugin, which creates a new corrected json file that you can import regularly to Ghost.

Written by yossale

October 2nd, 2014 at 11:16 pm

Posted in Uncategorized

Good NodeJS habits and practices (Heroku)

without comments

Heroku just published in their blog a very nice list of best practices for working with nodeJS and npm. Great read!

Written by yossale

March 20th, 2014 at 5:57 pm

Posted in Uncategorized

Easy Executors and Callables in groovy

without comments

Multithreading and general multi-tasking in groovy is super easy, thanks to frameworks like GPars. However, even the basic Java frameworks can be easily utilized in groovy for a rapid-no-brainer task-driven design.

Thanks to Groovy’s closure coercion, running a set of callabels is as easy as this:

Basically what happened here is that every closure in groovy is executable, as we can see here:

So it’s very easy to use a simple closure as a callable, and defer the actual execution of the command inside the closure to the Executor. Cool, ahh?

Written by yossale

March 13th, 2014 at 2:32 pm

Posted in Groovy

Url catching regexp

without comments

Not exactly a brilliant piece of engineering, but this is a useful dirty hack.  If you want to clean urls from a string, you can match it using this regexp:

For “http://…” kind of urls:

text.replaceAll(/(https|http):\\/\\/[a-zA-Z0-9\-\._~:\\/\?#\[\]@!\\u0024&'\(\)\*\+,;=]+/, "")

For “www.” kind:

ext.replaceAll(/www\.[a-zA-Z0-9\-\._~:\\/\?#\[\]@!\\u0024&'\(\)\*\+,;=]+/, "")

The main point here is that the above characters are the only one allowed in urls, so every string that matches these is a url. It doesn’t work for stuff like “io.com”.

 

 

Written by yossale

January 13th, 2014 at 11:10 am

Posted in Uncategorized

Node JS for backend programmers

without comments

I started working in a new and exciting start up, and the thought of using Java for development crushed my soul, so I began shopping around for a fast-development, fast-deployment, easy ramp up and heavily supported language. I wanted something that will be easy for new developers to learn, and most importantly, something that I’ll have fun writing in.

NodeJS was definitely not my first (or my second, for that matter) option: I’m a backend kind of guy. I’ve done some web, but I would hardly claim expertise in web and UI. When I’ve heard of NodeJS, which was described to me as “Javascript for backend”, I was hardly excited about it. I’ve had some experience with JS, and the experience has hardly made me want to keep using it, let alone write my backend with it – it had none of the order or the elegance of what I used to think of as “Real languages”.

However, since the name NodeJS kept coming up from people and colleagues whose opinions I really appreciate, I’ve decided I can’t keep ignoring it and I have to look into it before I dismiss it.

To make a long story short, I now use NodeJS as part of the production environment, and I find it flexible, easy to deploy and very well supported by the community. However, it doesn’t fit everything and everyone – specially if you have a cpu intensive app.

Bottom line – It’s a very efficient functional language with non-blocking I/O that runs on a single thread, on a single cpu (but you can start several apps and communicate between them) and with a large and thriving community.

So, what is NodeJS, anyway?

Basically, Node JS is frameworks designed around an event-driven, non-blocking I/O model, based on Javascript with I/O libraries written in C++. There are 2 main reasons why JavaScript was chosen as the programming language of NodeJS:

  • Google’s V8 engine (the javascript interpreter in chrome)
  • The fact that it had no I/O modules, so the I/O could be implemented a new to support the goal of non-blocking I/O.

The fact that it’s running on Google’s V8 also ensures that as long as google keeps improving Chrome’s performances, NodeJS platform will keep advancing as well.

What does it do, and how?

Well, from my POV, the main deal with node is that everything is non-blocking, except your code. What does it mean? It means that every call an I/O resource – disk, db, web resource or what have you is non-blocking. How can it be non blocking, you ask? everything is based on callbacks.

So for example, if you’re working with mongodb, this is how a query looks like:

var onDBQueryReturn = function(err, results) { console.log(“Found “ + JSON.stringify(results) + “ users”}
console.log(“Calling query”);
db.usersCollection.find({‘_id’: “1234”}, onDBQueryReturn);
console.log(“Called query”);

The output will be:

Calling query
Called query
Found {“_id”: “1234”, “name”: “User1”}

Now, this might make perfect sense for people who are experienced with functional programming, but it’s kind of a strange behaviour for people accustomed to Java, for example. In node (much like in other functional programming languages), a function is a first level object, just as a string – so it can be easily passed as an argument. What happens here is that when the call to the DB is completed, the callback function is called and executed.

The main thing to remember is that node js runs on a single cpu*. Because all it’s I/O intensive calls are non-blocking, it can gain high efficiency managing everything else on a single cpu, because it never hangs waiting on I/O.

How is it like to write backend app with Nodejs?

In one word – easy. In several words – easy until you get to parallelism .

Once you get the hang of the weird scoping issues of Javascript, Node can be treated like any other language, with a non-customary-yet-powerful I/O mechanism. Multi threading, however, is not a simple as in other languages – but keep in mind that the concept of non-blocking I/O has made the need for multithreading much less necessary than you usually think.

In addition, Node has a very thriving community – which means that everything you could possible want has already been developed, tried and honed: You can use the npm – Node Package Manager (which is Node’s equivalent of the maven repo, I would say) for easy access to everything: One of the most interesting pages is the Most depended upon modules.

Multithreading in Node

There is nothing parallel in node. It might look like a major quirk, but if you think about it, given that there’s no I/O blocking, the decision to make it single threaded actually makes everything a lot easier – You don’t really need multithreading for the kind of apps Node was designed for if you don’t hang on I/O, and it relieves you from the need to think about parallel access to variables and other object.

Inspite all the above, I feel strange when I can’t run things in parallel, and sometimes it easy kind of limiting, which is why Node comes with cluster capabilities – i.e, running several node processes and communicating between them on sockets – but it’s still experimental . Other options are using the fork(), exec() and spawn(), which are all different implementations of ChildProcess.

Should I use it or not?

The short answer is, as always, it depends.

If you feel at home with functional programming and you’re running an I/O intensive app – which is the classic use case for a website – then by all means, do. The community is very vibrant and up to date, deployment is a spree (specially with hosting services like Nodejitsu and codeship.io)

If you’re running a cpu-intensive application, if you don’t trust dynamic typing, if you don’t like functional programming – don’t use it. Nice as it is, I don’t think it offers anything that couldn’t be achieved using Ruby or Python (or even scala, for that matter).

Few last tips

(in no special order)

IDE

IDEs are a kind of religion, but I’ve been using WebStorm and I like it a lot. It’s 30-days trial and a $50 for a personal licence (or $99 for a commercial one), and I think they even provide them for free for open-source projects.

Testing

It’s very easy to make mistakes, especially in dynamic language. Familiarize yourself with Mocha unit testing, and integrate it into you project from day 1.

Coordinating tasks

Sometimes you want to run several processes one after another, or you might want to run a function after everything is done, and so forth. There are several packages for that exact purpose, but the most widely used and my personal favourite is Async

More resources

Understanding the node.js event loop
Multiple processes in nodejs and sharing ports
Multithreaded Node.JS

 

Written by yossale

January 5th, 2014 at 4:51 pm

Git – Checking out only a specific folder

without comments

I’ve been looking for a way to checkout only a specific folder from a git repository. Most answers I found to this problem stated that it’s not feasible – but apparently, this feature was added to recent versions of git, and it’s now quite easy, as described in the following blog post : http://briancoyner.github.io/blog/2013/06/05/git-sparse-checkout/.

One notice – when you run the “git pull origin master” command, it looks like it’s going to checkout the entire repository – it’s OK. It’s only going through the repository, but the eventual result will be only your specified folder.

 

Written by yossale

December 4th, 2013 at 10:37 pm

Posted in Uncategorized

Web programming for server people

without comments

I’m mainly a back-end engineer – through my not-so-long career, I’ve dealt with sql, no-sql, offline processes, etl mechanisms, billing services, web-crawlers, recommendation system, offline learning algorithms, and so forth, and so forth. The only web-related thing I did was to write struts hooks for specific server functions. So basically, I know nothing about http-requests, css, javascripts and other web-thingies – which makes me feel a bit handicapped.

So I was very happy to find the Ruby On Rails tutorial by Michael Hartl. It’s not a “Learn ruby and do some web things” – it’s more like “Basics of web programming – using Ruby on Rails as an example”. Going through the tutorial, you’ll learn a lot about web-programming thinking, conventions, and all those pesky thing about web-programming you didn’t know you didn’t know.

It’s very well written, takes you fast to where you want to go, and if you have enough programming background, you’ll skim right through it.

If you’re a back-end programmer and you want to know what this front-end thing is all about – this is definitely the tutorial for you. And it’s FREE!

 

 

Written by yossale

July 9th, 2013 at 11:14 am

Posted in Uncategorized

Finally! What’s hogging your RAM?

without comments

Some of you may have noticed that the Windows Task Manager doesn’t show you the real memory consumption of the applications you’re running. It’s quite obvious, specially when the actual memory consumption, and the sum of the memory consumption presented by the task manager doesn’t add up!

But fear not! Microsoft has a tool called RAMMap that will tell you, in excruciating details, exactly which process and file use each memory page. No longer will you try voodooing your application to find out what is draining your RAM!

You can get it right here

 

Written by yossale

July 1st, 2013 at 7:18 pm

Posted in Uncategorized

Zorpia is a scam

without comments

I’ve been getting a lot of emails lately from a website called “Zorpia” claiming I have a message from someone named Katja. I’ve never signed into this website, and I also don’t know anyone named Katja, so I ignored the first and the second emails – but I just kept getting more and more of them, so I decided to find out what the hell is this website.

Apparently, Zorpia is some kind of a social website, with big aspirations and not a lot of actual value to anyone. It uses spam mechanisms in order to lure you in : if you click on any of the link in the email, it redirects you to a Google page asking you to allow Zorpia access to your personal info and contacts list, and if you’ve done the mistake of allowing it – all your friends will be spammed from now to the end of days with annoying, deceiving e-mails. Actually, this is what Wikipedia has to say about it:

It is widely reported by users that Zorpia service has a general problem with honouring privacy. People have been reporting that the account deletion requests are not honoured and their data is continuously used for outbound invitations, and their – supposedly deleted – photographs being shared with others.

So do yourself – and all your contacts – a favour, and spam all these messages asap, without clicking anything.

Written by yossale

June 22nd, 2013 at 10:15 pm

Posted in Uncategorized

Maven – Building a Java-Groovy mixed project

without comments

I have a project that mixes Java and Groovy, and the main problem I ran into was that the jar was nicely built – but it only contained the Java classes, and none of the groovy ones.

When I ran the groovy compilation plugin, I got only the Groovy classes, and none of the Java ones…

So, what you need to do to compile both of them and have a normally built jar it this:

In your pom file, after all the dependencies and properties, just add this build section:


  src/main/groovy
  src/test/groovy
  
    
      ${project.basedir}/src/main/resources
    
  
  
    
      org.codehaus.gmaven
      gmaven-plugin
      ${gmaven-version}
      
        
          compile-groovy-classes
          
            compile
          
          compile
          
            
              
                
                ${project.basedir}/src/main/groovy
                
                  **/*.groovy
                
              
            
          
        
        
          compile-groovy-tests
          
            testCompile
          
          test-compile
          
            
              
                
                ${project.basedir}/src/test/groovy
                
                  **/*.groovy
                
              
            
          
        
      
      
        1.7
        
      
      
        
          org.codehaus.gmaven.runtime
          gmaven-runtime-1.7
          ${gmaven-version}
          
            
              org.codehaus.groovy
              groovy-all
            
          
        
        
          org.codehaus.groovy
          groovy-all
          ${groovy-version}
        
      
    
    
      org.apache.maven.plugins
      maven-surefire-plugin
      2.8.1
      
        true
        true
        plain
        true
      
    
  

Written by yossale

May 21st, 2012 at 4:48 pm

Posted in Frameworks,Java

Tagged with ,