Re: Calculating a moving average (Coding style) - Mailing list pgsql-general

From Russell Smith
Subject Re: Calculating a moving average (Coding style)
Date
Msg-id 200501242107.08305.mr-russ@pws.com.au
Whole thread Raw
In response to Re: Calculating a moving average (Coding style)  (Alban Hertroys <alban@magproductions.nl>)
Responses Re: Calculating a moving average (Coding style)
List pgsql-general
On Mon, 24 Jan 2005 08:32 pm, Alban Hertroys wrote:
> mstory@uchicago.edu wrote:
> > CREATE OR REPLACE FUNCTION get_bar_avg() RETURNS TRIGGER AS '
> > DECLARE
> > bar_record RECORD;
> > x INTEGER;
> > y DOUBLE PRECISION := 0;
> > BEGIN
> >      IF TG_OP = ''INSERT'' THEN
> >           y := y + NEW.bar;
> ...
> >           RETURN NEW;
> >      ELSIF TG_OP = ''DELETE'' THEN
> >            x := 0;
> ...
> >            RETURN OLD;
> >       ELSE
> >            y := y + NEW.bar;
> ...
> >            RETURN NEW;
> >       END IF;
> > END;
> > ' LANGUAGE plpgsql;
>
> I see people do this from time to time. Just out of curiosity, is this
> considered good coding style, or is it considered "lazyness"? I'm not
> sure what to think of it.
>
> If I would have written this, there would have been 3 triggers w/o the
> check on TG_OP. Is there an important drawback to doing so? Is there any
> document on "preferred" coding style in PL/PGSQL?
>
> Yes, I'm a bit of a purist...
>
Given you have to define a function for each trigger, my view is why write more functions.

Along with this.  As a C programmer, I would do a few more IF tests in a function, rather than
write another one.  I find that triggers like this are one functional block and all go together.
Then when you update the function, it's all in one place.

Others may have "better" reasons for why they do it the way they do.  But they are mine.

Regards

Russell Smith.

pgsql-general by date:

Previous
From: Alban Hertroys
Date:
Subject: Re: Calculating a moving average (Coding style)
Next
From: William Yu
Date:
Subject: Re: serialization errors when inserting new records