I have been migrating a functionality from VB6 to .Net code. Tasks like this can be tricky. Normally, the original developer is no longer around left behind him/her a piece of code with no documentation about the business rules - they are hidden gems to be discovered.
I started reading the code. Anyway, the code is the best documentation a developer can have. The frustrating bit is that the main method printed out to be 5 double sided A4 pages. FIVE double sided pages of code for a single method! Every time I see code like this, I want to cry it out - could you mighty developer have some sympathy towards those who end up maintaining your great piece of work? Could you at least cut it into shorter methods with meaningful names (function names and variable names)?
Quotes like the following should be shared among developers. Something I'd love to remind myself from time to time so that people who picks up my work later won't want to kill me. I admit that sometimes I couldn't understand my own code in couple of months time, but I promise that I won't write a single method lasts for pages and pages ;).
Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.
-- [source]
... nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the "but their own" and they're probably right...
--Jerry Coffin (on indentation) [source]
If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
--Linux 1.3.53 CodingStyle documentation [ source]
It's OK to figure out murder mysteries, but you shouldn't need to figure out code. You should be able to read it.
--Steve McConnell [ source]
If the code and the comments disagree, then both are probably wrong.
--attributed to Norm Schryer [source]
If you have a procedure with ten parameters, you probably missed some.
--[source]
Why do we never have time to do it right, but always have time to do it over?
--Anonymous [ source]
Computers are high-speed idiots, programmed by low-speed idiots
-- [source]
Thursday, 31 March 2011
Wednesday, 16 March 2011
How to move a borderless window
I had to display a warning message sitting on top of all other windows. I created a borderless form and pinvoked a windows API function in my C# code to achieve this.
However, I found that I couldn't move the borderless window using the mouse. The border actually responds to all resize and position messages. To move a borderless window, the following is needed.
Two API methods:
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
Handle the mouse down event in the borderless window:
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
private void borderlessWindow_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
}
}
However, I found that I couldn't move the borderless window using the mouse. The border actually responds to all resize and position messages. To move a borderless window, the following is needed.
Two API methods:
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
Handle the mouse down event in the borderless window:
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
private void borderlessWindow_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
}
}
Saturday, 12 March 2011
SQL Error Severity and Level Cheat Sheet
Sometimes I have to raise errors manually inside a stored procedure. It will be handy to have an error severity cheat sheet.
Code | Description |
1 - 9 | Informational messages/warnings. |
10 | N/A. @@ERROR won't be set. |
11 - 16 | Regular programming errors. Level 16 is not more serious than 11 |
11 | Specified Database Object Not Found |
12 | Unused |
13 | User Transaction Syntax Error. E.g. deadlock. |
14 | Insufficient Permission |
15 | Syntax Error in SQL Statements |
16 | Miscellaneous User Error |
17 | Run out of a configurable resource, such as locks |
18 | Nonfatal internal software problems |
19 | Nonconfigurable resource limit has been exceeded |
20 | An error with a statement issued by the current process |
21 | SQL Server has encountered a problem that affects all the processes in a database |
22 | A table or index has been damaged |
23 | The integrity of the entire database is affected and the database will be marked suspect |
24 | Hardware problem |
25 | System error |
Subscribe to:
Posts (Atom)