Wednesday, August 31, 2005

Boolean parameters

When I first started developing software, one thing that annoyed me immediately was when people used boolean parameters where their meaning wasn’t immediately obvious.

This is ok:
void setUserIsDeadFlag(bool)

This is not:
User findUserByName(string, bool)

In the documentation, the parameters will have names and possibly further info pertaining to their use, but when I come to write code, I’m forced to add a comment explaining what the parameter is, so that I can read my own code later:
User bob = findUserByName("bob", false /* Don’t include dead users */);

I can’t even trim my comment down, because the parameter name is something like include_dead_users. If I simply used the name in my comment, I’d make it look like I was passing false because I wanted to include dead users, which would be confusing:
User bob = findUserByName("bob", false /* include_dead_users */);

The best solution to this problem (in all the languages I know) is to use enumerations, or at least constants. Here’s C++:
enum DeadUserInclusionPolicy { Include, Exclude };

Before we start rushing off though, what’s wrong with the above? Let’s have a look at a method signature and a call:
void findUserByName(string name, DeadUserInclusionPolicy deadUserInclusionPolicy)
User bob = findUserByName("bob", Exclude);
The enumeration is still no help, because ‘Exclude’ means nothing to the reader of our code. Let’s try again:
namespace DeadUsers { enum InclusionPolicy { Include, Exclude } };
void findUserByName(string name, DeadUsers::InclusionPolicy policy)
User bob = findUserByName("bob", DeadUsers::Exclude);

Finally we have readable ‘user’ code. In VB.NET and C# you don’t need to bother with a namespace and creative naming, because when you pass a value from an enumeration, you have to prefix it with the name of the enumeration, e.g. DeadUsersInclusionPolicy.Exclude

A large proportion of code that I come across has the boolean parameter problem. When I’m using C++ or C#, I add a comment to remind myself what I’m passing - and try to remember to update the comment if I change the value I pass. When I’m using VB.NET, I get to use a feature of the language to make things explicit: named parameters.

IO.Directory.Delete("c:\", Recursive:=True)

That’s better! Not only does this help where I have to call a method with boolean parameters, it also allows me to call methods with multiple parameters with less worry that I’ll get them mixed up:
Dim Bob As New User(Name:="Bob", Surname:="Holness", Whereabouts:="Unknown")

Thursday, August 18, 2005

Adding a doctype to System.Xml.XmlDocument


Doc.AppendChild _
( _
Doc.CreateDocumentType _
( _
name:="html", _
publicID:="-//W3C//DTD XHTML 1.0 Strict//EN", _
systemID:="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", _
internalSubset:=Nothing _
) _
)

Wednesday, August 17, 2005

Allowing anonymous access to an IIS virtual directory through code (programmatically)

var entry = new System.DirectoryServices.DirectoryEntry(@"IIS:\\127.0.0.1\W3SVC\1\Root\" + VirtualDirectoryName);
entry.Properties["AuthAnonymous"][0] = true;
entry.CommitChanges();

Tuesday, February 1, 2005

An asynchronous testing framework for .NET

AsyncTestFramework is a simple test framework for .NET which allows testing of asynchronous code, i.e. code which uses callbacks. In .NET, there are two main types of callback systems: those based on AsyncCallback and those based on Events. This framework supports both.

Thanks to Jeremy Hopkin for suggestions, insight and asking me difficult questions.

Thursday, August 1, 2002

Backward clock


Description

Backward clock is a backward clock. The hands rotate backwards. I used to have a real backward clock, so I knocked up this one so I can use an analogue clock on my screen without getting confused. There are .NET and Qt versions. The clock drawing bit (of the Qt version) is nicked from kicker (a program which used to be part of KDE).

Features

Clock (backwards).
Screenshots

Qt version. It’s 20 past 3.



.NET version. It’s 26 to two.


Friday, June 21, 2002

A7M266-D

Introduction
This page is a little collection of notes I’ve taken while getting my A7M266-D motherboard to work under Linux.

Working setup

The case: Chieftec Dragon full tower.

The processors: Athlon MP 1800+.

The first thing I had to do, in order to make the second processor usable, and indeed the machine bootable without using the nosmp LILO flag, was to use the noapic LILO flag. I don’t know why the kernel didn’t know that it should do this itself.

The PCI cards installed in the box are an Intel NIC and a SoundBlaster PCI 128 (Ensoniq 1371). To make the soundcard work, I had to tell the BIOS that I do not have a Plug ‘n’ Play OS installed. Must be something to do with interrupts. I’m no hardware guru.

There is only one hard disk in the system - a Western Digital 120G. WD are not known for the reliability of their disks, but all my important stuff is mirrored elsewhere in the world (CVS) so I don’t care too much. I just make sure I have a spare disk lying around just in case. I like the quiet running (much quieter than the current Maxtor drives) and the excellent speed thanks to the 8M cache.

I use the following in my boot scripts to set up the disk:

hdparm -c 1 -d 1 -k 1 -u 1 /dev/hda

I can get some sensor information, using a CVS build of lm_sensors. The loading stuff looks like this:

modprobe i2c-amd756
modprobe w83781d
sensors -s

Cooling

I bought 2 Arctic Cooling ‘Super Silent’ to cool my CPUs. They’re not silent. They don’t cool very well. At full load, ambient temperature 22 celsius, the chips ran at 65 celsius.

To replace them, I bought 2x Noise Control Silverado . These are about 30% quieter than the above, and they brought the top chip temperature down to 60 celsius, using the black 10V cables. The difference in noise between the 6V and 10V cables isn’t really noticeable.

The Chieftec 340W PSU seemed to be overheating (it came with the case - don’t blame me) - it was too hot to touch, and 2/3 times the machine wouldn’t switch on via ‘soft power’ and required the PSU switch to be turned off and on again.

To get the chip temperatures down, I bought 4x Enermax 80mm case fans. They’re very quiet at the lowest fan speed. My top CPU temperature is now 52 celsius - much more sensible. The PSU now seems to be happier too. It’s cool running - just a hint of warmth from its exhaust fan - but it still has trouble starting up. Perhaps time for a beefier one.
Boot problem

When I first bought this board, it kept failing to boot when the soft-power switch was pressed. I found I could work around the problem by turning the power supply off for a few seconds before booting.

One day, however, the machine just started booting normally. I have no idea what fixed it - but I don't think it was a BIOS update or a setting in the BIOS. It hasn't had any problems since.
/proc/cpuinfo

Note that the processors are labelled MP - the kernel is just having trouble recognising them.
See the next section for a better scan.

processor     : 0
vendor_id     : AuthenticAMD
cpu family    : 6
model         : 6
model name    : AMD Athlon(TM) XP 1800+
stepping      : 2
cpu MHz       : 1533.441
cache size    : 256 KB
fdiv_bug      : no
hlt_bug       : no
f00f_bug      : no
coma_bug      : no
fpu           : yes
fpu_exception : yes
cpuid level   : 1
wp            : yes
flags         : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mmxext 3dnowext 3dnow
bogomips      : 3060.53

processor     : 1
vendor_id     : AuthenticAMD
cpu family    : 6
model         : 6
model name    : AMD Athlon(TM) XP 1800+
stepping      : 2
cpu MHz       : 1533.441
cache size    : 256 KB
fdiv_bug      : no
hlt_bug       : no
f00f_bug      : no
coma_bug      : no
fpu           : yes
fpu_exception : yes
cpuid level   : 1
wp            : yes
flags         : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mmxext 3dnowext 3dnow
bogomips      : 3060.53

x86info

x86info v1.7.  Dave Jones 2001
Feedback to .

Found 2 CPUs
CPU #1
Family: 6 Model: 6 Stepping: 2 [Athlon MP]
Processor name string: AMD Athlon(TM) XP 1800+

PowerNOW! Technology information
Available features:
  Temperature sensing diode present.

CPU #2
Family: 6 Model: 6 Stepping: 2 [Athlon MP]
Processor name string: AMD Athlon(TM) XP 1800+

PowerNOW! Technology information
Available features:
  Temperature sensing diode present.

lspci

00:00.0 Host bridge: Advanced Micro Devices [AMD]: Unknown device 700c (rev 11)
00:01.0 PCI bridge: Advanced Micro Devices [AMD]: Unknown device 700d
00:07.0 ISA bridge: Advanced Micro Devices [AMD]: Unknown device 7440 (rev 04)
00:07.1 IDE interface: Advanced Micro Devices [AMD]: Unknown device 7441 (rev 04)
00:07.3 Bridge: Advanced Micro Devices [AMD]: Unknown device 7443 (rev 03)
00:10.0 PCI bridge: Advanced Micro Devices [AMD]: Unknown device 7448 (rev 04)
01:05.0 VGA compatible controller: ATI Technologies Inc: Unknown device 514c
02:05.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8029(AS)
02:06.0 Ethernet controller: Intel Corporation 82557 [Ethernet Pro 100] (rev 0c)
02:08.0 Multimedia audio controller: Ensoniq ES1371 [AudioPCI-97] (rev 08)

/proc/pci

PCI devices found:
  Bus  0, device   0, function  0:
    Host bridge: PCI device 1022:700c (Advanced Micro Devices [AMD]) (rev 17).
      Master Capable.  Latency=32.  
      Prefetchable 32 bit memory at 0xfc000000 [0xfdffffff].
      Prefetchable 32 bit memory at 0xfb800000 [0xfb800fff].
      I/O at 0xe800 [0xe803].
  Bus  0, device   1, function  0:
    PCI bridge: PCI device 1022:700d (Advanced Micro Devices [AMD]) (rev 0).
      Master Capable.  No bursts.  Min Gnt=8.
  Bus  0, device   7, function  0:
    ISA bridge: Advanced Micro Devices [AMD] AMD-768 [??] ISA (rev 4).
  Bus  0, device   7, function  1:
    IDE interface: Advanced Micro Devices [AMD] AMD-768 [??] IDE (rev 4).
      Master Capable.  Latency=32.  
      I/O at 0xb800 [0xb80f].
  Bus  0, device   7, function  3:
    Bridge: Advanced Micro Devices [AMD] AMD-768 [??] ACPI (rev 3).
      Master Capable.  Latency=32.  
  Bus  0, device  16, function  0:
    PCI bridge: Advanced Micro Devices [AMD] AMD-768 [??] PCI (rev 4).
      Master Capable.  No bursts.  Min Gnt=4.
  Bus  1, device   5, function  0:
    VGA compatible controller: PCI device 1002:514c (ATI Technologies Inc) (rev 0).
      IRQ 11.
      Master Capable.  Latency=64.  Min Gnt=8.
      Prefetchable 32 bit memory at 0xf0000000 [0xf7ffffff].
      I/O at 0xd800 [0xd8ff].
      Non-prefetchable 32 bit memory at 0xef000000 [0xef00ffff].
  Bus  2, device   5, function  0:
    Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8029(AS) (rev 0).
      IRQ 4.
      I/O at 0xa800 [0xa81f].
  Bus  2, device   6, function  0:
    Ethernet controller: Intel Corp. 82557 [Ethernet Pro 100] (rev 12).
      IRQ 10.
      Master Capable.  Latency=32.  Min Gnt=8.Max Lat=56.
      Non-prefetchable 32 bit memory at 0xee000000 [0xee000fff].
      I/O at 0xa400 [0xa43f].
      Non-prefetchable 32 bit memory at 0xed800000 [0xed81ffff].
  Bus  2, device   8, function  0:
    Multimedia audio controller: Ensoniq ES1371 [AudioPCI-97] (rev 8).
      IRQ 3.
      Master Capable.  Latency=32.  Min Gnt=12.Max Lat=128.
      I/O at 0xa000 [0xa03f].