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.
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
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
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