Monday, April 14, 2014

Better way of combining path in C#

You could do string operation to construct path as below :

string path = path1 + "\\" + path2;

However, more portable and cleaner way would be to use Path.Combine as below :

Path.Combine(path1, path2);

The above avoid you to make manually ensuring the correctness of path separators.

Monday, March 24, 2014

Choosing Logging Library for .Net : NLog

When it comes to logging, printing it out yourself to console or file probably the most straightforward way. However, if you want to get more structured in your log, with level and flexible on/of switching you'll need a more dedicated logging library. However, like any library, choosing the sweet spot where you get the feature you want but with the least complexity and learning curve can be challenging.

As for logging, when I research around the two that pop up most is log4net and NLog. The choise is pretty simple though, log4net is good but pretty outdated now while NLog is quite active, well-supported has rich feature. So, the choice goes to NLog.

Quick Usage :

After you link the library (using NuGet or manually). Declare the logger in your class :

private static Logger logger = LogManager.GetCurrentClassLogger();

and call at the place that needed it :

logger.Debug("log message");

You can add a lot of additional details to your log with its predefined variables. You can consult the manual to see what is available. Also, since you declare logger on each class, it can automatically  print in which class and the point where the log is called. It is quite useful for debugging.

Monday, March 10, 2014

Lesson about delegation from OpenCL

Using OpenCL and the complexities / benefits that comes with it reminds me of an issue of trade-off in delegation. You can always do things the simple straightforward, serial way but there is a limit of how much and how fast it can finish the tasks. We can scale things better by utilizing more external agents and manage it. However using additional agents means delegating tasks to those agents and manage the overhead and unpredictability that comes with it. This is why the code to achieve the same thing using OpenCL can be quite complex since you need to manage the data and logic that comes to and from those computing units and get them to work together properly.

It's not just cost that is increased though, the risk is also higher. When you delegate, you add another factor with its own set of quirkiness to your system. Each agent you add, you add another risk of it fail in some way and effect the overall system. In OpenCL, certain GPU might not behave as it should, your calculation might fail in certain configuration. So, you will need to add more activities to manage the risk. How it can be anticipated, localized and how the system can cope gracefully when it happen.

So, since delegation is not free it is important that the scale of the problem would need to justify the use of delegation.  You can add more things, method, people to your system, but when it does not fit your scale, it will become a burden since your output, what you get, is lower than what you need to expense. In term of OpenCL, the low complexity or low number of calculation probably not worth the trouble. For example, even when you ding the formula is complex and can be paralellize but if your expected data to be processd is not much, it probably better to just code it the traditional way.

Delegation can be costly and risky, but if you need your solution to scale better, the return can be very much worth it.

Friday, February 28, 2014

Translating Traditional Program Logic to OpenCL

If you just started to look at OpenCL and see the example of the code, it can be quite confusing on how it got that way from its traditional program logic counterpart. Take a look at an example of comparison below taken from PDF Overview on OpenCL site :

You can see that the calculation is intact but you can't seem to find where the for loop counterpart is.

It is because, the logic has been separated between host (your main program) and kernel (that will be sent to device). So, you need to look at both of them to get the whole picture. There is some data partitioning done that you can take a look at the code at the host. Together with the batch processing happen in kernel code you can start to see the implied loop there.






Due to its parallelism and batch processing nature, the logic in OpenCL can look more declarative and more separated compare to traditional one. So, the logic that you expect to find will probably be implied somewhere in host and kernel code. This could help if you try to read the existing code.

Thursday, February 27, 2014

Getting Started with OpenCL

I started working with OpenCL recently on the project at work. It's quite exhausting to learn initially since OpenCL has quite steep learning curve. However, once you have started to picked up some of the core concept, it could be quite interesting. Also, you will add another valuable tool to your programming arsenal, especially in the aspect of performance and speedup. You will start to be able to exploit not just CPU but also any other computing device that exist on the system like GPU quite conveniently.

Here's summary and pointers of how to get started with it from what I note so far.

Overview

You can get an initial overview from the surces below :

 As for its Wikipedia Page, I don't find it really informative at the moment, at least if you want to get an overview of what OpenCL is and its relevance.

As usual, google search and some blog entries would not hurt too. However, it's better not to take too much time researching things online since I think your time is better spend reading the book below.

Book : OpenCL in Action

There is not many technical book that you can enjoy reading cover to cover. This one is one of them. It's better you read from beginning to end serially. It build up from chapter to chapter. Also, it does not cross reference things too much between materials which could be annoying on some technical books. The materials coverage is very good, from the history, introduction to advanced topics.

If you have some questions on your mind from reading other sources previously, there is a good chance that you will start to piece things together when reading this book. It gives a good foundation for understanding why OpenCL is the way it is. OpenCL syntax and functions will still be quite overwhelming even after you finished with the book but knowing the concept behind will make it much more manageable.

I find the highlights of the book is its use of analogies that helps a lot in explaining the abstract concept and architecture of OpenCL.

Revisit Specification and Reference Card

If you have skimmed the specification and reference card above, it's time to revisit it again. You will now find it much more clearer now and they are starting to be more useful. It will also add more details from what is covered on the book. Personally, I really like the layout on reference card which help in getting a good high level picture of different parts of OpenCL with it's colorful boxes and groupings.

At this point, you can start to work on your specific issues and continue learning from there. As usual, after the initial learning, the actual work is where we will learn most. This is especially true in complex technology like OpenCL.

Thursday, February 20, 2014

Migrating to Android from iPhone

My iPhone4 got the "people can't hear me" problem. I've exhausted all the possible trick to solve it that does not require opening up the hardware. There is little-used s3-mini laying around so I decided to try to migrate to it and see what happen.

I expected some limitations when moving to Android from iOS since I have been accumulating many apps and it has quite integrated to my daily workflow. However, after sometime, I can say that the migration is quite seamless. Most of my usage can be transferred to Android plus some more Android-specific goodies (I think widget is cool). More about this below.

The Apps

At this point, almost all the well-known apps have both iOS and Android version. So, for apps like Evernote, Springpad, Pocket, Feedly etc. I don't have any problem at all. There is slight differences here and there but for my personal usage there is nothing significant that I find missing.

For less well-known apps I usually can find the comparable counterpart. I have to say though, the apps on iOS feels more "elegant" somehow. However, if you care mostly for the function, it's quite negligible since Android now has large number of well-written apps.

Surprising Finding on Navigation Apps Department

I have Garmin Indonesia on my iPhone and it is sad to see that there is none of it on Google Play Store. I have searched for the alternatives on several occasion with no luck until I stumble upon Polnav EasyDriving app. It's quite "hidden" from my previous search, probably because it's google playstore page uses non-latin characters mostly.

Surprisingly, it works really well. I used Indonesian map from navigasi.net (which is a really great crowdsourced map, BTW) and it is loaded very nicely. The navigation screen is clear, fluidly updated and give me several notification when getting close to turn to make sure I don't get past it. Really cool!. And what is even more surprising is that is free which is quite unusual for offline gps navigation app nowadays.

The search for address is really good but somehow there is no POI-based search that I can find. This is quite unfortunate and hopefully it will be there on future versions. Aside from that, I have no complain for it.

What is Missing

One thing that I miss quite a lot is a good freeplane-compatible mindmapping app. There is iThoughts in iOS but none that comparable to it on Android. Fortunately, I have been "flattening" my data recently and many has been migrated as Evernote or Springpad data. Some of it are still on freeplane format though and for those I still have iThougts on iPad. So it's not really a critical issue actually but it will be nice if I can still accessing my mindmaps on the smartphone too.

My iPhone4 is not going to retire soon though, I still have it laying around and use the apps on it occasionally. It basically now function more as and iPod than an iPhone. For example, I still use Garmin on it although I already have Polnav on Android as mentioned above. It is still useful to use GPS on separate device on some circumstances.

So far I quite enjoy the migration and I can't say it's better or worse. It just different, I guess.

Tuesday, February 04, 2014

Import Existing Repository to Bitbucket using TortoiseGit

When you want to import an existing local Git repository to bitbucket you can follow this official guide. However, one of the section involving steps with git command line as below :


I am used to using TortoiseGit on Windows and rely on it for all my Git command needs. So, I find it unpleasant to go to the shell to just do all the above. It feels like a leaky abstraction happening. I am sure there is more convenient way to do it using TortoiseGit instead. So, below  are the above steps done using TortoiseGit :
  • Right click on the folder->TortoiseGit -> Settings
  • Git->Remote and enter remote origin value as pictured below
 
  •  Right click on the folder->TortoiseGit ->Push
It looks more wordy but it is actually more pleasant to do in GUI-based environment.


Monday, November 25, 2013

Use Fiddler to help with development with Web API

Developing application that connect to Web API can be quite a pain if we are not equipped with the right tools. Network issues, response format, request parameters, could add more to an already existing complexities. On the early days, I usually use Firebug for most of the task to debug Web API. Lately, however, I rely more on Fiddler. It does all that Firebug do for me plus much more. Compare to Firebug, Fiddler the debugging feels much more active since we can modify and manipulate a lot of things to test different cases on Fiddler itself without having to write test code or scripts. It sits between the application and Web API so we can build scenario to test each side.

Here are some of its use :

  • Tracing request and JSON response. This is probably the most important ones. Tracing response on the code or by debugging in IDE can drain a lot of development time quickly. By monitoring it outside the application in realtime help us to focus more on the next step i.e: fixing code, add feature
  • Emulate scenarios. The request / response can be emulated in Fiddler which enable us to see how the server or client response on certain request / response format and content without having to write the code first and do expensive IDE-based debugging
  • Offline response. There are times when we want to rapidly and quickly test client behavior on certain response. Having to actually request to server could make the work take long since each request involve the whole request / response sequence. Fiddler could tap certain request format and give cached/predefined result instead. This will speedup the work since there is no actual network request happening. It could also help when we have issues with network availability when developing
Those are just some my main use cases that I've encountered mostly. It has many other tools that can be useful for various Web-API-related task.

Wednesday, October 30, 2013

XtraGrid : Use more specialized event for better code in handlers

DevExpress's XtraGrid is quite a complex beast. It's a library with tons of features and it can be overwhelming to use it optimally at first. So you might get your job done with certain way but overtime you find something new that you didn't notice at first that actually a better way than how you did previously.

That is the case when I just notice that XtraGrid has RowCellClick event that fires on specific cell click. I have been using Click event previously. Click event work just fine, however within it I need to have numbers of code to filter out cases that is not relevant to what I am trying to achieve. On the other hand, RowCellClick already comes with RowCellClickEventArgs with all the relevant info has been nicely parsed for use. The code get much cleaner when I adapt the code from handling Click event to RowCellClick.

So, the lesson is to use the most specific event that fit your case instead of the more general one. It can be a good guideline for using other parts of DevExpress library too. DevExpress components do a lot of inheritance from .Net components and with it inherit lots of pre-defined functions too. So, it has to do some specialization to extend the functions. Hence, it has some functions that is quite similar but slightly specialized.

Springpad + Evernote, Revisited

I wrote about using Springpad and Evernote sometime ago. My use of them since has evolved following the progress of those two services/apps. For example, Springpad note support was quite basic at that time but now it has become quite consistent and enjoyable to use. So, some of my setup has follow suit.

I now use mainly Springpad while Evernote is more for exceptional things like on the cases below :

  • Need things offline
  • Long writing
  • More "sticky" stuff like resources for projects
  • Scans and documents
Springpad has now can fulfill most of my needs. It's better not to have too much tools and have things duplicated so I cut down my Evernote uses. It still an important part of my system though within its scope. As for Springpad, here's ans example of how I use it for :
  • Manage and track media : movies, series, books, games, etc. It is really useful on this
  • Checklists : Springpad checklist support is quite advanced and it's what I have been looking for sometime
  • Project research and resource collection : Springpad has various type of content, not just notes, that makes collecting resources for projects really
  • Random notes, items and collections : for collecting interesting item from either online or offline that has not yet fit anywhere yet. I can search or organizing it easily later on

Friday, October 11, 2013

Visual Studio Deployment Project : Make Single Executable Installer using WinRAR

I wrote a while back of making a single executable installer for Visual Deployment Project (which output two file : setup.exe and setup.msi).  It was done using IExpress that comes with Windows installation itself. However, it feels quite outdated now. Even the page that I refer to in that post is no longer works :). It require some scripting and the use of IExpress is not exactly intuitive nor friendly.

So, comes more straightforward solution using WinRar. I stumbled upon this post on stackoverflow. It basically intend to do the same thing using self-extracthing-archive feature in WinRar. It works much better and more maintainable. It does not require external scripting and you can add logo, title, description for the bootstrapper. I find the resulting installer looks cleaner to run with helpful info/logo.

You can checkout the post above for some description and screenshot (hopefully stackoverflow link is more long-lasting). In summary, the steps are :

  • create New Archive
  • set to SFX mode
  • go to advanced tab
  • click SFX options button
  • customize things to your needs
The rest is about experimenting until you got the setting that suits your needs. After that,  you can save the setting (Tab General -> "Save current settings as default") and later when you create self-extracting-archive it will be done according to your last setup.


Tuesday, October 08, 2013

Note from "The Lean Startup"

I have recently finished reading "The Lean Startup" by Eric Ries. It takes an experimental (as in scientific method of testing hypothesis) approach to make the work more efficient (does not waste valuable time/cost) and efficient (working on the right thing).  It emphasize on the importance of rapid learning and adjustment based on that learning. In this aspect, it seems to use the same foundation of Agile Software Development i.e: make small iterations and adjust/improve process/prediction after each iterations.

Below is summary of the process on Lean Startup:

  • Make leap of faith. Make your hypothesis that you think is true, just make your best guest what will be useful and interesting to user so they will use it. Assume for now that it is true.
  • Develop Minimum Viable Product. Work on smallest possible set of feature that will have significant effect first. The smallness is important for having rapid feedback for learning
  • Validated Learning. Review the reesult of the above using actual/verifiable data
  • Pivots. Decide based on the learning above what is the best course to take next. This could be improving things, discarding things or drop it and do something else entirely
In essence, the process actually useful in many areas of life. Having a rapid feedback can minimize our accumulated errors.

Friday, October 04, 2013

Accurate and Consistent Naming, the simplest yet has the most beneficial discipline in programming

I think if you have to name one discipline that is the most simple but give the most return in term of maintainability, it's probably proper naming. You can do any fancy pattern or coding practice but if you are lazy in maintaining consistent naming of variables, functions, classes, your code will soon rot and unmaintanable. Ironically, it probably the simplest practice to do, especially with powerful capabilities in todays IDE and text editor in doing search/replace and rename-refactoring.

When you don't consistently naming everything properly in your code, you'll obscure your code intention thus making the maintenance hard even for yourself, let alone others. I prefer to write code that I can forget, meaning the code that when I visit it again in the future, it will take the least effort for me to remember what it is that I intent to express with it. Ideally, looking at the naming, structure and perhaps some external notes and diagram should be enough for  us get an understanding into the code again.

There is now a lot of guideline how to do consistent and proper in naming in codes. There might also some variation of guidelines between  languages, framework, platform. Whatever standard you use, any naming guideline/standard is better than none, I guess. Do yourself, and others, a favor and properly and consistently naming things on the code.

Thursday, October 03, 2013

xUnit : Low Cost/Commitment Unit Testing

I have been having love/hate relationship with Unit Testing so far. I understand the need and rationale behind it and often times I even enjoy doing it and makes programming more fun. However, when the tests has hit certain large  numbers and you accumulated significant cases where it just hard, if not impossible, to test (despite decoupling the code heavily and usage of patterns in high-dose), unit tests becomes a chore and annoying burden to maintain. You start to question yourself it's worth the trouble and should you just going forward without TDD.

I find the test framework that I use is a significant factor whether I keep unit-test the code. Having found xUnit sometime ago, I can now say again unit testing is functional, useful and fun again. Unit tests is support/driving activity of the main activity (coding), so the simplest possible support code/framework is the way to go.  xUnit has minimal "accessories" which make writing test code quick and straightforward. So far, it fits my need of unit test framework. For example, it get rid of Setup and Teardown and use very minimal set of attributes.

One important thing, psychologically for me, of having simple unit test framework is it makes my attachment to my unit tests relatively low. This means I can throw away, rewrite, change my unit test anytime and anyway I want with very minimal guilt. I can throw away large number of any unit tests that no longer serve any purpose and start over with the tests progressively. I can do architectural level changes with more freedom without unit test maintenance becoming too much of a burden. My focus now back to the main logic of program with unit test to support it.

Of course, at some level, unit test framework can be very personal choice like choices of your favorite text editors or OS. Personally, I like xUnit for my (.Net) testing framework. If you have been throwing unit testing from your development arsenal lately, you might want to try xUnit and see if it can interest you to get back to unit testing your code again.

Wednesday, October 02, 2013

Free solution for .Net Profiler : Slimtune

When it comes to profiling and performance tuning of application for .Net application, the proprietary solution can be quite expensive. However, I stumble upon SlimTune recently. It's a free .Net profiler and I find it quite sufficient for common profiling needs.



If you just interested to see your application performance hotspot, it already does that quite well. It's not fancy, sure, and the GUI need getting used to initially, but it does it's job fine. I can spot and tune the performance issue quickly with it.

Thursday, September 26, 2013

No Duplication for a Good Code and Good Life

More things in life does not always leads to a good quality life. If the things is duplicated, its function is already done by something else, it's existence is a clutter and use up unnecessary energy. The duplication is bad in code too, it is a nightmare to maintain the same logic that scattered on several places. One changes on one place need to be repeated on the other ones that use similar logic, otherwise you'll break the code.

Having enough things is not just about living a minimalistic lifestyle, it also a way to stay sane, to not use things unnecessarily. If we can simplify things internally, we can achieve more complex meaningful things with much ease.

Wednesday, September 25, 2013

Bootstrap : Programmer's Friendly Web Front-end Framework

I had task recently to port some web content into a mobile App. I translated some web-navigation navigation into native mobile one. For example, some section listing is translated into TableView in iOS. However, for the content pages itself I need to basically put the HTML content into an offline-bundled resources within the App.

Below is some problem with the task :

  • The Desktop version link to various online Javascript and CSS resources
  • The layout is for desktop and need to be made more mobile-friendly
It will be time consuming to try to proceed without some kind of reusable solution. After some research I settled with using Bootstrap to help with the it. It's a web front-end framework that bundle some Javascript and CSS into a convenient package. My plan is to offload the script/style to it so I can focus on porting the content.

The content of bootstrap pretty much suffice for the current typical need for multi-device HTML rendering. At least with it I can port the existing Desktop-based content pretty quickly for offline mobile consumption. The styling and layout is already fit out-of-the box to my need. I can strip the existing Javascript and CSS references and make the content use things from Bootstrap. There is still some labor work on the porting but doing on top of a good foundation makes it more manageable.


Thursday, September 12, 2013

JEdit : One Text Editor to Rule Them All (Functions and Platforms)

Any programmer would say his favorite text editor is the best one so I won't bother saying which one is best :). However, I do use one text editor, JEdit, for quite a long time already and find it a really useful text editor. Below is some of the reason :

  • It does "everything" I can think of and more. It has a rich plugin list and so far I haven't find any of my needs that is has not met. Apart from coding which I do mainly on IDE, I use JEdit for the rest. Editing and Viewing binaries with it's Hex editing plugins, Editing XML even using it as a full-fledged IDE for some language e.g: Ruby, Python.
  • It runs on all Platform. One of my main priorities of text editor is it needs to to be able to run on OS that I use. I did works on Windows, Mac and Linux so having text editor that can run on all of them is a big requirement for me. It saves a lot of time to be familiar with one advanced text editor and can run it on all system. The time you spend customizing the configuration, learning the shortcuts will pay off really well.
There is one situation that I can't use JEdit though and that is the time when I am working on command-line/shell. When that happens, VIM is my friend. So, I guess, JEdit doesn't really rule everything yet.

Wednesday, September 11, 2013

Code for your future self

It's quite annoying to work on existing code that you find hard to navigate and understand. It makes you spend some significant time first to understand the existing structure before you can finally add the feature or fix bug you intend to do in the first place. It does not have to be the code written by someone else, even your own code can give you some hard time when you revisit it again at the later time.

That is why the importance of writing good code is not just so the features runs well and the code looks good to your peer. It is also for your own benefit to have the code done in a well-designed and intuitive way at the moment you wrote it.

There will be temptation to code something quickly when deadline is approaching or you just feel lazy to extract those abstraction that you find. For your own sake, resist them and write code properly in the most natural and the most obvious way you can think of it. Your future self with start with that kind of intuitive thinking before he jump into specialize cases, workarounds and other tricks.

Here is an example of the things you can do to minimize the pain of your future self coding session :

  • Choose good naming. This is simple and highly essential but easy to break at the time when you feel lazy or under pressure
  • Have all the dependencies locally and self-contain whenever possible. For all dependencies that you can not put locally, put detailed documentatio on how to get it
  • Automate and simplify the build system. Again document unscrypted steps extensively
  • Make notes and diagram on general architecture. Give high level diagrams and don't forget to update them when it no longer reflect the actual code. Do not go into too much detail on this. Focus on  describing the big picture of architecture on the notes and diagram and let the code speak for itself whenever it can
Think of yourself when coding. He will thank you greatly later when he has to work on it again in the future.You know, even your current self will appreciate it too. It's much more fun to make a good code than to make a hasty/sloppy/buggy one.

Tuesday, September 10, 2013

Focus on Personal Workflow and System, Not Gadget and Apps

Gadgets and Apps are abound today and the discussions that runs mostly are about what Gadgets can do this and that. I think many times the focus are misplaced. Gadgets are just tools and what is most important is the personal system workflow that a person has. Without that, gadget has no context and will just be a distraction with each additional feature. However, when you have system, each feature on the gadget can use to improve certain aspect of that system.

When you have a workflow involving calendar and reminder, a good calendar app can make that part of your system more optimal and in turn will make the overall system runs better. However, when you don't have need for calendar in your personal workflow (which is not a sin, BTW :) ) it will be just a toy to try a little bit and never used again or worse you keep keep wasting time using it without adding any benefit to your system.

So, I guess what matters is what system that you have and what combinations of gadgets and apps that can make it runs better. Researching new gadgets and apps can be useful if you use it to see how you can improve your existing system with it.