Имя пользователя:

Пароль:


Список форумов КАНАДА Работа Программирование и IT Просмотров: 268 Промотать вниз к быстрому ответу

Элегантное программирование: Функции должны быть простыми


Давим клаву за бабло
   Поделиться темой: 
  #1
Сообщение 29 дек 2013, 16:45
Ursego Аватара пользователя
СОЗДАТЕЛЬ ТЕМЫ
Canada, Ontario
Город: Toronto
Стаж: 4 года 3 месяца 16 дней
Постов: 9105
Лайкнули: 2623 раз
Карма: 29%
СССР: Днепропетровск
Пол: М
Лучше обращаться на: ты
Заход: 1 минуту назад

Разделяйте программу на подпрограммы, каждая из которых выполняет одну чётко определённую задачу - эта концепция называется "Разделение ответственности". Код должен быть организован следующим образом: главная (корневая) подпрограмма вызывает подпрограммы (имеющие качественное, "говорящее" название), те в свою очередь точно также вызывают другие подпрограммы - и т.д. Это позволит программисту "перемещаться" вверх и вниз по уровням абстракции (всякий раз концентрируясь на логике соответствующего уровня) по иерархиям любой глубины вложенности.

В разных языках программирования подпрограммы именуются по-разному (методы, функции, процедуры, сабрутины и т.д.) но далее я буду использовать термин "функция".

Описываемого подхода следует придерживаться не только если код будет использоваться многократно (т.е. функция будет вызываться из разных мест), но даже и тогда, когда будет лишь один вызов. Это не беда если конкретный бизнес-процесс будет разбит на десятки функций - разработчик всегда может сфокусировать внимание на одной. А вот если впихнуть всё в одну функцию (длинную, как рулон туалетной бумаги), то будет очень сложно разобраться что к чему. Я уверен, что Людовик XI произнёс свою знаменитую фразу "Разделяй и властвуй" именно о программировании, и действительно: будешь разделять ответственность между функциями - будешь властвовать над всей аппликацией! Ведь если придерживаться этого правила, то можно создавать простой код даже если вся система очень сложная.

Для поддержания простоты функций можете воспользоваться следующими советами:

В идеале длина функции не должна превышать один экран (не считая верхнего комментария-"шапки"). Два экрана - всё ещё приемлемо, а вот три уже сигнализируют о неправильной организации кода - кроме случаев, когда выполняется длинная "чёрная работа" которую нет смысла разбивать на фрагменты - например, когда функция обрабатывает большое число полей, полученных от внешнего сервиса.

Другой приемлемый совет я нашёл в одной из книг:
функция должна состоять из не более, чем 100 строк кода не считая комментариев.


Обратите внимание, что проблема слишком длинных функций обычно идёт в комплекте с проблемой избыточного отступа!

А вот что написал Jorn Olmheim в книге "97 Things Every Programmer Should Know":
There is one quote, from Plato, that I think is particularly good for all software developers to know and keep close to their hearts:

"Beauty of style and harmony and grace and good rhythm depends on simplicity."

In one sentence, this sums up the values that we as software developers should aspire to. There are a number of things we strive for in our code:

Readability
Maintainability
Speed of development
The elusive quality of beauty

Plato is telling us that the enabling factor for all of these qualities is simplicity.

I have found that code that resonates with me, and that I consider beautiful, has a number of properties in common. Chief among these is simplicity. I find that no matter how complex the total application or system is, the individual parts have to be kept simple: simple objects with a single responsibility containing similarly simple, focused methods with descriptive names.

The bottom line is that beautiful code is simple code. Each individual part is kept simple with simple responsibilities and simple relationships with the other parts of the system. This is the way we can keep our systems maintainable over time, with clean, simple, testable code, ensuring a high speed of development throughout the lifetime of the system.

Beauty is born of and found in simplicity.

Steve McConnell пишет в своей знаменитой "Code Complete":
A large percentage of routines in object-oriented programs will be accessor routines, which will be very short. From time to time, a complex algorithm will lead to a longer routine, and in those circumstances, the routine should be allowed to grow organically up to 100-200 lines.

Let issues such as the routine's cohesion, depth of nesting, number of variables, number of decision points, number of comments needed to explain the routine, and other complexity-related considerations dictate the length of the routine rather than imposing a length restriction per se.

That said, if you want to write routines longer than about 200 lines, be careful. None of the studies that reported decreased cost, decreased error rates, or both with larger routines distinguished among sizes larger than 200 lines, and you're bound to run into an upper limit of understandability as you pass 200 lines of code.

Ну и напоследок - несколько идей, найденных в интернете:
When reading code for a single function, you should be able to remember (mostly) what it is doing from beginning to the end. If you get partway through a function and start thinking "what was this function supposed to be doing again?" then that's a sure sign that it's too long...

Usually if it can't fit on my screen, it's a candidate for refactoring. But, screensize does vary, so I usually look for under 25-30 lines.

IMO you should worry about keeping your methods short and having them do one "thing" equally. I have seen a lot of cases where a method does "one" thing that requires extremely verbose code - generating an XML document, for example, and it's not an excuse for letting the method grow extremely long.

...you should make functions as small as you can make them, as long as they remain discrete sensible operations in your domain. If you break a function ab() up into a() and b() and it would NEVER make sense to call a() without immediately calling b() afterwards, you haven't gained much.

Perhaps it's easier to test or refactor, you might argue, but if it really never makes sense to call a() without b(), then the more valuable test is a() followed by b().

Make them as simple and short as they can be, but no simpler!"

As a rule of thumb, I'd say that any method that does not fit on your screen is in dire need of refactoring (you should be able to grasp what a method is doing without having to scroll. Remember that you spend much more time reading code than writing it).

~20 lines is a reasonable maximum, though.

Aside from method length, you should watch out for cyclomatic complexity i.e. the number of different paths that can be followed inside the method. Reducing cyclomatic complexity is as important as reducing method length (because a high CC reveals a method that is difficult to test, debug and understand).

During my first years of study, we had to write methods/functions with no more than 25 lines (and 80 columns max for each line). Nowadays I'm free to code the way I want but I think being forced to code that way was a good thing ... By writing small methods/functions, you more easily divide your code into reusable components, it's easier to test, and it's easier to maintain.

I often end up writing C# methods with 10 - 30 lines. Sometimes I find longer methods suitable, when it’s easier to read/test/maintain.

My problem with big functions is that I want to hold everything that's happening in the code, in my head all at once. That's really the key. It also means that we're talking about a moving target.

Dense perl should appear in shorter chunks than padded VB. And, worse, at only 38, I'm finding that while my abilities have matured in nice ways, by core ability to grok a bunch of code is diminishing and thus the size of code blocks I want to handle is shrinking.

Because the goal is usability, the one screen rule really does make sense even though you can point to seeming flaws like varying screen resolutions. If you can see it all at once without paging around the editor, you are very much more likely to handle it all as a block.

What if you're working on a team? I suppose the best thing for the team would be to determine the lowest common denominator and target that size. If you have someone with a short attention-span or an IDE set up displaying around 20 lines, that's probably a good target. Another team might be good with 50.

And yeah, whatever your target is, sometimes you'll go over. 40 lines instead of 25 when the function can't really be broken up reasonably is fine here and there. You and your partners will deal with it. But the 3K line single-function VB6 modules that exist in some of my employer's legacy suites are insane!

I prefer to try to keep everything in a function on the same level of abstraction and as short as possibly. Programming is all about managing complexity and short one purpose functions make it easier to do that.

Kent Beck пишет в книге "Smalltalk Best Practice Patterns":
Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.


Чтобы ответить в этой теме, зарегистрируйтесь или быстро войдите через соцсеть: