Boiler plate code replacement – is there anything bad about this code?

This is good stuff. Make them extension methods though to clean up your code a little more. For example: //Replaces OnMyEventRaised boiler-plate code //Usage: SafeInvoker.RaiseEvent(this, MyEventRaised) public static void Raise(this EventHandler eventToRaise, object sender) { EventHandler eventHandler = eventToRaise; if (eventHandler != null) eventHandler(sender, EventArgs.Empty); } Now on your events you can call: myEvent.Raise(this);

What kind of prefix do you use for member variables?

No doubt, it’s essential for understanding code to give member variables a prefix so that they can easily be distinguished from “normal” variables. I dispute this claim. It’s not the least bit necessary if you have half-decent syntax highlighting. A good IDE can let you write your code in readable English, and can show you … Read more

#ifdef vs #if – which is better/safer as a method for enabling/disabling compilation of particular sections of code?

My initial reaction was #ifdef, of course, but I think #if actually has some significant advantages for this – here’s why: First, you can use DEBUG_ENABLED in preprocessor and compiled tests. Example – Often, I want longer timeouts when debug is enabled, so using #if, I can write this DoSomethingSlowWithTimeout(DEBUG_ENABLED? 5000 : 1000); … instead … Read more

How to clean up if else series

Use a Dictionary. Something like this: Dictionary<int, ServiceClass> dictionary = new Dictionary<int, ServiceClass>() { {1, new ServiceClass()}, {2, new ServiceClass()}, {3, new BTWithdrawal()},//assume BTWithdrawal inherits from ServiceClass }; An example of how using it: ServiceClass value=new ServiceClass(); value.FromServiceId=1; value.ToServiceId = 2; dictionary.TryGetValue(value.FromServiceId, out value); //or dictionary.TryGetValue(value.ToServiceId, out value); if (value != null) MessageBox.Show(value.Id.ToString());

How can we force naming of parameters when calling a function?

In Python 3 – Yes, you can specify * in the argument list. From docs: Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments. >>> def foo(pos, *, forcenamed): … print(pos, forcenamed) … >>> foo(pos=10, forcenamed=20) 10 20 >>> foo(10, forcenamed=20) 10 20 >>> foo(10, 20) Traceback (most … Read more