Clarifying Boolean Parameters, part 1

Have you ever written code like this:

public void UpdateLayout(bool doFullLayout) {
    if (doFullLayout) {
        //Expensive code
    //More code

This pattern is commonly used when some operation has a “cheap” mode and an “expensive” mode.  Other code will have calls like UpdateLayout(false) and UpdateLayout(true) scattered throughout.

The problem is that this isn’t very obvious for people who aren’t familiar with the codebase.  If you take a look at a file you’ve never seen before and see calls like UpdateLayout(false) and UpdateLayout(true) scattered, you’ll have no idea what the true / false means.

The simplest solution is to break it out into two methods: UpdateComplexLayout() and UpdateBasicLayout().  However,  if the two different layout modes have intertwined code paths (eg, the code before and after the if above), this either won’t be possible or will lead to ugly duplication of code.

One alternative is to use enums:

public enum LayoutUpdateType {

public void UpdateLayout(LayoutUpdateType type) {
    if (type == LayoutUpdateType.Full) {
        //Expensive code
    //More code

This way, the callsites are much more descriptive: UpdateLayout(LayoutUpdateType.Full).  This also makes it easy to add more update modes in the future should the need arise.  However, it makes the callsites much more verbose.  When used frequently, this pattern can lead a vast proliferation of enum types that are each only used by one method, polluting the namespace and making more important enums harder to notice.

Next time: Cleverer alternatives


public void UpdateBasicLayout() {
public void UpdateFullLayout() {
private void UpdateLayout(bool doFullLayout) {
    /* Code */

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

It raises very important issues.
massage London

If you are using internet explorer then find the "Internet Options" in the Tools menu of your browser. Click on the "Security" tab. Make sure the "Internet" globe icon is highlighted. Click on the "Custom Level..." button to bring up the safety options for your browser. Search through the menu for the "Active scripting" option. Make sure "Enable" is chosen. Click the "OK" button.Buy Assignment - Close this window and refresh the page that asks Javascript.

It seems like you are interested in an expository essay writing as me.

I am first time hearing about boolean parameters and its parts. I am very glad to know about it and I also want to share this news with the qualified writers of dissertation writing service

Managing the regular maturing cycle of the railings - Powder covered aluminum handrail may begin to show a particular layer on top. This is a piece of the railing's. Aluminum profile manufacturers in china

common maturing cycle and you would have the option to see the layer that takes after chalk dust on a superficial level. portable power station suppliers

Green espresso beans are only an unroasted seed of the plant known as Coffea Arabica. coffee bag manufacturer The simmered rendition of the seed isn't at all advantageous for our wellbeing,

Going to college? Going to College | Tips for a Rugby Player Each of them has extensive experience in their field and graduated from the prestigious Magna Cum Laude universities.

Really I like your blogs it is easy to reading. You are a good blogger pls keep it up. Thank you so much. Best Dissertation Writing Services in the UK.

Post a Comment