Server programming

K

koranthala

Hi,
Is server programming in Python procedure oriented or object
oriented?
I have this question because lately I am asked to make a medium
complex web program (extremely database oriented) using Django. When I
used to do application programs earlier (in Python itself), the whole
thing being object oriented came out easily in programming. So, I was
able to use different patterns etc for programming and the whole thing
was - quite fun to design and create.
But when I program in Django, since I just have to work on user
responses - everything else being taken care of by Django - only, the
complete coding has become procedure oriented. It is not kludgy or
anything, but it doesnt have the nice feeling that we get when we code
a proper object oriented program.
Is my coding in error here? This is infact my first web program,
so it might be the reason. What does other people find? Does web
server programming using web frameworks (Django, TurboGears etc) make
it procedure oriented? If I am in the wrong, it might be due to a
wrong design or mindset, and I would like to change it.
 
S

Steve Holden

koranthala said:
Hi,
Is server programming in Python procedure oriented or object
oriented?
I have this question because lately I am asked to make a medium
complex web program (extremely database oriented) using Django. When I
used to do application programs earlier (in Python itself), the whole
thing being object oriented came out easily in programming. So, I was
able to use different patterns etc for programming and the whole thing
was - quite fun to design and create.
But when I program in Django, since I just have to work on user
responses - everything else being taken care of by Django - only, the
complete coding has become procedure oriented. It is not kludgy or
anything, but it doesnt have the nice feeling that we get when we code
a proper object oriented program.
Is my coding in error here? This is infact my first web program,
so it might be the reason. What does other people find? Does web
server programming using web frameworks (Django, TurboGears etc) make
it procedure oriented? If I am in the wrong, it might be due to a
wrong design or mindset, and I would like to change it.
It's just that Django *seems* procedure-oriented because your views are
written as functions. But they take request objects as arguments and
return response objects as results, so the framework is object-oriented.
Also each model is a class, and querysets eventually yield lists of
instances.

I wouldn't worry too much until Django forces you to do somethign you
really don't want to do ...

regards
Steve
 
B

Bruno Desthuilliers

koranthala a écrit :
Hi,
Is server programming in Python procedure oriented or object
oriented?

It's how you want it to be.
I have this question because lately I am asked to make a medium
complex web program (extremely database oriented) using Django. When I
used to do application programs earlier (in Python itself), the whole
thing being object oriented came out easily in programming. So, I was
able to use different patterns etc for programming and the whole thing
was - quite fun to design and create.
But when I program in Django, since I just have to work on user
responses - everything else being taken care of by Django - only, the
complete coding has become procedure oriented. It is not kludgy or
anything, but it doesnt have the nice feeling that we get when we code
a proper object oriented program.

So you may want to learn to enjoy the nice feeling we get when we code a
proper procedural program - or a proper functional one FWIW !-)

There's nothing inherently wrong with procedural programming. Nor with a
mix of procedural, OO and functional code - which is usually the case in
Python. It's just a matter of using the right tool for the problem to
solve.
Is my coding in error here?

Don't know - I don't have access to your code. But my experience with
Django is that I tend to have quite a lot of code in models and
templatetags, and much less in the views themselves. So I wouldn't say
that "Django takes care of everything else". If you really ends up
writing pages upon pages of repeting procedural code in your views and
nothing in the other parts of the app, then yes, there might be
something wrong - probably a case of AnemicDomainModel, and possibly a
lack of knowledge of the whole framework. One of the reasons views are
usually implemented as functions is that in most cases, you shouldn't
have a need for more. FWIW, you sometimes don't even need to write a
specific view - Django's GenericViews can handle quite a lot of cases

Note also that Django doesn't _require_ that you use functions as view
handlers - any callable object will do. But given how the HTTP protocol
works and how Django is designed, there's more often than not just no
*need* for a custom callable object.

And finally, as Steve already mentioned, OO is first about objects, and
that's what you're dealing with in your views - request objects, session
objects, model objects etc...
This is infact my first web program,
so it might be the reason. What does other people find? Does web
server programming using web frameworks (Django, TurboGears etc) make
it procedure oriented?

Not necessarily, no. Some frameworks requires your request handlers to
be methods of classes FWIW, and nothing in Django prevents you from
doing so if you want.
 
K

koranthala

koranthalaa écrit :


It's how you want it to be.


So you may want to learn to enjoy the nice feeling we get when we code a
proper procedural program - or a proper functional one FWIW !-)

There's nothing inherently wrong with procedural programming. Nor with a
mix of procedural, OO and functional code - which is usually the case in
Python. It's just a matter of using the right tool for the problem to
solve.


Don't know - I don't have access to your code. But my experience with
Django is that I tend to have quite a lot of code in models and
templatetags, and much less in the views themselves. So I wouldn't say
that "Django takes care of everything else". If you really ends up
writing pages upon pages of repeting procedural code in your views and
nothing in the other parts of the app, then yes, there might be
something wrong - probably a case of AnemicDomainModel, and possibly a
lack of knowledge of the whole framework. One of the reasons views are
usually implemented as functions is that in most cases, you shouldn't
have a need for more. FWIW, you sometimes don't even need to write a
specific view - Django's GenericViews can handle quite a lot of cases

Note also that Django doesn't _require_ that you use functions as view
handlers - any callable object will do. But given how the HTTP protocol
works and how Django is designed, there's more often than not just no
*need* for a custom callable object.

And finally, as Steve already mentioned, OO is first about objects, and
that's what you're dealing with in your views - request objects, session
objects, model objects etc...


Not necessarily, no. Some frameworks requires your request handlers to
be methods of classes FWIW, and nothing in Django prevents you from
doing so if you want.

Hi Bruno,
After reading your email, I tried reworking my code so that most of
my logic moves to Models.
But, most probably because this is my first application
development, I am unable to do so.
For example:
I have Models A,B, C, D . Now, there is not much model specific
code (preprocessing before updating code inside Models)in it. Rather
most of the code is of the format:
data received and to be added to D. But for adding to D, check
whether it is already in C - if not add to C and B. etc...
Now, I tried putting this code inside Model D,but it does not seem
to belong there - since it modifies other Models.

Is keeping such code inside views against Django's/Application-
Developments philosophy? In that case, where will this go?
 
B

Bruno Desthuilliers

koranthala a écrit :
(snip)
Hi Bruno,
After reading your email, I tried reworking my code so that most of
my logic moves to Models.
But, most probably because this is my first application
development, I am unable to do so.
For example:
I have Models A,B, C, D . Now, there is not much model specific
code (preprocessing before updating code inside Models)in it. Rather
most of the code is of the format:
data received and to be added to D. But for adding to D, check
whether it is already in C - if not add to C and B. etc...

And you don't find it "model specific" ? Man, this is business rules,
and as such belongs to the models part, not to views. You surely want to
make sure these rules always apply, don't you ?
Now, I tried putting this code inside Model D,but it does not seem
to belong there - since it modifies other Models.

And ? Who said a Model (or ModelManager) shouldn't touch other models ?
Is keeping such code inside views against Django's/Application-
Developments philosophy?

FWIW, I see this antipattern (AnemicDomainModel) way too often in django
apps. This doesn't make it less of an antipattern.
In that case, where will this go?

It's impossible to say exactly where in the models module without a
sufficiant knowledge of the domain, rules etc. Sorry, but my crystal
ball is out for repair.
 
K

koranthala

koranthala a écrit :
(snip)


And you don't find it "model specific" ? Man, this is business rules,
and as such belongs to the models part, not to views. You surely want to
make sure these rules always apply, don't you ?


And ? Who said a Model (or ModelManager) shouldn't touch other models ?


FWIW, I see this antipattern (AnemicDomainModel) way too often in django
apps. This doesn't make it less of an antipattern.


It's impossible to say exactly where in the models module without a
sufficiant knowledge of the domain, rules etc. Sorry, but my crystal
ball is out for repair.

Thank you, Bruce.
Sorry for the rather naive questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top