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

From Alban Hertroys
Subject Re: Calculating a moving average (Coding style)
Date
Msg-id 41F4C09F.1070004@magproductions.nl
Whole thread Raw
In response to Re: Calculating a moving average  (mstory@uchicago.edu)
Responses Re: Calculating a moving average (Coding style)
List pgsql-general
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...

--
Regards,

Alban Hertroys
MAG Productions

T: +31(0)53 4346874
F: +31(0)53 4346876
E: alban@magproductions.nl
W: http://www.magproductions.nl

pgsql-general by date:

Previous
From: Devrim GUNDUZ
Date:
Subject: Re: Weird problem in 8.0.0
Next
From: Russell Smith
Date:
Subject: Re: Calculating a moving average (Coding style)