|
| |
April 11th, 2008
Recently I wondered if code should be always nested in a namespace or not.
The question actually shouldn’t be which is “better” rather which is better for a particular case.
using bar;
namespace Foo {
// etc
}
or
namespace Foo {
using foo;
// etc
}
I don’t know how to answer the question “which is better?” because I do not know the intended purpose of the code. I therefore tried to think of ways that these could be different, and answer the question “how do these differ?”
The answer is that if there is only one namespace in the file, it hardly matters. If there are two or more namespaces in the file, then put the directives you want shared amongst them at the top of the file, and the ones you do not want shared inside each the namespace. I further noted that having multiple top-level namespaces in the same file is a little weird, so maybe don’t even go there and make it a moot point.
My first statement was not right. There is a way that these could be different that I forgot about. The first version is equivalent to
using global::bar;
namespace Foo {
// etc
}
The second version is equivalent to
namespace Foo {
using global::bar;
// etc
}
unless Foo contains a namespace called bar. In that case, it is equivalent to
namespace Foo {
using global::Foo.Blah;
// etc
}
So there are at least two ways that things can get broken here. First, moving a not-fully-qualified directive from outside to inside (or vice versa) may cause a semantic change in the program. And second, if there is a not-fully-qualified directive inside the namespace which refers to a top-level namespace then adding a child namespace that collides with that name can introduce a semantic change in the program.
Therefore my advice is:
- have only one top-level namespace per file
- if you choose to put using directives inside your namespaces then fully qualify them with the global alias qualifier
If you take this advice then it really does not matter whether the directives go inside the namespace or outside; either way you will not accidentally break yourself by introducing a name collision.
April 10th, 2008
The Monitor class controls access to objects by granting a single thread a lock for an object. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object no other thread can acquire the lock for the object. Additionally, the Monitor class can be used to ensure that no other thread can access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object.
I ever wondered what the difference between using a lock statement, versus using a Montior.Enter / Monitor.Exit clause is? Actually thereâs a few, but the main one is that a lock statement is inherently thread safe, where-as a Montior is not. So -
try {
Monitor.Enter(_lock);
DoThreadSafeCode();
} finally {
Monitor.Exit(_lock);
}
is essentially the same as -
lock (_lock) {
DoThreadSafeCode();
}
April 6th, 2008
Every time I have occasion to look at the Windows command line documentation, I seem to discover something new. Admittedly this is because I don’t really look for new stuff all that often…
The year’s discovery for 2008 might be Pathping which is a Windows based command-line tool (for Windows cmd) used to provide information about the path data takes to its intended destination, network latency and network loss at intermediate hops between a source and destination.
It’s a TCP/IP based utility that can be used when looking for errors in network. It does this by sending echo requests via ICMP and analyzing the results. ICMP stands for Internet Control Message Protocol. ICMP is an extension to the Internet Protocol (IP - part of the TCP/IP protocol suite). ICMP supports packets containing error, control and informational messages.
Like tracert, pathping will discover the route IP traffic will currently take to from your machine to a particular target on the internet, and how long each hop takes. But while tracert just shows you three samples for the timing to each hop, pathping runs for a few minutes, and sends out a much larger number of packets - 100 per hop by default. It then displays the average performance and packet loss for each hop in the chain. Since timing and packet loss can vary quite widely, three samples often isn’t enough to characterise the quality of your connectivity, so pathping can give a better picture of network health between distant machines.
A sample output could be:
pathping -n server
Tracing route to server [10.1.1.5]
over a maximum of 30 hops:
0 10.1.2.1
1 10.1.1.1
2 10.1.1.5
Computing statistics for 50 seconds...
Source to Here This Node/Link
Hop RTT Lost/Sent = Pct Lost/Sent = Pct Address
0 10.1.2.1
0/ 100 = 0% |
1 35ms 0/ 100 = 0% 0/ 100 = 0% 10.1.1.1
13/ 100 = 13% |
2 28ms 16/ 100 = 16% 3/ 100 = 3% 10.1.1.5
0/ 100 = 0% |
Trace complete.
PathPing is supplied in Windows NT, Windows 2000, Windows 2003, Windows XP and Windows Vista.
Finally it’s nothing spectacular that can’t be archieved otherways but it will ease your work in some cases.
April 3rd, 2008
I have been irritated many times when IntelliSense stoped working immediately in the middle of programming. Sometimes the whole IntelliSense quits and at other times only certain sections of code fails to bring up List Members and Parameter Info.
According to Microsoft if there is an incomplete function or other coding error above the location of the insertion point, IntelliSense may be unable to parse the code elements and therefore will not work. It’s recommented to comment out the applicable code to enable IntelliSense again.
—
I found out that certain lines in my code bring IntelliSense to a stand-still.
Any code in Visual Studio that uses a comma-list to define an array of object constructor parameters will kill IntelliSense for all code following the definition.
This works well:
int iSomeNumbers[] = {4, 14, 1, 44 ,1};
char cWord[] = "test";
’cause it’s just a simple array.
The problem appears when using array definitions that include object constructors to create elements.
IntelliSense gets confused by this and crashes:
Point arPoints[] = {Point(1,4), Point(4,1), Point(4,1), Point(4,1)};
The easiest workaround to keep IntelliSense keep on working proper is to this kinda long winded way:
Point arPoints[4];
arPoints[0] = Point(1,4);
arPoints[1] = Point(4,1);
arPoints[2] = Point(4,1);
arPoints[3] = Point(4,1);
At least I discoverd in the web that this line messes up IntelliSense:
Point arPoints[] = {Point(1,4), Point(4,1), Point(4,1), Point(4,1)};
This one does not:
Point[] arPoints= {Point(1,4), Point(4,1), Point(4,1), Point(4,1)};
Notice that the array brackets are after the type not the identifier.
—
Hopefully someone can help determine other situations that cause IntelliSense to fail.
April 1st, 2008
On the newer and older version of Gmail, but not in the basic HTML version, in the upper right corner, next to Settings, a link appeared labeled, “New! Gmail Custom Time”.
The page claimed a new Gmail feature had been added that would allow people to date stamp their emails in the past, allowing for all sorts of mischief. Users would be limited to ten Custom Times a year to minimize the number of fraudulent emails.
You’d probably ask why exacly ten. Their researchers have found out that allowing each person more than ten pre-dated emails per year would cause people to lose faith in the accuracy of time, thus rendering the feature useless.
Their findings:

N = Total emails sent
P = Probability that user believes the time stamp
Ï = The Golden Ratio
L = Average life expectancy

With GMail Custom Time, you can date your email in the past. Just click âSet custom timeâ from the Compose view - any email you send to the past appears in the proper chronological order in your recipientâs inbox. You can opt for it to show up read or unread by selecting the appropriate option.
The link led to a 404 error before April 1st. - It now leads to their latest hoax, Gmail Custom Time.
Clicking any of the three links at the bottom of the page brought the user to a page indicating that Gmail Custom time was, in fact, their April Fools prank for 2008.
|
| |