Today I came across something with extension methods that can cause code to break. If an extension method’s signature is equivalent to an actual method on the class or interface the extension method will not be called. For example let say I have the following classes:
public class Foo
{
public object FindItem(int id)
{
//Some implimentation
}
}
public static class FooExtensions
{
public object FindItem(this Foo foo, int id)
{
//Some slightly different implementation.
}
}
The FindItem in FooExtensions will never be called unless you call it as FooExtensions.FindItem(foo, id);. In general this is not a horrible problem as you should avoid naming extension methods the same as an existing method. The problem comes in if someone extends or modifies a class so that the extension method was hidden. I discovered this the hard way. I had a class setup that looked like:
public class Foo
{
public object GetItem(int id)
{
//Some implimentation
}
}
public static class FooExtensions
{
public object FindItem(this Foo foo, int id)
{
//Some slightly different implementation.
}
}
GetItem and FindItem had subtly different behavior in how they returned a value. In 90% of the cases the two methods behaved the same. However, there was one case where they didn’t. During a refactor of some code I was trying to unify the language in the Foo interface and renamed GetItem to be FindItem. I didn’t realize that there was an extension method elsewhere in the code that this change would hide.
Inevitably, this change broke in the one case where the two methods did not behave in the same manner. However, it was unclear as to why the code broke. I spent a good hour looking at the broken code and could not determine why it ever worked. Since the extension method is hidden by the class’s method of the same name it does not appear when trying to find references. After searching for a solution to no avail, I made a change to the code call to what used to be the extension method version of FindItem. Interestingly, this made the code more efficient. It was only a month later i.e. today when I came across the extension method and found no references to it that I realized what had happened.
This experience has led me to some thoughts about good coding practices to avoid this issue.
- Make sure all your extension methods have unit tests associated with them. This issue would have been spotted in a unit test for the extension method if a test for it had existed. My project doesn't use a TDD approach and we don’t have full coverage so this error was not caught at this level. A good unit test would have caught the case that caused the error. This is the most straightforward and safest way to prevent the error.
- Collect the extension methods in a place that makes them easy to find. They probably should be within their own namespace. Part of the reason this error wasn't caught was that the offending extension method was in a general namespace that was referenced for other reasons. If a namespace suddenly was no longer needed, this would have been a clue about the cause of the problem.
- Be judicious with the use of extension methods. The FindItem method should have been part of Foo. It was a mistake to not put it there in the first place. Extension methods are really tempting for people who come from a procedural programming background. However, they should only be used as a mechanism to extend functionality when the code cannot be extended otherwise. They should not be used as way of not adding a method to an object or just providing a method that should be a static method to the class.
1kate spade handbags
ReplyDeletecanada goose outlet sale
north face outlet 70% off
north face jackets for women
christian louboutin outlet
coach outlet online store
north face coats
ralph lauren polo shirts
oakley sunglasses outlet
ferragamo outlet
coach outlet online store
kobe shoes
mont blanc pen
hermes belt
canada goose outlet
burberry scarf
gucci.com
nike outlet
burberry sale
north face denali jacket clearance
replica rolex watches
michael kors outlet store
michael kors handbags clearance 75% off
jordan 6
michael kors outlet online
louis vuitton belt
fitflops clearance
cheap jordans
pink jordans
polo ralph lauren factory store
ralph lauren polo outlet online
gucci outlet
cheap jordan shoes
nike high heels
coach factory outlet
coach outlet store
jordan 12
tods shoes
dior handbags
michael kors outlet store
new jordan
cheap nike air max
columbia outlet
cheap vans
1014hxt
Machine Learning Projects for Final Year machine learning projects for final year
DeleteDeep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.
Python Training in Chennai Python Training in Chennai Angular Training
michael kors outlet
ReplyDeleteversace sunglasses wholesale
nike store uk
birkenstocks
sac longchamp
polo ralph lauren outlet
burberry outlet canada
air max 90
reebok classic
michael kors handbags outlet
coach outlet store
michael kors outlet online
coach outlet online
reebok outlet store
converse outlet
nba jerseys wholesale
cheap nike air max
michael kors outlet online
louis vuitton uk
pandora charms uk
hollister clothing store
supra store
oakley sunglasses wholesale
pandora charms sale clearance
abercrombie kids
fitflops sale clearance
nike air max 95
yeezy boost 350 white
prada sunglasses wholesale
adidas shoes
polo ralph lauren men
michael kors bags
michael kors canada
hollister uk
adidas superstar white
rolex replica watches
michael kors uk
kate spade handbags
michael kors outlet
cartier
chenzhen201671
for beginners like me need a lot of reading and searching for information on various blogs. and articles that you share a very nice and inspires me .
ReplyDeletecara menggugurkan kandungan
obat aborsi
kalkulator kehamilan
ugg boots
ReplyDeletecheap jordans
rolex watches
coach outlet store
the north face outlet
canada goose jackets
gucci outlet
lebron james shoes 2016
nike free flyknit
polo ralph lauren outlet
20170217caiyan
Occasionally you logdown.com/ just do not intend to play a More Info video game or make use of an Machine Liker Download and Install on PC, APK Android Free application on an and also Mac to mount Machine Liker App BlueStacks if you're making use of a Mac PC or Laptop. Machine Liker Greater than Facebook.
ReplyDeleteAs specified over, the Revolution splashthat.com/ SE Single from BOB includes a flexible suspension system Website that is indicated to enhance safety and security and Best Jogging Stoller in June 2017 safety for your youngster.
ReplyDeletecc1226
ReplyDeletecoach factory outlet
burberry handbags
adidas superstar shoes
ugg boots
kate spade outlet
fred perry polo shirts
ugg boots
uggs outlet
ugg outlet
hermens
cc1226
coach outlet
ReplyDeletedoudoune moncler
ralph lauren
michael kors handbags
mulberry handbags
ugg boots
longchamp outlet
michael kors outlet
hermes handbags
oakley sunglasses
cc0206
adidas yeezy
ReplyDeleteair max
supreme shirts
rolex replica watches
red bottom heels
coach factorty outlet online
pandora charms outlet
louis vuitton handbags
bally outlet
air jordan shoes
clb20180521
jordan
ReplyDeletenike tn
jordans
adidas nmd for sale
louboutin
nike air force 1
columbia sportswear
jordan xx9
stussy clothing
mizuno
chenyingying20180604
lululemon outlet
ReplyDeletegucci outlet
links of london
michael kors outlet
nike store
nike air force
nike blazer
adidas outlet
air max 95
pandora
2018.6.26chenlixiang
polo ralph lauren
ReplyDeletecheap mlb jerseys
chelsea jersey
canada goose coats
nike factory outlet
coach outlet
swarovski outlet
jack wolfskin
hermes birkin
ray ban sunglasses
chanyuan2018.06.28
michael kors outlet
ReplyDeleteair jordan 4
nba jersey
tory burch outlet
cheap nfl jerseys
michael kors outlet
polo outlet
ugg outlet
canada goose outlet
pandora charms
yaoxuemei20180702
lebron 15
ReplyDeleteasics kayno
tom ford sunglasses
ray ban sunglasses
adidas eqt support
ray ban sunglasses
cheap jordans
michael kors handbags
goyard bags
off white hoodie
20180724 xiaoou
ReplyDeletetoms outlet
mulberry handbags sale
mulberry handbags
swarovski jewellery
cheap nba jerseys
air jordan shoes
fitflops sale clearance
bally shoes
pandora charms sale clearance
nobis jackets
seo fiyatları
ReplyDeletesaç ekimi
dedektör
instagram takipçi satın al
ankara evden eve nakliyat
fantezi iç giyim
sosyal medya yönetimi
mobil ödeme bozdurma
kripto para nasıl alınır
En Son Çıkan Perde Modelleri
ReplyDeleteNUMARA ONAY
mobil ödeme bozdurma
NFT NASİL ALINIR
Ankara Evden Eve Nakliyat
trafik sigortası
dedektör
WEBSİTESİ KURMA
aşk kitapları
smm panel
ReplyDeletesmm panel
İs ilanlari
İnstagram takipçi satın al
hirdavatciburada.com
WWW.BEYAZESYATEKNİKSERVİSİ.COM.TR
Servis
Tiktok Para Hilesi İndir
beykoz toshiba klima servisi
ReplyDeletetuzla vestel klima servisi
kadıköy arçelik klima servisi
kartal samsung klima servisi
pendik toshiba klima servisi
kadıköy lg klima servisi
maltepe alarko carrier klima servisi
ataşehir vestel klima servisi
çekmeköy bosch klima servisi
bostansepeti.com
ReplyDeletesite kurma
ürünler
vezirsosyalmedya.com
postegro
sosyal medya yönetimi
surucukursuburada.com
patent sorgula
ReplyDeleteyorumbudur.com
yorumlar
tiktok jeton hilesi
mobil ödeme bozdurma
mobil ödeme bozdurma
mobil ödeme bozdurma
pubg uc satın al
pubg uc satın al
betboo
ReplyDeletesüperbahis
1xbet
bedava bonus veren siteler
bonus veren siteler
1xbet
bahigo
anadolucasino
1xbet