Friday, May 23, 2025

Yet another blog moves on

I started this blog around 13 years ago. I would like to have said that it is nice to see this grow up to a teenager. But I don't think I can say that. I abandoned this blog more than 7 years ago, and started focusing on my github blog. Today I am declaring the end of this blog. Please follow my posts on https://debamitro.github.io

Saturday, December 30, 2017

Smallest electron app written in clojurescript

I was toying with GUI app development in Clojure, and I tried Seesaw. It was good, however the startup time of the app didn't please me. So I started off the Electron route instead, and decided to use Clojurescript. Here is how a very tiny electron app written in Clojurescript looks like.

Source files

There will be two sets of source files - some for Clojurescript and some for electron. We start off by creating a project using Leiningen

lein new cljstest1 

We will use the lein-cljsbuild plugin for our convenience, so in addition to modifying the dependencies section in the project.clj file to

  :dependencies [[org.clojure/clojure "1.8.0"]
                 [org.clojure/clojurescript "1.9.946"]]

we will also add a plugins section

  :plugins [[lein-cljsbuild "1.1.5"]]

After this an execution of

lein deps

is sufficient to install all dependencies. We will also add some more options to the project.clj file, and what it looks like at the end is this.

The project.clj file

(defproject cljstest1 "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [org.clojure/clojurescript "1.9.946"]]
  :plugins [[lein-cljsbuild "1.1.5"]]
  :cljsbuild {
    :builds [{ :id "default"
               :source-paths ["src"]
           :compiler {
                 :output-to "app/app.js"
                 :target :nodejs
                 :main cljstest1.core
                 :optimizations :simple
                 :pretty-print true
                 :externs ["externs.js"]
               }
             }
    ]
  }
 ) 

As you can see we plan to generate the Electron app in app/app.js. The directory 'app' will be created soon. Before we forget we need to create the externs.js file which we will need in order to use __dirname from Clojurescript.

The externs.js file

var __dirname;

The core.cljs file

Leiningen generated a src/core.clj file for us. Let's rename it to core.cljs so that the cljsbuild tool finds it automatically. Here is what it looks like.

(ns cljstest1.core
     (:require [cljs.nodejs :as nodejs]))

(def electron-pkg (nodejs/require "electron"))
(def url-pkg (nodejs/require "url"))
(def path-pkg (nodejs/require "path"))

(defn create-window
  []
    (.
      (new
        electron-pkg.BrowserWindow
        #js
        {:width 800 :height 600}
      )
      loadURL (url-pkg.format
               #js
                {:pathname (path-pkg.join js/__dirname "index.html")
                 :protocol "file"
                 :slashes true
                 }
               )
      )
)

(defn -main
  []
  (.on  (.-app electron-pkg) "ready" create-window)
)

(set! *main-cli-fn* -main)

The last line which updates the *main-cli-fn* variable is a very important part. Without that node.js won't be able to run the app.

After the Clojurescript part we need to generate the Electron part of our source. We need to create a directory 'app', and inside it place the following package.json file

The package.json file

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
      "electron": "~1.7.8"
  }
}

In this directory, we will also place a index.html file

The index.html file

<html>
  <body>
    <h1>
Ha!</h1>
</body>
<html>

Steps to compile and run

  1. cd app; npm install; cd ..
  2. lein cljsbuild once
  3. cd app; npm start

Monday, November 6, 2017

World's smallest hugo site

I have been playing with Hugo for a while and it is
  • easy to set up
  • fast
  • flexible
  • well-documented
Here's how to create a tiny single-page website using Hugo.

Generate the source code to the site

Create a directory for the source code, and give this command

hugo new site <directory-name>

Add some basic customization

Go to the source code directory and open up config.toml and edit any field you want to. If you don't it is fine. Generally I disable unnecessary stuff like RSS feeds etc, so I add

disableKinds = ["RSS"]

 I also want to control where the generated website goes, so I add

publishDir = <directory-path-within-quotes>

Create the html page

Here is a simple html page for starters

<html>
  <head>
    <title>
      Home page of XYZ

    </title>
  </head>
  <body>
    Nothing of importance at the moment
  </body>
</html>

Save it to layouts/index.html

Generate the site

The command is, simply

hugo

That's it. Your website is generated in the output directory 'public' or the value of the variable publishDir if you changed it.

Notes

I know that this is probably not the prescribed way of using a static site generator. Most people are talking about a separation of concerns, where your theme and layout are not connected with your content. I feel that that should not be the only way to create websites. It is possible for content and layout to be mixed for very small sites. Or when you start building the site. Later on you can gradually move the content out and create a stand-alone layout.

Sunday, November 5, 2017

World's smallest electron app

Electron seems to be a very easy-to-setup system if you already have node.js and npm. I could get a small app running even on Windows. Here's what you do.

Create a package.json file

You can use 'npm init' for this, or just your text editor. The example I can give is

{
  "name": "electron1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "~1.7.8"
  }
}

Create an index.html file

This will be the default screen of the app. The example I can give is

<html>
  <body>
    <h1>
Ha!</h1>
</body>
</html>

Create a index.js file

This will be the code behind the app. The example I have is

var electron = require('electron')
var app = electron.app

var path = require('path')
var url = require('url')

let mainWindow


function createWindow () {
  // Create the browser window.
  mainWindow = new electron.BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
 
    // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

Launch the app

There is a one-time command you need to execute:

npm install

After that is done you can launch your app using

npm start

Credits

This is nothing but a reduced version of the Electron quick-start app

Wednesday, November 2, 2016

Writing a web app

Recently I have started writing a web app, which I was trying to build for a long time. This time I am much more closer to completion than ever before, and I feel I can easily complete it.
I want to think about why I couldn't do this for a long time, and why I have done a large part of it in a couple of weekends. Possible reasons coming to my mind are:

  • I was misguided by many blog posts
  • I made my first attempt using PHP, just because I knew the language
  • I was focusing on the wrong part of the app
  • My day job and my life in India was draining out all of my energy
  • I was trying to do too many things and not doing most of them
I'll know better when I actually get to launching the first version of the app. So far, here are my learnings:
  • Prefer podcasts over blog posts, if you have learn from the net
  • Put highest preference to books - buy them or loan them from a library - they are worth every penny
  • Start your prototype as a front-end app, using html and javascript. Use jquery extensively.
  • Use a Javascript array as your database, to begin with. It will be good enough for most prototypes.
  • If you are stuck, stop and read a book or discuss with someone
  • Don't start with any app framework - you don't know what you need.
I'll stop here and resume when I have made more progress

Friday, October 28, 2016

My Javascript workflow

So I am trying out my hand in JavaScript, and want to talk about my workflows. I have two development setups - one on a cloud VM which is a DigitalOcean droplet, and the other is my laptop which happens to run Windows (since it is provided by my office).

On the cloud machine development is super easy. I connect to it using putty from Windows, and write my code using emacs, and run a python server (python -m SimpleHTTPServer) in another shell. I run both these shells inside a screen session which I keep alive, and detach when I logout. I can view pages served by the web server on my local machine's web browser by going to my VM's IP address and using the port 8000 which is what the python module uses by default.

On my Windows laptop it seems things are not that hard. I write my code using Notepad++ or Brackets, and run a node server (http-server.cmd) from the Windows PowerShell. I can view the web pages on my web browser by going to http://lopcalhost:8080 as 8080 is the port used by the node.js module. The Windows port of node.js looks pretty good, which means a lot of web development can be done on PCs now. How do I use git? Well github has a Windows desktop client which makes development in github repositories a breeze!

Tuesday, October 11, 2016

Poster inspired by podcast on abstractions

I am being mindblown by the world of podcasts, and programming podcasts in particular. Today I heard this episode of "this developer's life" where there was a nice quote at the end. I decided to make a poster out of it.

Wednesday, September 21, 2016

Revisiting Javascript

Javascript was the first programming language I learnt, back in 2001, about to leave high school. In college I somehow 'learnt' that these were not real programming languages. The real languages were C, C++, Java and the like. I gradually drifted towards C++, and then worked in C and C++ for a decade in the industry.

Now I am stumbling upon one thing after the other about Javascript which makes me think - god why didn't any of the tutorials tell me all this back in 2001! Specially about the bits of functional programming possible in Javascript - and how you can quickly built complex programs with it.

I just want to list down the nice things I've encountered in the past few days:
1. A blog about how to write a programming language in Javascript
2. Marijn Haverbeke's book 'Eloquent Javascript'
3. A talk from EmpireJS on Knitting in Javascript
4. A 30-line implementation of a simple spreadsheet in Javascript
5. A blog post about variable scoping in Javascript

The language is confused and allows multiple paradigms - but so is C++. The best thing about Javascript is that it is much easier to use functional programming techniques in it. And the even better thing is that most devices/platforms seem to be supporting it. I am tending to believe that the bigger things of the future will be built upon powerful programming languages or systems. I am gradually moving away from the importance of building faster low-level programming systems.

Saturday, April 4, 2015

A command-line user's guide to building Android apps

I'd like to document my bare-bones Android app development work flow which is entirely command-line-based. The internet is full of app development guides using Eclipse or other IDE's, but resources for command-line users like myself are rare. I don't know why this is so, because the command-line is heavily used by programmers in most other domains I know.

Creating a new project

cd <android-sdk-dir>/tools
android create project --target <target-id> --name <name-of-project> \
--path <path-where-you-want-to-keep-your-project> \
--activity <first-activity-name> \
--package <app-package-name> 

For target-id do
android list targets
to see the available targets and select an id from them

Example:
android create project --target 1 --name MyFirstApp \
--path ~/Apps/MyFirstApp \
--activity MainActivity \
--package com.example.myfirstapp

Making changes

I use emacs to edit the java code generated, and the layout xml file main.xml. To test any non-Android-specific piece of functionality it is a good idea to try it out in a stand-alone .java file outside the project. I have done this for trying to do a http request and it has helped.

Building the app (debug version)

cd <your-project-directory>
PATH=<android-sdk-dir>tools:$PATH ant debug

Launching the app in your emulator or phone

Start up your emulator by opening the avd manager:
<android-sdk-dir>/tools/android avd

Otherwise, plug in your phone and turn on USB debugging.

In your project directory, do:
<android-sdk-dir>/platform-tools/adb install bin/<app-name>-debug.apk
<android-sdk-dir>/platform-tools/adb logcat

Your app is installed, run it and look at the logcat output running on your command-line to debug what went wrong. I am assuming you have put enough Log.d() messages in your app code.

After you're done, you can uninstall the app by doing
<android-sdk-dir>/platform-tools/adb uninstall <package-name>

where package name is that com.example.myfirstapp or something you gave while creating the project

Note: Google's documentation has all of this information - you just have to fish it out since their main focus seems to be Eclipse/Android Studio based development.

Wednesday, April 1, 2015

How to learn programming

There is a huge 'learn coding' wave nowadays. Coding is, as we know, common parlance for computer programming. Many people are prophesying that coding will soon be the new literacy. I don't totally agree with this with stand, but I do realize that programming a machine is a fundamentally new invention of mankind (like the invention of writing) which will lead us into different ways of thinking (like writing did).
What I don't really agree with is the numerous 'teach coding' programs around.

I'd like to give my views on programming and how to learn it, from my own experience of more than 10 years (college+industry). The first and most important thing to know is that programming cannot be taught. It can be learned, by doing it. Yes, you will get stuck every day while you are programming and then you will have to refer to some guides, or consult an expert, or listen to a class. But your guide or your teacher will only give you inspiration, direction, and clarity. And also what he/she thinks.
But programming is like human thought -- it is continuously evolving. When you are stuck at a problem, you might come up with a different way of programming which no one has done yet -- and this has been happening regularly for the last few decades.

As I see it, the way you advance in programming is:
1. get inspired and get some direction, by talking to someone, or listening to a good classroom lecture, or whatever works for you
2. get started and get stuck as soon as possible -- getting stuck is crucial to learning programming (it is crucial to any learning)
3. go back to the references, talks, scratch your head, and come back to step 2.
This is why I like programming so much -- it is such a very practical art which one cannot learn theoretically.

Wednesday, March 11, 2015

Hurdles in android development

I have recently started trying to create an android app. Belonging to the command-line world of C/C++-gcc-make-linux this entire IDE-based development system is a big puzzle to me. I am not averse to IDE's. I wrote some of my first programs in Visual C++ 6 which was a terrific IDE. But somehow I could never get the hang of Eclipse, and could never appreciate why people used it when it took so much time to start up (VS was fast).
Coming back to android, I have succeeded in building a bare bones app from the command-line, and using Google Play services in it. The problem is that most guides on the internet (including Google's) lean heavily on the Eclipse or Android Studio paths. I couldn't find much guidance for the problems I have faced so far, and they have taken me a long time to figure out.
For example, when I tried to integrate Google Play services (for using GPS location data in my app) everyone told me about copying the google_play-services_lib folder to the libs directory of my project, and also updating the project.properties file of my project. But no one told me about putting a build.xml file into the google_play-services_lib folder. I learnt it the hard way.
The latest problem I was facing is that I couldn't install apps onto my phone because adb was complaining about my device being unauthorised. I did run my app on the emulator -- but that is a very slow way and practically useless for testing anything apart from the UI. Today, I finally found the answer to my problem in this stackoverflow post. And my app now installs merrily on my phone.
My next target is adding unit testing to the app's code, and also improving the location-finding capability. Let's see how I progress.

Sunday, December 21, 2014

Not so functional yet

Functional programming still seems an elusive concept to me -- something that I am trying to pick up but can't yet apply anywhere. The big boost came from the SICP lectures by Sussman and Abelson which I started watching from sometime in the middle of the year. Now, as the year is about to end, I have watched only till lecture 4A. And I cannot think of any way to apply what I have learned. A few days ago I was watching a talk called the 'The Feel of Scala' and I felt Scala was nothing but Java with a powerful front-end which does a lot of rewrites. Nevertheless it was clear that the features provided were for functional programming. Yesterday I downloaded Scala and tried to get the basic control structures and syntax. But again, I hit a wall when I started thinking -- where do I use these features? I know Scala is giving me immutable lists but so what about that? Maybe I'll have answers after I finish the SICP lecture series.

Monday, December 1, 2014

The first task

I could have written this entry in my general blog as well, since unlike regular technical blog posts this one doesn't involve any code. But I am choosing to put it here because I feel it is an important topic which all technical teams need to understand.
Let me get down straight to the point I want to make - how the developer you hired will perform depends a lot on what is the first major task you give him/her. Don't give your dirtiest task which others in the team are 'too important to do'. I have seen this happen to different people 4 times in my 9 year career. Yes, it once happened to me as well, long ago.

So what I am talking about is the following sequence of events:
1. New candidate is joins a technical developer position after tremendous technical interviews
2. A few days after joining, the 'technical' manager asks the candidate to do one of the following as a prime activity for some time:
a. analyse everyone's regression failures every day and/or delegate them to the right engineers
b. manage some infrastructural role for keeping the codebase in sync with components it depends on

I have observed this so far in Indian technical teams only. I don't know if this is a fall-out of our society's caste system where some 'dirty' work is left to be done by a certain group of people only -- that is something for sociologists to comment on! But in effect it is equally devastating to the new candidate's morale and growth.
I guess some 'technical' managers dream of this as the ultimate way to get started with understanding a new product and team. They could not be more wrong. The best way to get someone started is to give him/her good problems to solve. I understand the best work has to be reserved for more experienced candidates, but nothing can justify giving some non-development tasks to a developer at the beginning. I also appreciate the need for developers to be able to do all such stuff, but that should come in time after a developer has started contributing smoothly to a codebase. Only after you have started managing own your work well can you afford to do some housekeeping for others.

Saturday, June 21, 2014

Insecure coding talk by Olve Maudal

I was introduced to Olve Maudal by a friend who used work in a company called Tandberg (which finally got acquired by Cisco). This guy is a real expert, one who can explain things nicely as well. I just finished watching his talk at this year's NDC. It is titled 'Insecure coding in C and C++'. I think it is a must watch for all C programmers. The concept of 'Return-Oriented Programming' just blew me away.

Insecure coding in C and C++ from NDC Conferences on Vimeo.

Tuesday, June 17, 2014

MIT's take on web development

Yesterday I was reading the introduction to a book written in 2006 by some MIT professors titled 'Software Engineering for Internet Applications'. I was thrilled at the level of ideas they proposed, many of which are a reality now. For example, here is an excerpt:

"...Speaking of mobile browsers, their small screens raise the issues of multi-modal user interfaces and personalization. With the General Packet Radio Service or "GPRS", rolled out across the world in late 2001, it became possible for a mobile user to simultaneously speak and listen in a voice connection while using text screens delivered via a Web connection. As an engineer, you'll have to decide when it makes sense to talk to the user, listen to the user, print out a screen of options to the user, and ask the user to highlight and click to choose from that screen of options. For example, when booking an airline flight it is much more convenient to speak the departure and arrival cities than to choose from a menu of thousands of airports worldwide. But if there are ten options for making the connection you don't want to wait for the computer to read out those ten and you don't want to have to hold all the facts about those ten options in your mind. It would be more convenient for the travel service to send you a Web page with the ten options printed and scrollable...."

What is being proposed has already been partly achieved in the 'responsive' web design trend going on for the last two years!

The best  part I liked was about how web applications could fulfill the unmet goals of humanity:

"...What are the enduring unmet human goals? To connect with other people and to learn. Email and "reference library" were the two universally appealing applications of the Internet, according to a December 1999 survey conducted by Norman Nie and Lutz Erbring and reported in "Internet and Society", a January 2000 report of theStanford Institute for the Quantitative Study of Society. Entertainment and business-to-consumer e-commerce were far down the list...."

Saturday, June 7, 2014

A tool to solve any problem

Last year I developed an itch for learning functional programming (so much so that I signed up on a functional programming contest!), and that led me to Brian Harvey's course in UCB (search for CS61A on youtube). I enjoyed that immensely and watched more than 10 lectures from the series. A few days ago when I mentioned this to my friend Sayamindu, he suggested I watch the SICP lectures by Sussman and Abelson. And so I started watching them as well, and I now like these even better. Compared to Brian Harvey's course this one is faster and much more intense. Of course I might have felt that this was too abstract if I hadn't watched the previous one.
So far it feels any big or complicated problem can be solved programmatically using lisp. Since I have been working only on big and complicated software systems for most of my career, I am really looking forward to use it fruitfully.

Thursday, May 1, 2014

Inspiration to write your own language

This line set me thinking of various possibilities today:

"...So, you want to write your own language? All I can say is: Certainty of death. Small chance of success. What are you waiting for?..."

Read the full article by Walter Bright, the creator of D, on DrDobbs site.

Tuesday, March 18, 2014

A hidden insecurity of phones

This talk was a pretty alarming one. There are at least two OS's inside every smartphone, and the communication with your service provider is done via a proprietary RTOS which shares the same RAM as your Android/IOS/etc. Corrupting the RTOS is apparently pretty easy.
I watched it yesterday:

Tuesday, March 4, 2014

Future of democracy by Smari McCarthy

I watched this talk today. It really blew my mind. I suggest it strongly for those of us who are working towards bringing democracy in our respective countries. The speaker is, among other things, a founding member of the Iceland Pirate Party.

Monday, February 24, 2014

Local sharing

I was looking for a way to copy some of my music recordings with my teacher from my office laptop to my mac and realised I really don't know any easy way to do that. Its not that I don't know of any way to do this -- I have copied lots of stuff by connecting two machines with a LAN cable years ago before internet was so widespread. But I don't know a real sleek and easy way like sharing over dropbox or gmail which we use all the time. I think we have too many apps based on the internet today. We forget that now we all have a LAN at home -- a wifi router which all our powers all our internet-elabled devices. Shouldn't we have more apps which lets us chat and share over LAN?

By the way I found this excellent lifehacker post which I eventually followed. However I'd still be happy to see something sleeker and easier.