是否有一种良好做法,将复杂方法的部分功能划分为几个较小的方法,仅就良好的代码可读性而言(但也许为调用方法支付一些性能)?或者这是某种“坏风格”?
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
是否有一种良好做法,将复杂方法的部分功能划分为几个较小的方法,仅就良好的代码可读性而言(但也许为调用方法支付一些性能)?或者这是某种“坏风格”?
最基本的规则是,你的方法应该只做一件事。如果你察觉到它正在做许多不同的事情,那么你显然可以重新思考机会。
如果您到达了您的方法有单一责任但大小又过长的关卡, 请尝试将“ 帮助者” 功能提取到分离的方法中。 您甚至可以检测到可以被分隔的类的代码 。
开发TDD是避免这类问题的好方法,因为它真正有助于为测试目的将关注问题明确分开,避免在单一方法上出现一堆代码。 如果你不写简明的方法,就很难正确测试它们。
如果您想重用代码( CheckeruserExist code>, < code> < saveuser
和 < code> > saveOrder 。 您应该能够在必要时在代码的其他领域重新使用此功能。 将其分割成模块使您的代码更容易读 。
Unclebob (Robert C. Martin) 认为方法不应大于4-5行代码,他的座右铭是“Extract until you drop"。从个人角度看,我认为这是一个非常好的做法。
视觉工作室允许您通过按 CTRL+R, M 来提取一种方法 。
除了Claudio的回答之外,还有一些SMELLS, 如果被检测到, 这意味着你必须重构和分割你的代码。
1 - 不 < a href=>""http://www.artima.com/intv/dry.html" rel="不跟随 nofollow noreferrer" >DRY 代码:如果你发现自己复制/粘贴一些线到不止一个地方,这是一个臭味,需要放置在一个中心,并尽可能多地调用。
2. 有数百行线的方法:任何好的方法都不应超过50行代码,也许或多或少于这个数字,但请放心,这种346行的方法不是一件好事。
3 - 参数太多:你的方法中参数清单很长,使得可读性和代码质量更差。
4 代码入侵:使用许多从另一类中阻塞的方法,应该存在于这一类中。
希望这有帮助。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...