Pages

Friday, April 29, 2022

Trust, advices and decision-making

Disclaimer: this post is based on personal (i.e. not scientific) observations.

In one of my recent meetings, there was an example of people seeking help from dating services and the advices given to them, as follows:

You say that your first impressions are not good. You show up here  at the dating service office  in tee shirt and short pants and you say that you go on your first dates wearing similar clothes. To improve your first impression, you should try dressing up a little.

To someone who is clueless about fashion, this stuck with me for a few days after the meeting. While walking my dog and thinking about the advice above, I reached the conclusion that if I were the one receiving it, I would have given up on match making.

Why? Because I'm clueless about fashion. If you tell me to go to a specific hair dresser, have a specific hair cut, buy a specific shirt at a specific shop then I can do all that, but if you just tell me "dress up" then I'm totally lost, overwhelmed by the infinity of options to choose from.

Another thing to consider is my capability — or lack thereof — to learn about fashion. You could say that it's not that I'm incapable but only unwilling to learn it. Regardless of which statement is correct, the end result is all the same: I am not capable of making fashionable decisions. And all of us have this set of topics that we feel at a loss, be it sports, math, finance, relationships, etc.

Other topics, however, are very interesting or easy to learn even if we don't know about them. Those topics we can dive deep and enjoy the process until we reach a decision that best fit our needs and preferences. I like web technologies and I enjoy the process of searching for a new web framework to learn for my next projects, even if they introduce new languages or concepts: I do want to make that decision myself!

Finally, there are topics we can make instant decisions about, without a flinch. It could be something we're deeply knowledgeable about, experienced, or even just careless. For this kind of topics, we may just want an overview of the options, maybe some hints and directions, but we don't want to be showered with detailed advices because, well, we know what we want.

So there are two variables here: the Decision-Making Capability (which includes the will or lack thereof to learn) and the Advice Granularity (the level of detail of the advice). Plotting a chart, it becomes something like this:


While explaining to my peers about my need for specific advices, I gave another example. I cannot cook. More precisely, I can only cook with specific instructions. "Salt to taste" "mix until it turns brown" are examples of unclear instructions, whereas "1 teaspoon of salt" and "mix for 5 minutes" are clear instructions. With a few exceptions, recipes have unclear instructions that confuse me and many many many mistakes were made. Only when my wife instructed me to "cut ingredient A in size B, add C tsp of spice D after adding E" and so on that I could lear how to cook (only this dish). For dinner tonight, she vaguely asked me to make salad and well, I did make it, but it turned into a mountain of mixed vegetables because I need specific amount instructions.

Then a colleague asked me how could I blindly trust those specific advices. I thought about it for a moment, and realized that it's not always easy to trust just because it's something I don't know about. If it's an important decision such as investing my family's entire fortune on something, I would absolutely not trust the people giving me advices so casually.

So, again, two variables here: the Trust Level (on the advisor or the advices given to me) and the Importance Level (of the decision that I am expected to make). Plotting a chart, it becomes something like this:





Thank you for reading! Feedback is welcome.

Thursday, December 4, 2014

Run cron job in Sails.js

This is based on the discussion about the topic here, so it's not exactly a "cron job" but it can be executed like one and also have access to all the nice features that Sails.js provides, including models.

Note: the version of Sails.js as of this post is 0.10.5.

Install node-schedule.

$ cd /my/sailsjs/app_root/
$ npm install node-schedule


Update /config/bootstrap.js so that it looks like the code below:

module.exports.bootstrap = function(cb) {

  // add the lines from here
  var schedule = require('node-schedule');
  Object.keys(sails.config.crontab).forEach(function(key) {
      var val = sails.config.crontab[key];
      schedule.scheduleJob(key, val);
  });
  // add the lines until here
  
  cb();
};


Create file /config/crontab.js and add the code below:

module.exports.crontab = {

  /*
   * The asterisks in the key are equivalent to the
   * schedule setting in crontab, i.e.
   * minute hour day month day-of-week year
   * so in the example below it will run every minute
   */

  '* * * * * *': function(){
      require('../crontab/mycooljob.js').run();
  }
};


Create directory /crontab, then create file /crontab/mycooljob.js and add the following code:

module.exports = {
    run : function(){
        console.log('do something very cool here');
    }
};

Run sails lift and after a minute, you should see "do something very cool here" in the console.

Monday, January 20, 2014

Compiling and creating jar of JSON Java

This is mostly going to be a "note to self" since I'm going to cover some very basic stuff such as:

  1. get code from github
  2. compile Java files into .class files
  3. create jar file
  4. import into Eclipse
I happened to need a JSON parser in a  Java project and chose the reference implementation from json.org site.

$ # 1. clone Git repository (it's like svn checkout)
$ git clone https://github.com/douglascrockford/JSON-java
$ 
$ # 2. change directory to the downloaded JSON-java directory
$ cd JSON-java/
$ 
$ # 3. add all the .java files in the current and subdirectories into a file called sources.txt
$ find . -type f -name "*.java" > sources.txt
$ 
$ # 4. create a directory to place the compiled .class files called build
$ mkdir build
$ 
$ # 5. tell Java compiler to read the files from sources.txt and place the compiled files in the build directory
$ javac @sources.txt -d build
$ 
$ # 6. change directory to the build directory
$ cd build/
$ 
$ # 7. create jar file named JSON-java.jar including all (*) files in the current and child directories
$ jar -cf JSON-java.jar *
$ 
$ # 8. copy jar file to Eclipse's WEB-INF/lib/ directory
$ cp JSON-java.jar /path/to/eclipse/workspace/project_name/war/WEB-INF/lib/

After the jar file was copied to /war/WEB-INF/lib/ directory of the Eclipse project, open Eclipse and "include" it:
  1. from Eclipse's menu, open Project > Properties
  2. on the left pane, choose Java Build Path
  3. select Libraries tab
  4. click on Add External JARs button
  5. browse to the /war/WEB-INF/lib/ directory of your project and select JSON-java.jar
  6. click OK to close dialog
  7. in any .java file, in the import statements, try to add the following import:
import org.json.*

If it doesn't become red it means everything is fine. If there's anything wrong, check each step, especially the generation of .class files.

Cheers! 



Tuesday, January 14, 2014

Workaround for timeout issue in Titanium's WebView.evalJS method

First of all, yes, I'm using Titanium because I want to save time writing code for each platform and I like JavaScript.

I'm writing an app that uses WebView and came across this bug. At first I thought I would have to either wait or write my own Android module, but this hack gave me the hint I needed: bookmarklets! If you don't remember, a bookmarklet is just a JavaScript code that runs when your browser tries to open a URL that starts with javascript: instead of http://.

The only thing that was needed was to prepend any JavaScript code with javascript: and remove any "junk" as suggested here:

  1. comment lines
  2. convert tabs into spaces
  3. convert multiple spaces in a single space
  4. remove leading and trailing spaces
  5. remove line breaks
After the "cleanup" above, I also thought that it would be necessary to URL-encode the result, but it's NOT necessary to do so with Titanium WebView because somebody else (other then myself) is URL-encoding it under the hood.

So, putting everything together:

var jsCode = "" +

// this line starts with TAB and ends with line break
"\t\t// this is a sample comment line that should be deleted\n" + 

// this line has multiple spaces between "body" and "="
"\t\tvar body                = document.body;\n" +

// this line has leading spaces
"        var contentLength = body.innerHTML.length;\n" +

// this line has trailing spaces
"\t\talert( 'Your BODY tag has ' + contentLength + ' characters!' );    \n";

// remove comment lines
jsCode = jsCode.replace(new RegExp("^[\s\t]*//[^\n]+[\n]","gm"),"");

// convert tabs into spaces
jsCode = jsCode.replace(new RegExp("[\t]","g")," ");

// multiple spaces as single space
jsCode = jsCode.replace(new RegExp("[ ]{2,}","g")," ");

// remove leading and trailing spaces
jsCode = jsCode.replace(new RegExp("^[ ]*|[ ]*$","gm"),"");

// remove line breaks
jsCode = jsCode.replace(new RegExp("[\n]","gm"),"");

var prefix = "javascript:(function(){";
var suffix = "})();";
var finalCode = prefix + jsCode + suffix;

Ti.API.debug(finalCode);

var myWebView = Titanium.UI.createWebView({url:'https://www.google.com'});
myWebView.addEventListener('load', function(){
 myWebView.setUrl(finalCode);
});

var myWindow = Titanium.UI.createWindow();
myWindow.add(myWebView);
myWindow.open();

Cheers!

Thursday, January 9, 2014