Wednesday, July 28, 2010

Managing Assembly Version

As some of you might have known that we could manage the information about the assembly that we create through AssemblyInfo.cs (in C#) or AssemblyInfo.vb (in VB.NET). When you have a lot of assemblies within of your application, it is not an easy task to manage them, and sometimes it become annoying. Let say that you want to upgrade you version number each time you want to build them, you will face the reality that you need to change all the AssemblyInfo files within your solution. And that is a very boring thing to do.

I found these two articles which suggest us to use single file to maintain information which is generic for all assemblies within our application (such as AssemblyCompany, AssemblyVersion, etc). Very nice articles. Thanks to the writers, it helps me a lot managing my assemblies' version. You can find the articles here, and here.

Another good article is this one, which explain how to automate the process of giving version number each time we build our application. I haven't try it yet. But, it seems that it will be helpful to manage version of your application, specially if your application is quite large and following complex process (maybe involving step such as creating nightly build), which might require you to sign your assembly each time you build them.

Friday, July 23, 2010

The Killer Robot Case


Have you ever read the killer robot case? Well, just in case you don't, this is a good (and long) story about ethical scenario which occurs in a development of robot. In short, the programmer who wrote the codes for this robot, was mistakenly wrote a wrong formula which cause the robot to perform inappropriately and killed its operator. After a deep investigations held inside the company, it was found that the company also did some unethical actions which responsible direct/indirectly to the death of the operator.

Hmm, I guess you need to read it. It's a good case, believe me! You can read the story here. If you have other links for the same case, I would really thank you for sharing.

Now, the case has been assigned as a homework in one of training class that I attended. The training is about Software Quality, and one of the sub topic is about ethics related to Software Engineering. My writing below, was my answer for that homework. I shared it here, because I think it will be helpful for others who work on the same topic, not by giving them direct answer, but to better analyze the case so they can answer better than my answer. If you have worked on the same topic in the past and would like to share anything about this, I would really thank you for sharing it.

So, we were asked to identify the top 5 violation of code of ethics in this case, explain why and what should have been done instead.

And here is my answer.

Violation 1
The quality assurance division was made false claim that they said the software was successfully been tested and error free. They made a fake testing document which said that the. This violation is shown by Cindy Yardley who made fake test document as per request from Ray Johnson. Ray Johnson was also made fake claims that he used to convince Cindy Yardley, that the software is safe that no one will know the test document was a fake. This is violation for the following codes:
  1. Code number 1.06 which says “Be fair and avoid deception in all statements, particularly public ones, concerning software or related documents, methods and tools”.
  2. Code number 6.07 which says “Be accurate in stating the characteristics of software on which they work, avoiding not only false claims but also claims that might reasonably be supposed to be speculative, vacuous, deceptive, misleading, or doubtful”.
  3. Code number 6.08 which says “Take responsibility for detecting, correcting, and reporting errors in software and associated documents on which they work”.
Both Cindy Yardley and Ray Johnson should never make deceptive claim regarding the application, since it may lead to serious problem (cause death to the operator).

Violation 2
People in Techtronics never inform the operator that the software has exceptional conditions which were clearly stated in the requirements document, as stated by Ruth Whitherspoon that there are exceptional conditions written in the document section 5.2.4.26. This is violation for code number 1.04 which says “Disclose to appropriate persons or authorities any actual or potential danger to the user, the public, or the environment, that they reasonably believe to be associated with software or related documents”. According to this code, the people from Techtronics should warn the operator prior to operate the robot, and even give them appropriate training about how to act when such exceptional conditions occur.

Violation 3
Some of people involved in the project had no appropriate experience and educational backgrounds. This is a violation for the following codes:
  1. Code number 2.01 which says “Provide service in their areas of competence, being honest and forthright about any limitations of their experience and education”.
  2. Code number 3.04 which says “Ensure that they are qualified for any project on which they work or propose to work by an appropriate combination of education and training, and experience”.
  3. Code number 5.04 which says “Assign work only after taking into account appropriate contributions of education and experience tempered with a desire to further that education and experience”.
The violations were shown by the following acts:
  1. Randy Samuels has no educational background in mathematics, statistics and physics. Knowledge on those areas is needed to be able to judge that the business rules, formulas, and logics are correctly implemented within the application. It is true that Randy has experience in programming, but it doesn’t make Randy the appropriate person to judge that the code that he wrote has correct logic or not.
  2. Ray Johnson who leads the Robotic Division had no sufficient experience in software development, since his background is from manufacturing. Therefore his decision was inappropriate for example when he insisted to add developers to speed up the project which is based on his experience in manufacturing field
  3. The decision that has been made by the management to place Sam Reynolds as the project manager was also violation of the codes above, since Sam has no experience in Robotic software. Instead, Sam was previously experienced in data processing which characteristics of the projects have differences from robotic software.
To avoid the violations, the executives or managements should place people who have expertise in area of robotic programming.

Violation 4
Ray Johnson was unethically order Cindy Yardley to create fake test document by using his power as Head of Robotics Division. This action was a violation of the code of ethic since Johnson was asking his engineer to do such action that was inconsistent with the code of ethic. This is a violation for the following codes:
  1. Code number 5.11 which says “Not ask a software engineer to do anything inconsistent with this Code”.
  2. Code number 8.08 which says “Not influence others to undertake any action that involves a breach of this Code”.
According to the codes above, Johnson should never use his power to influences his staffs to do any unethical actions. He should let his engineers to work professionally based on their expertise and knowledge.

Violation 5
The project was not developed based on a realistic schedule and estimations. Both Ray Johnson and Sam Reynolds realize that it was impossible to deliver the product perfectly within the time constraint provided by the executives (1 January). However, both Johnson and Reynolds were choosing to force their engineers to do the impossible project, instead of trying to convince the executives that their time constraint given to them was inappropriate. This is violation for the following codes:
  1. Code number 3.01 which says “Strive for high quality, acceptable cost and a reasonable schedule, ensuring significant tradeoffs are clear to and accepted by the employer and the client, and are available for consideration by the user and the public”.
  2. Code number 5.05 which says “Ensure realistic quantitative estimates of cost, scheduling, personnel, quality and outcomes on any project on which they work or propose to work, and provide an uncertainty assessment of these estimates”.

Thursday, December 3, 2009

My Notes About .NET Framework


These are my notes about .NET. I wrote it here, just to review my knowledge about this framework because it's growing very fast. Sometimes, I confused whether WPF was published before the WCF or otherwise. Whether LINQ is built in the framework since it first version, etc. Nothing technical, though. I believe there are millions articles out there that can give a better explanation on specific topic than this article.

So, my notes are:
  • The .NET framework 1.0 was released by Microsoft on 10 July 2002.
  • Early .NET framework articles can be found http://msdn.microsoft.com/en-us/magazine/bb985004.aspx and http://msdn.microsoft.com/en-us/magazine/cc748671.aspx
  • The conceptual overview of the framework can be found at http://msdn.microsoft.com/en-us/library/zw4w595w.aspx.
  • .NET framework release timeline can be found at http://en.wikipedia.org/wiki/.NET_Framework#Versions, more completed list can be found at http://en.wikipedia.org/wiki/.NET_Framework_version_list
  • In .NET 3.0, Microsoft introduces four major new components WPF, WCF, WF, and Windows CardSpace.
  • Windows Presentation Foundation (WPF), formerly code-named Avalon; a new user interface subsystem and API based on XML and vector graphics, which uses 3D computer graphics hardware and Direct3D technologies. See WPF SDK for developer articles and documentation on WPF.
  • Windows Communication Foundation (WCF), formerly code-named Indigo; a service-oriented messaging system which allows programs to interoperate locally or remotely similar to web services.
  • Windows Workflow Foundation (WF) allows for building of task automation and integrated transactions using workflows.
  • Windows CardSpace, formerly code-named InfoCard; a software component which securely stores a person's digital identities and provides a unified interface for choosing the identity for a particular transaction, such as logging in to a website.
  • In .NET 3.5, Microsoft introduces LINQ

Saturday, December 20, 2008

Reading Serial Port with C#

Couple days ago my junior in college called. He was asking about how to read data from Serial Port. He is currently working with his final project to pursue his Bachelor Degree in Electrical Engineering.

In general, his project is creating an RFID reader which can be used to read the RFID tag. The data then will be sent to a PC through RS232 Serial Port. This unique ID then will used as primary key to save data to database.

Hmm... this is a kind of a new thing for me, because I'm never done it before. To give you a better overview of the project, you may want to read these sources before continue:
Ok, let's get back to our main focus of how to read data from Serial Port.

According to MSDN, starting from .NET 2.0, there is a class which can be used to read and write data from Serial Port easily. You can read the documentation here. By using this class, working with serial port is no more a nightmare. The class make us able to work with serial port, as if we were working with file. In sort, you just need to specify the port object, instantiate it, open it, and start working with it (read or write data).

Specifically to create a port reader, SerialPort has an event which will be trigerred when there is a data being sent to the port. The event handler is called DataReceived. What you need to do is creating a method to be executed when the event raised, and attach the method with a SerialDataReceivedEventHandler delegate to the event handler. Let's look at the class that I've created, I call it PortReader class.

This class has three properties which are Name - name of the port which will be read, Error - to hold error occured during the operation (if any), and Message - to hold the message from the port.



private string _name = string.Empty;
private string _error = string.Empty;
private string _msg = string.Empty;

...

public string Name
{
get { return _name; }
set { _port.PortName = value; }
}

public string Message
{
get { return _msg; }
set { _msg = value; }
}

public string Error
{
get { return _error; }
}


You need to specify several attributes of the port such as BaudRate, Parity, DataBits, and StopBits (and other properties as necessary). In this class I specify it in the constructor.



public PortReader()
{
_port = new SerialPort();

_port.BaudRate = 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.DataReceived += new SerialDataReceivedEventHandler(PortReader_SerialDataReceived);

}


You may notice that I also specify a new instance of SerialDataReceivedEventHandler which then attached to DataReceived event of the object. I also create a new event for my class which wrap the DataReceived event - EndReadEvent. The codes is as the following:



public delegate void PortReaderEventHandler();
public event PortReaderEventHandler EndReadEvent;

...

private void PortReader_SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!_port.IsOpen)
{
_error = "Can't read. Port is closed.";
return;
}

_msg = _port.ReadExisting();

//raise end read event
if (EndReadEvent != null) EndReadEvent();

}


PortReader_SerialDataReceived is a method which is used to handle the DataReceived event. It's simply read the port data and hold it in _msg variable. The method then raise its own EndReadEvent. This will allow user to do necessary action after the read operation.

Other methods are the DoOpen and DoClose method, which are as the following:




public bool DoOpen()
{
if (_port.PortName == "")
{
_error = "Please supply port name!";
return false;
}

if (_port.IsOpen)
{
_port.Close();
}

_port.Open();
return true;
}

public bool DoClose()
{
if (!_port.IsOpen)
{
_error = "Can't close port that is not in open state.";
return false;
}

_port.Close();
return true;
}


That's it. The complete codes of this class is as the following:



public class PortReader
{
private SerialPort _port;
private string _name = string.Empty;
private string _error = string.Empty;
private string _msg = string.Empty;

public delegate void PortReaderEventHandler();
public event PortReaderEventHandler EndReadEvent;

public string Name
{
get { return _name; }
set { _port.PortName = value; }
}

public string Message
{
get { return _msg; }
set { _msg = value; }
}

public string Error
{
get { return _error; }
}

public PortReader()
{
_port = new SerialPort();

_port.BaudRate = 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.DataReceived += new SerialDataReceivedEventHandler(PortReader_SerialDataReceived);

}

private void PortReader_SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!_port.IsOpen)
{
_error = "Can't read. Port is closed.";
return;
}

_msg = _port.ReadExisting();

//raise end read event
if (EndReadEvent != null) EndReadEvent();

}

public bool DoOpen()
{
if (_port.PortName == "")
{
_error = "Please supply port name!";
return false;
}

if (_port.IsOpen)
{
_port.Close();
}

_port.Open();
return true;
}

public bool DoClose()
{
if (!_port.IsOpen)
{
_error = "Can't close port that is not in open state.";
return false;
}

_port.Close();
return true;
}

}


Now to use the class, you only need to instantiate the object, specify the port name, open the port, and specifying the action when any read event occurred. Just place your action in OnPortReader_EndRead (you may change it with your own method). A simple example may look like the following:



PortReader myPort = new PortReader();
myPort.Name = "COM1";
myPort.EndReadEvent += new PortReader.PortReaderEventHandler(OnPortReader_EndRead);
myPort.DoOpen();


To test the application you might want to use a Virtual Serial Port Emulator. I use Virtual Serial Port Emulator form Softpedia. It's a nice application - very easy to use, very helpful, and it's free. The application will allow you to create a virtual ports as many as you want up to 255 ports.

Ok, that's all. Happy playing with the port. Any input, please welcome. :D

Friday, December 5, 2008

Even Code Comments, Are Very Important!

The story was started by this email:
All,

Please, allow me to share a little bit.

I have just read the Programming Standards.doc, and I found out something interesting in the document about code comments (all of you might have known this). I have highlighted some statements that might important to be concerned when writing our codes.

You can find the complete standard in Programming Standards.doc.

... Some paragraphs has been deleted...

By reading this, I become more realized that even comments in code have a standard for our client. Overall, I think to follow this standard there is no other way except trying to follow them when writing our codes in each task (And I am still trying to do so).

Any input, please welcome.

Regards,

-imsuryawan-
Above, was my email a couple days ago to all team members about code comments. Yes, I know this is an old topics for the one who have been involved in Programming for long years. But, still... for a newcomers, this is a very good stuff to know.

I remember when I was at the first time involved in a professional team who worked as an outsourced Software Engineers for a well known company in Australia, that code comments is very important. My senior who was being my code reviewer, writes a huge bunch notes about my comments. That time I was thinking, "Man, this is just a comment. The compiler will never care about them! So, what's wrong with this?". Hahaha... Please understand, I was still a Junior SE that time. :D

Now, I am a senior programmer in a different outsourced team for the same client. After reading the Programming Standard I become more realized that event code comments are a very important thing to be concerned, so they write them in the document.

I am sorry that I can not tell you what the rules in the document are. Overall, most of the item written in the document is a common rule when writing code comment. There are so many articles on the net about how to write a good code comment. This one is a good reference for you. Other source maybe by reading Code Complete by Steve McConnel - which discussing how to create a self documenting codes in chapter 32.

So, let's start to write a good comments in our codes. :D

Tuesday, September 23, 2008

Hello Again!

Huah... after couple months of hiatus, now I'm back. Yeap... It has been a busy months lately. Some personal stuffs has made me busy. And ofcourse, the project is getting crazy.

Yeah, whatever!

Btw, I just want to update my blog. No particular topic in this post. Because I was too busy to write an article. hahaha... I know, you may call it lazy. Hehehe... It's tight related each other, though!

Btw, I was starting to use Linux again. I used to use Fedora last year. But, Fedora is not for a newbie like me - because to many things to do to make it works as smooth as W*****s, so I uninstalled it from my PC! After searching for several opinions from internet, most of the review told me to use Ubuntu! So, this Ubuntu thing is in my PC right now!

Oh yeah! About my project, I'm learning new thing right now. It's called Service Oriented Architecture. This thing has made me so busy for a couple of week. I hope I can write something about this architecture lately. Just ask uncle Google if you want to know about this stuff. I guess this link is a good start! Just check it out.

Allright then! Enough for now... Hello again all!

Saturday, June 14, 2008

For Delphi's Scanline Property, in .NET We Use LockBits

When I was in college, I used Delphi to do my assignment in Digital Image Processing subject. No particular reason though. It just because Delphi is the most popular tools to be used when working with image manipulation. My senior told me that those tools are quite good to use, because we can do an image manipulation in bit level and accessing the pointer. Since Delphi using Pascal as the core language, and Pascal do support pointer, I think the statement is right. CMIIW.

The most important one is that Delphi has a very powerful method to work with image. The method called Scanline. For a brief explanation of this method, just read it here.

Once, a friend asked me about how to implement Delphi's ScanLine when working in .NET. Hmm... that's new to me. As far as I now, there is no such method which have same ability with
Delphi's ScanLine. Common methods used to work with Image (specifically Bitmap) is Bitmap.GetPixel and Bitmap.SetPixel method. But these methods are extremely slow to be used when working with a huge image.

After browsing through uncle Google for a while, I found out that there is a method which can be used when we want to work directly accessing image data in memory. The method is Lockbits. Please refer to this article for the explanation. The writer has just explain the method in very detail and good way, so I believe you'll understand quickly.

Here is an example to do a Grayscale operation with
Bitmap.GetPixel and Bitmap.SetPixel method.


///
/// Grayscale method
///

public void Grayscale()
{
int width = _bmp.Size.Width;
int height = _bmp.Size.Height;

for (int heightIdx = 0; heightIdx < height; heightIdx++)
{
for (int widthIdx = 0; widthIdx < width; widthIdx++)
{
Color objImgColor;
objImgColor = _bmp.GetPixel(widthIdx, heightIdx);

int grayScaleBase = (int)((objImgColor.R + objImgColor.G + objImgColor.B) / 3);
Color grayScaleColor;
grayScaleColor = Color.FromArgb(grayScaleBase, grayScaleBase, grayScaleBase);

_bmp.SetPixel(widthIdx, heightIdx, grayScaleColor);
}
}
}



When woring with a large image, the method above is extremely slow. Just try it If you don't believe me. And here is how we implement Lockbits method to do a Grayscale operation on image.


///
/// Grayscale method by using pointer access
///

public void SpeedGrayscale()
{

// Specify width, height, anf pixel format
int imgWidth = _bmp.Width;
int imgHeight = _bmp.Height;
PixelFormat imgPixFormat = _bmp.PixelFormat;

// Create BitmapData and Rectangle object, which will be used
// in Lockbits method
BitmapData bmpData = null;
Rectangle rct = new Rectangle(0, 0, imgWidth, imgHeight);

try
{
// We use unsafe keyword, because this is a pointer operation
unsafe
{

//Lockbits = lock bitmap data to memory
bmpData = _bmp.LockBits(rct, ImageLockMode.ReadWrite, imgPixFormat);

byte* row; // Pointer for a bitmap row
byte* rowElement; // Pointer for a pixel
int intGrayscaleValue; // grayscale value for one pixel

// Loop based on image height (y)
for (int y = 0; y < bmpData.Height; y++)
{
// Find starting memory pointer address for each row
row = (byte*)bmpData.Scan0 + (y * bmpData.Stride);

// Loop through the row (x)
for (int x = 0; x < bmpData.Width; x++)
{
rowElement = row + (x * 3); // find the pixel pointer address

intGrayscaleValue = (rowElement[0] + rowElement[1] + rowElement[2]) / 3;

// assign grayscale value for each pixel element
rowElement[0] = (byte)intGrayscaleValue;
rowElement[1] = (byte)intGrayscaleValue;
rowElement[2] = (byte)intGrayscaleValue;
}
}

// Unlock the bitmap from memory
_bmp.UnlockBits(bmpData);
}

}
catch (Exception ex)
{
throw ex;
}
}

Friday, May 16, 2008

Other Old Articles Written By Me

It's been almost a month, and I haven't updated this blog yet. Huh! There was a busy month. A lot of work in the office with a huge bunch of tasks with complex specification.

Btw, I just want to share links from my primary blog. Yeah! I have written some articles, and putted in my primary blog. Nothing important, but they might helpful for some of you. The articles are:
I wrote those articles when I was in Induction Training. The topics may not interest you but, for some people who are really need it, I believe it can give a brighter light. Hopefully! hehehe...

So please enjoy.

Sunday, April 27, 2008

Use DateSerial Function To Avoid Regional Settings Problem In VB.NET

This is based on my experience. I used to construct a date type with from string. The sample code is as follow:


Dim dt As Date = CDate("2/3/2008")
MessageBox.Show(dt.ToLongDateString)

Hell yeah! I realized now that this is not a good way. When you execute the code above by using Regional Options set to English US - which Short date format in this setting is "mm/dd/yyyy" - you'll get a message box with message "Sunday, February 03, 2008". Comparing the result with Regional Options set to English (Australia) - which the format is "dd/mm/yyyy" - we'll get a message box with message "Sunday, 2 March 2008". Whew! So which date do the code means?

Oh, I means February 03, 2008! It's just a date, we can fix it just by changing it, it's not a big deal! Hell yeah! You can just simply change it when we are talking about one or two variable, and a simple program like those "Hello World Style" like above. When we're talking about an enterprise application like e-banking which all reports are based on transaction date, believe me that this style of code will bring you to hell.

The simple way to avoid this Regional Options problem, is just get yourself used to construct the datetime variable value by define its day, month, and year value specifically. In .NET you can use DateSerial function.

According to msdn this function returns a Date value representing a specified year, month, and day, with the time information set to midnight (00:00:00). The format of this function is as follow:


Public Function DateSerial( _
ByVal [Year] As Integer, _
ByVal [Month] As Integer, _
ByVal [Day] As Integer _
) As DateTime

So, when you means February 03, 2008, the code above will be as follow:


Dim dt As Date = DateSerial(2008, 2, 3)
MessageBox.Show(dt.ToLongDateString)

The code above will give you February 03, 2008 in any Regional Options you use!

This is a very simple thing to do, but not all realized it's effectiveness (including me, was). But, just by using this kind of simple code behavior will avoid you from a lot of problem caused by Regional Options different from miscalculating reports to unpredictable error in your application.

Tuesday, April 15, 2008

MozillaCacheView to Sneak Into Your Mozilla Cache Folder

If you are a Mozilla Firefox user, have you ever check the cache directory. Aha... you might asking, why should you check this directory. Well sometimes you might want to get some files (for example a picture) from a website you've visited but you forget the address. You can check the cache folder, because usually the files you've browsed will stay in this folder for a while (as long as you don't delete them).

But, when you look at the folder you might surprised (or maybe get confused), because this folder only contain files, without extension on it. And the file name didn't look like a filename. See the screen shot below.

Well, MozillaCacheView might help you to sneak at this folder. By using this tool you can read files located at this cache folder. It''s really simple. Just open the tool, and it'll work for you. Very exciting. You can also, copy file(s) you want into certain location.

This is the screen shot of this tool.

This tool works for Firefox/Mozilla/Netscape Web browsers. And also the great news is this tools is freeware, so you can download it free. I''m sorry I don't remember the location (and also because this is not an ads), but I believe Uncle Google will tell you better than me! :D

Btw, this is the location of cache folder for several browser.


The cache folder of Mozilla Firefox is located under C:\Documents and Settings\[User Name]\Local Settings\Application Data\Mozilla\Firefox\Profiles\[Profile Name]\Cache

The cache folder of SeaMonkey is located under C:\Documents and Settings\[User Name]\Local Settings\Application Data\Mozilla\Profiles\[Profile Name]\Cache

For other variants of Mozilla, you may find the cache folder under C:\Documents and Settings\[User Name]\Application Data\Mozilla\Profiles\[Profile Name]\Cache