[GRASS-dev] new addon on the way

Vaclav Petras wenzeslaus at gmail.com
Mon Nov 16 20:45:42 PST 2020


On Mon, Nov 16, 2020 at 8:09 AM Veronica Andreo <veroandreo at gmail.com>
wrote:

> Then, I'll be working on writing (a bit) better code. Is it recommended to
> create classes?
>

Hi Vero,

Classes can be sometimes useful for writing things like scripts and GRASS
modules, but usually in these cases, just writing functions instead of
having everything in one long function does what you need which is
understandable and maintainable code.

A class could be useful to carry some state through your code gluing most
related variables and functions together like this:

p = Process(some, vars, always, needed)
result1 = p.phase_one_of_processing(some, other, vars)
result2 = p.another_phase_of_processing(result1, and, even, more, vars)
...

However, that's nothing you can't do well with just functions or functions
and a class which has only data attributes, but no functions. Sometimes, it
is a matter of taste. Sometimes, you may see that the functions are somehow
not quite fitting to do what you need to do and that's where classes might
come in.

With your current code, I would just run Pylint on in with default settings
and try to make it as happy as possible. Pylint usually forces me to split
big chunks of code into smaller documented functions with more clear inputs
and outputs. You can also run Black on it and forget about details of
formatting. I didn't really look into the logic of your code, but I think
your code looks good and readable, so you don't have much to worry about
even when you start applying some strict tools such as Pylint.

HTH,
Vaclav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20201116/db336ed3/attachment.html>


More information about the grass-dev mailing list