Saturday, July 30, 2016

5 ways to do Decision trees in C#

At Agile2016 we spent the mornings doing Mob Programming working on the Tennis Refactoring Kata from Emily Bache.

Thursday we ran accross this issue:

#1. If Structure

This is fairly easy to code as is, the trouble is it looks horrible:

So we started to play around with alternative structures...


#2. Pass in result

One solution is to call all the methods but abort early if you've already found a solution.
This looks a lot nicer, the problem is it leaks into the underlying methods
So we keep looking..

#3. Linq

If you like linq you can solve this with a bit of functional code


This reads nicely, but is a bit confusing as to why it works and means all your methods must not use any parameters.

but It can also be used with the yield keyword, which is neat and allows parameters but splits logic.
so we kept looking...

#4. Do If Null

We were able to get closer to the 'Pass in result' without leaking with the addition of an extension method.
at this point someone pointed out the ?? operator...

#5. ?? Operator

The ( a ?? b ) operator says if  a is null do b. The resulting code is rather nice
It's worth noting that if your language has 'truthiness' (like javascript) you can also do this with the or (||) operator


Which is Best?

Of course that's up to you. For us most liked the ?? operator best, while a few thought the Linq solution was the nicest.

Which one do you like? 

29 comments:

Jay Bazuzi said...

Option 1a: invert the `if`s and save the results. Not sure if it's better or worse than #1, but it's a little shorter.

1a: invert the if. Not clear if it's better or worse, but some people might

https://gist.github.com/JayBazuzi/cea91f55a8ff2d117ea537fa7914d9ef

Option 4a: as long as you have a `DoIfNull()` method, just inline the use of the local: https://gist.github.com/JayBazuzi/c93ac3f8da87983b0d8a1896160fda97

I like #5 (chained `??`) the best.

Henrik Ebbeskog said...
This comment has been removed by the author.
Henrik Ebbeskog said...
This comment has been removed by the author.
Chris Lucian said...

I had a lot of fun at #Agile2016 and this was a great way to start the morning. Highly recommended kata for people looking into Legacy Refactoring and Mob Programming.

Vermil John said...

One have to refrain from moving the upper frame and raise the lower frame at the pelvis, now not the hip. Bending on the Exoslimhip is a great hip flexor exercising, but does little for the abdomen. another accurate stomach workout is the bicycle. For more ==== >>>>>> http://greentheorygarcinia.com/exoslim-garcinia-cambogia/

Darry Penoy said...

A non-stop workout performed with flawlessly crafted transitions might construct stamina and increase the strength Hydro Muscle Max of the muscle tissues. A pilates home fitness center is the perfect routine for people who do now not have plenty of time on their side. It is easy to do, convenient and now not very time eating. consequently, you want to begin a fitness routine now to be on the way to having the body of your goals and enhance your health.For more ==== >>>>>> http://supplementsabout.co.uk/hydro-muscle-max/

Vermil John said...

What you do no longer understand Tvolve is that many humans are in contrast to you. They visit the gymnasium with simply one component in thoughts. Have a exquisite exercise and cross domestic. so you are taking on their time. moreover, your chatter will even distract and annoy different humans. *Do now not Hog - Do be conscious that the health club equipments are for all to share.For more ==== >>>>>> http://boostupmuscles.com/tvolve-scam-fake/

Dway Elle said...

A working mother who is looking to live in form. i am no longer looking to run a marathon or hike 20 miles via a Alpha Tren dust pit dressed like a ballerina, however i am trying to live active, construct muscle and lose fats. i have found out first hand that after you turn forty - it is all downhill. Your metabolism slows down (which completely sucks) and i've a number of accidents that i am dealing.For more ==== >>>>>> http://www.strongtesterone.com/alpha-tren-reviews/

Eve Ame said...

Cooking dishes for the own family, Alpha Tren eating place, or for private consumption the usage of meat is a routine done through humans. With this, it's miles predicted that there may be positive chops for every and each piece of meat that an person partakes. whether or not it's loin, grounded, or brisket, such cuts are very vital in preparation for the favored dish one desires to eat. For more ==== >>>>>> http://jackedmuscleextremeadvice.com/alpha-tren/

Claris Kray said...

So now you're all ready to add some serious muscle gains by using grabbing a jar of whey protein. Which whey Alpha Trenprotein might you select? There are sorts of why protein which can be to be had in the marketplace: concentrates and isolates. Purity is what makes the 2 distinct. An isolate carries extra protein and much less lactose and fat in line with serving, and is that this taken into consideration greater pure than a listen. For more ==== >>>>>> http://free4trial.com/alpha-tren/

DEANNA RODGERS said...

BodyĆ¢s poisonous elements are distinctly removed on this regard so that your body cells may be energized with fantastic T-90 Xplode strength which can be later on used for conducting extraordinary varieties of day by day activities and sporting events. in case you are inclined to recognize greater approximately the beneficial impacts and the herbal components of these quality.For more ==== >>>>>> http://t-rexmuscleadvice.com/t-90-xplode/

duncawal495 said...

And why would it no longer be Xplosive Vital the size of the penis is the mark of manhood and the signal of fertility as properly. It defines greater satisfaction, a amazing intercourse lifestyles and manliness as well.the secret behind the increase of the penis starts offevolved from puberty during which the body goes thru natural growth manner. For more ==== >>>>>> http://www.strongtesterone.com/xplosive-vital/

andrea jean said...

A hard and fast of thrice per week bodybuilding sporting events might be perfect for muscle enhancement. but if you Xplosive Vitalfeel you may tolerate extra than this, possibly four instances in step with week, then that would be excellent. Squats, dips, and dead lifts are sporting activities for muscle constructing.For more ==== >>>>>> http://boostupmuscles.com/xplosive-vital-reviews/

Floren joy said...

One has to take one pill a day and notice the way it works surprise within 30 days. 6 months of ProSolution tablet intake Infinite Male Enhancement is recommended and no prescription is required for tablet consumption. The pill could function better if some penile stretching and power schooling sports are completed in conjunction with pill intake. The exercising application must continue for a hundred and twenty days from the primary day of pill consumption. For more ==== >>>>>> http://www.strongtesterone.com/infinite-male-enhancement/

duncawal495 said...

But there are additional all natural and also with no adverse effects approaches that you could make use of to keep Infinite Male Enhancement your levels healthy which is with a supplement called Vydox that just appeared on the market just recently. This supplement has become so preferred that it is actually being made use of by expert athletes like PGA Golf player Brian Gay that strongly endorse Vydox.For more ==== >>>>>> http://www.strongtesterone.com/infinite-male-enhancement/

calven glen said...

T-Volve ntramuscular injections, usually T-Volve each two or 3 weeks.estosterone patches worn both on the body or at the scrotum T-Volve the sac that contains the testicles. these patches are used daily. The body patch software is rotated among the buttocks, arms, again or abdomen.Testosterone gels that are implemented day by day to the shoulders, higher palms, or stomach. Testosterone replacement remedy clinics assist sufferers identify the type of remedy first-rate acceptable to their condition and frame and inspire regular take a look at-americawhich will preserve a right stage of testosterone within the frame of all of the sufferers affected by a deficiency.For more ==== >>>>>> http://www.healthsupreviews.com/t-volve/

TwanF elt said...

XCELE BRAIN best the major strategies that may XCELE BRAIN be utilized by distinct ages to boost mind power. all the techniques which are discussed here are in form of physical activities. developing motor pathways. that is an important exercise endorsed for people in all ages. XCELE BRAIN Even at old age, you can rent this exercising to boost your brain energy. Tightening up connections in addition to new pathways is quite simple so long as someone is constant in the exercising. For more ==== >>>>>> http://www.strongtesterone.com/xcele-brain-reviews/

herly kegan said...

XCELE BRAIN It handiest takes a few XCELE BRAIN clean easy steps XCELE BRAIN to widen your horizons in existence, paintings, and school. The 21st century is referred to as a time where expertise is same to gold, that means that being informed equals money. Reciting some easy steps every day will boom your know-how quicker, because of being capable of read more data and preserve extra data in shorter intervals of time. For more ==== >>>>>> http://boostupmuscles.com/xcele-brain/

herly kegan said...

Nitro MXS It improves sensation in the genitals. Nitro MXS It's miles one of the proven herbal treatments to reinforce male libido.It energizes and rejuvenates your reproductive organs. It boosts patience and energy to closing longer in bed. It rejuvenates cells and muscle mass. It improves Nitro MXS functioning of your heart and cures popular weakness. It reverses growing old effects and boosts immunity. Therefore, normal intake of shilajit es tablets is one of the great herbal methods to enhance male strength.For more ==== >>>>>> http://jackedmuscleextremeadvice.com/nitro-mxs-scam/

Floren joy said...

Alpha Tren Another source of power is through getting Alpha Tren an ok amount of rest. Experts say that approximately six to eight hours of sleep is recommended for the frame's practise for every other day's Alpha Tren paintings. Through dozing or without a doubt sitting down and relaxing, the frame is relieved of strain. Whilst the body is absolutely relieved of strain, muscle mass begin to paintings nicely and it takes an amount of labor for the muscle groups to lose their capability at one hundred percent.For more ==== >>>>>> http://boostupmuscles.com/alpha-tren/

DEANNA RODGERS said...

Juggernox It also improves emotions, Juggernox which lets in you to revel in greater inspired and willing to get perform carried out. Juggernox muscle constructing exercising supplements customers not best experience improved garage and pay attention, but a few have discovered sensation higher ranges of psychological power and high-quality. while combined with the incredible mma muscle constructing exercising dietary supplements boom your tiers of.For more ==== >>>>>> http://www.strongtesterone.com/juggernox/

Wal Tosado said...

Pyramids are very useful for Juggernox learning the proper execution of sporting events. With little or no weight on the start, form might be mounted after which weight may be extended until the exercise is performed with the proper weight, repetitions and accurate form. The element to bear in mind is never sacrifice form at the same time as the repetitions are geared in the direction of elevate-of entirety failure.For more ==== >>>>>> http://jackedmuscleextremeadvice.com/juggernox/

ecgrady debbi said...

Have you read one skin care tip after any Eco Maxx other and felt uncertain about which one you need to observe? if you open up magazines or surf the web, you'll find masses of pores and skin care tips and regularly it is definitely hit or leave out. it may be very difficult to kind through the hype to find honest, sincere, clinically validated pores and skin care advice. we are able to speak how to kind lots of the statistics available to get to the easy statistics fast and without difficulty.For more ==== >>>>>> http://www.skinshining.com/eco-maxx-anti-aging-cream/

ecgrady debbi said...

Later, this exercising could come to be Mega Maximus one of the 3 gauges of powerlifting. The deadlift is a loose-weight workout that utilizes a loaded barbell. starting from a dishonest over position the barbell is lifted from the floor up into a status role with the fingers completely prolonged. There are unique variations of the deadlift, but the concept is the same. The deadlift is a compound workout, that means a couple of muscle group and/or joint is worried.For more ==== >>>>>> http://www.strongtesterone.com/mega-maximus/

Leema Koi said...

As soon as such emotional states are Relaxphin brought beneath manipulate, the reversal of an awful lot of the negative strain one experiences is equally delivered beneath manage. That helps repair the mind's natural state of harmony while also putting off the effect pressure yields at the frame. In fact, the impact of stress on the frame should show to be even extra difficult.For more ==== >>>>>> http://boostupmuscles.com/relaxphin/

ecgrady debbi said...

weight but not gaining any muscle tissues. Juggernox the solution isn't more repetitions. Lifting extra weight at a better intensity is what you need to do. an appropriate scenario is which you work out for a shorter period of time, with much less frequency, but you work your muscle tissues tons more difficult than you're now. To gather lean muscle, you need to workout less however with more.For more ==== >>>>>> http://www.healthsupreviews.com/juggernox/

Leema Koi said...

Sunburn is any other hassle that impacts Satin Youth Cream skin immediately. predominant cause for this trouble is over exposure to warm solar's rays. At gift, the various lotions and extracts used for shielding skin from UV rays of sun are introduced with aloe vera. It soothes skin floor thoroughly and naturally. Aloe Vera Gel is a super preference for everybody searching for a product that may keep your skin silky and shining. For more ==== >>>>>> http://www.strongtesterone.com/satin-youth-cream/

CLAUDIA BALDWIN said...

A studies indicates that sixty nine% of usa Penetrex Male Enhancement citizens normally take herbal merchandise, vitamins and minerals and or any known natural fitness products. herbal health merchandise are defined as: nutrients and minerals, remedies or aide which includes alteril for insomnia, fitness medicines, conventional medicines which include chinese language and Thai, Probiotics and Others like vital fatty and amino acids .For more ==== >>>>>> http://boostupmuscles.com/penetrex-male-enhancement/

CLAUDIA BALDWIN said...

Are generally accessible if you'd want to Brick Muscle acquire larger in addition to fuller breasts without a present process surgical treatment, you'll be able to pick out from a variety of natural dietary supplements which might be created from natural substances. natural breast enhancement lotions can also be employed as they may truely stimulate the improvement of your breasts. For more ==== >>>>>> http://www.strongtesterone.com/brick-muscle/