3

Routines, procedures, methods - whatever you call them, they are important building blocks for us developers. What single characteristic would you rate as the most important one?

(By providing one characteristic per answer, it is possible to vote for them individually. I.e. the purpose of this question is not to decide single out one characteristic, but rather, to highlight all of the important ones.)

Ola Eldøy
  • 5,720
  • 7
  • 49
  • 82
  • 1
    I'm detecting a leading question here... What do you think? – Jon 'links in bio' Ericson Dec 12 '08 at 22:01
  • -1: I think there are several dimensions to quality, and picking one of them is not helpful. There's "meeting the need", "performance", 'maintainability", "adaptability" and "cost of ownership", which are orthogonal. Lifting up one seems like a bad idea. – S.Lott Dec 12 '08 at 22:11
  • You are obviously right! No single characteristic alone is enough to make a good routine. The idea is actually to highlight all these features. So you could write an answer for each one of them. – Ola Eldøy Dec 12 '08 at 22:15
  • "So you could write an answer for each one of them". That's precisely what I'm rejecting as not practical or even helpful. Since context defines the relative ranking of the various attributes, there's no possible answer outside a specific project/problem domain. – S.Lott Dec 12 '08 at 22:23
  • 1
    S.Lott It seems to me you are thinking at a different level than routine level.Most characteristics that set a good routine apart from a bad one are orthogonal to the criteria you mention in your first comment. For example, how a good name or a single purpose affect performance or cost of ownership? – Vinko Vrsalovic Dec 12 '08 at 22:29
  • Wrong question: there *is* no *single* characteristic that distinguishes a good routine from a bad one. – Charlie Martin Dec 13 '08 at 00:27
  • Charlie, the purpose of this question is not to decide which one, but which ones, are the important ones. – Ola Eldøy Dec 13 '08 at 04:33

20 Answers20

16

I think the most important criteria would be that it has a single purpose.

After that, that it satisfies that purpose (and only that purpose) correctly.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
8

Self Commenting Procedure Names.

Examples: GetStoreFromAddress GetCarsByMake

FallenAvatar
  • 4,079
  • 1
  • 21
  • 24
4

It should be easily unit tested.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
4

The routine's name maps one to one to it's functionality.

It's surprising how often a function X does X and also Y, or most of X but not all of X.

Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
4

There is no single criterion that distinguishes a good routine from a bad one.

Among the criteria are:

  • conceptual integrity: it does something that can be described in a simple short form, one sentence or paragraph;
  • loose coupling: its behavior is not sensitive to what goes on in code around it;
  • reasonable size: long routines are harder to read and understand, and are less likely to have good conceptual integrity;
  • Parnas's criterion: they "hide" one thing that can change, so that requirements changes have limited effect on the rest of the system.
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
3

designed to be easily read and understood by humans - without that in place it is much harder to modify it to have all of the other wonderful attributes that will be listed here

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
3

Number of things it tries to do.

If this isn't exactly 1 then you probably have a problem.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

It shouldn't have unexpected side-effects.

Cristian Libardo
  • 9,260
  • 3
  • 35
  • 41
2

good error handling (reliability)

2

brevity

(this was supposed to be a semi-fun answer, but SO wouldn't let be post the single word on its own!)

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

It has to be atomic

NotMe
  • 87,343
  • 27
  • 171
  • 245
1

Lines of code.

Whytespot
  • 921
  • 12
  • 18
1

You should track the number of edits required after the routine was put into use. A 'good' routine is one with few edits required. A 'bad' routine definitely proves itself to be so when there are a bunch of fixes required.

This can easily be accomplished with a comment header on each method call that gets updated after each edit.

Darian Miller
  • 7,808
  • 3
  • 43
  • 62
1

It does one thing or delegates multiple things to other functions

jmucchiello
  • 18,754
  • 7
  • 41
  • 61
1

Clarity - Easy to understand

marcumka
  • 1,695
  • 3
  • 12
  • 14
1

I think this is more easily answered if you consider routines as part of an API. There aren't many routines that stand alone, at least not in a truly useful system. So honestly, I think the most important things to consider when writing routines are:

  1. Intuitiveness How intuitive is my set of instructions -- will people understand the purpose without having to wade through a lot of documentation?

  2. Orthogonality How orthogonal are my routines? Does each accomplish one particular task, or are there multiple (but slightly different) ways to do the same thing? If there are, this is bad, and the API probably needs to be redesigned.

  3. Compactness How much of the API does it take to get simple tasks done? Do I need to learn a lot of stuff to get something done, or can I suffice with just a couple routines that do something intuitive and powerful? You need to weigh the tradeoffs of this one with orthogonality to strike a good balance for your particular domain.

Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96
  • That's nice. Why not split it into three answers then? – Ola Eldøy Dec 12 '08 at 22:08
  • Maybe I shouldn't have answered the question b/c I think it oversimplifies. I'm saying that many times you can't consider a routine by itself in any meaningful sense. These three go together for considering all the routines in a module or API. – Todd Gamblin Dec 12 '08 at 22:14
  • Thank you for your input, though. The points you made are very good ones! – Ola Eldøy Dec 12 '08 at 22:17
  • I disagree. You certainly can analyze a single routine and say if it's a bad routine or a good routine. You can (and should) also analyze at the API level, but it's just that, a different level. – Vinko Vrsalovic Dec 12 '08 at 22:20
1

From the routine name, you can say what the routine does (and when you check the code, you realize that you were right ;-)

gius
  • 9,289
  • 3
  • 33
  • 62
1

The routine uses a consistent level of abstraction throughout.

Jimmy
  • 89,068
  • 17
  • 119
  • 137
1

I would say well documented (and actually enforced) pre and post conditions.

0

A single return point

Rich
  • 896
  • 6
  • 15