Clarifying Boolean Parameters, part 1

Have you ever written code like this:

public void UpdateLayout(bool doFullLayout) {
    //Code
    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 {
    Basic,
    Full
}

public void UpdateLayout(LayoutUpdateType type) {
    //Code
    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

16 comments:

public void UpdateBasicLayout() {
    UpdateLayout(false);
}
public void UpdateFullLayout() {
    UpdateLayout(true);
}
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 - assignmentcorp.com Close this window and refresh the page that asks Javascript.

It seems like you are interested in an expository essay writing https://pro-academic-writers.com/blog/expository-essay 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.

it makes the callsites substantially more verbose. Need Someone To Write My Essay When utilized oftentimes, this example can lead a tremendous multiplication of enum types that are each just utilized by one strategy, contaminating the namespace and making more significant enums harder to take note.

NJ Casinos and Gaming - DrMCD
New Jersey Casinos and Gaming (NJGE) 부천 출장샵 · Las Atlantis Casino Resort 동두천 출장마사지 & 용인 출장샵 Spa · The Venetian 구리 출장샵 Resort Casino & 논산 출장샵 Spa · Borgata Hotel Casino & Spa · The Spa

The sound of the air passing through the fan. Therefore, more expensive models of air conditioners, as a rule, have a larger size of the indoor unit third grade math games compared to more budgetary models of the same capacity. The explanation for this is simple: the same volume of air passing through a larger fan rotating at a lower speed creates less noise.

This is a good piece of article on your site that can be very beneficial for everyone. I was searching for finance thesis topics then I find this site and You are sharing amazing things with us.

Post a Comment