473,811 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

messages from pylint - need some reasoning

Hello,

I am pylinting some software of mine.

Now pylint throws messages, and I know of pylint --help-msg to get
some more text. What is missing out are explanation, WHY some things
are bad, so I am searching for explanations and ways to improve my
code:

Example:
1st) "to many local variables"
I searched big G, and found: many local variables make it harder to
refactor, as all those variables will have to be passed to the
factored-out function. Even worse when the local variables are
mutable, and have to be passed back.
Similiar explanations I am searching for

2nd) "to many statements (in function / method)
okay, shorter functions are easier to grasp. Is there any more
reason?

3rd) space before operators, space after operators, space after ","
that's just readability, or is there some deaper reasoning?

4th) maximum line length
yeah, more then 80 chars suck when outputting to punching cards; but
any 21century reason for this default? (can and have made it higer)
5th) "Too many branches"
"Used when a function or method has too many branches, making it hard
to follow."

So what is the preferred way of repairng this?

Especially if the branches are something like:

if checkforconditi on1():
# inlinecode
# to handlecondition 1

if checkforconditi on2():
# inlinecode
# to handlecondition 1

[...]

and multiple conditions can be present at the same time.

Something like mytodolist=[ (tester1, handler1), (tester2,
handler2), ...] and

for tester, handler in mytodolist:
if tester(situatio n):
handler(situati on)

would get rid of the branches; BUT... I cannot see how that is really
easier to follow.
Who can give me some hints to improve my code or arguments to switch
of that warnings?

best wishes,

Harald

Oct 17 '08 #1
2 12348
On Fri, 17 Oct 2008 00:59:16 -0700, GHUM wrote:
Example:
1st) "to many local variables"
I searched big G, and found: many local variables make it harder to
refactor, as all those variables will have to be passed to the
factored-out function. Even worse when the local variables are mutable,
and have to be passed back.
Many local variables can also make it harder to follow what the function
is doing. As rule of thumb a function should do "just one thing", many
variables indicate the function might be doing too much. Or maybe, if
most of the variables belong together somehow, they are candidates for a
class to encapsulate them.

Pylint's default limit is set to 15. So there are more than 15 named
things one has to follow when that message appears.
Similiar explanations I am searching for

2nd) "to many statements (in function / method) okay, shorter functions
are easier to grasp. Is there any more reason?
I think that's *the* reason. Especially in a such a high level language
like Python most things can be expressed quite compact but still
readable. Huge function bodies are an indicator that a function is doing
too much and not just "one thing".

The more code in the function the higher the risk of many nested
indentations and hitting the maximum line length guide.
4th) maximum line length
yeah, more then 80 chars suck when outputting to punching cards; but any
21century reason for this default? (can and have made it higer)
80 characters are still the default width of most terminals and code is
often seen there in diffs or python shell sessions for example. And even
while there is more space on today's monitors, modern IDEs occupy it with
all sorts of function and class browsers, breakpoint windows, online help
etc. so that 80 characters is still a good choice for the editor window.

When posting to mailing lists or newsgroups 80 characters (actually just
75) are the safe limit to ensure that most people can read posted code
without annoying line breaks forced by the reading application. Not so
bad with languages like C because the code still works when copied and
pasted into a text file, but quite catastrophic in languages like Python
that rely on correct indentation.

Last but not least typesetting tells that about 60 to 70 characters are a
good line length to read texts. While program source is usually more
"light" there's still documentation and comments in the source that
should follow the guide lines of typesetting.
5th) "Too many branches"
"Used when a function or method has too many branches, making it hard to
follow."

So what is the preferred way of repairng this?
Breaking it into smaller functions.
Especially if the branches are something like:

if checkforconditi on1():
# inlinecode
# to handlecondition 1

if checkforconditi on2():
# inlinecode
# to handlecondition 1

[...]

and multiple conditions can be present at the same time.

Something like mytodolist=[ (tester1, handler1), (tester2, handler2),
...] and

for tester, handler in mytodolist:
if tester(situatio n):
handler(situati on)

would get rid of the branches; BUT... I cannot see how that is really
easier to follow.
Well, you have more, smaller functions that are themselves easier to
follow. And you can/should document each function. Something that you
might not have done (so extensively) in the one function version. Also
the separation of the former "inline code" reduces the possiblity of
names reused for different things in different branches and maybe even
side effects between the branches if they accidentally share objects.

And this refactoring takes an indentation level from the old inline code,
making it easier to keep the lines in the 80 characters limit.
Who can give me some hints to improve my code or arguments to switch of
that warnings?
Try to write the code with less names and statements. Write functions
that do just "one thing" and split big functions into smaller ones.

Ciao,
Marc 'BlackJack' Rintsch
Oct 17 '08 #2
GHUM <ha************ **@gmail.comwri tes:
Who can give me some hints to improve my code
In addition to the responses you've already had, I would highly
recommend you get ahold of the book “Code Complete”, which gives
excellent, reasoned advice on how to perform the line-by-line craft of
programming.

Read it cover to cover, and you will not only improve your programming
skill, you will also be able to explain to yourself *why* some styles
are better than others.

--
\ “It is difficult to get a man to understand something when his |
`\ salary depends upon his not understanding it.” —Upton Sinclair, |
_o__) 1935 |
Ben Finney
Oct 17 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2087
by: Frans Englich | last post by:
Hello, I take PyChecker partly as an recommender of good coding practice, but I cannot make sense of some of the messages. For example: runner.py:878: Function (main) has too many lines (201) What does this mean? Cannot functions be large? Or is it simply an advice that functions should be small and simple?
8
1655
by: clarket | last post by:
Hi, I was wondering if it was possible to have a variable count down in a window.confirm dialog box. Like the type of message some systems have to log users out automatically. E.g "You will be logged out in x seconds. Do you want to stay logged in. Yes No" where the x value would count down and if they didn't press yes then
4
5024
by: Michael Yanowitz | last post by:
Hello: I ran the new pylint and my code and I had a few questions on why those are warnings or what I can do to fix them: 1) W: 0: Too many lines in module (1587) Why is 1587 considered too many lines? Would there be necessarily be an advantage to split it up into 2 or 3 files? Can I up the limit? 2) C: 0: Missing required attribute "__revision__"
5
11151
by: Tuomas | last post by:
#!/usr/bin/python """test pydev_0.9.3/../pylint""" __revision__ = "test_mod 0.1 by TV 06/10/22" lst = lst = map(lambda x: x.strip(), lst) result = """ No config file found, using default configuration ************* Module test_mod
7
1799
by: montyphyton | last post by:
Some recent posts about Python programming style got me thinking. Since we have the PEP 8 which gives some guidelines about the style to be used, do we have any program that can check for violations of these guidelines within the source code? I understand that there are a lot of code beautifiers out there, but i haven't seen one specially tailored for Python... Is there even a desire in Python community for a program like this (by Python...
2
1289
by: lgfang | last post by:
Hi, I think this is a bug of pylint.el. But I failed finding a way to submit the bug neither in its official site nor in google. So I post it here wishing it may be useful for some buddies. The bug is that it uses "compile-internal" from "compile" without require compile. So "M-x pylint" will fail if compile hadn't been loaded in advance by any means.
2
2873
by: Mick Charles Beaver | last post by:
Hello, I've been looking into using PyLint on some of my programs, just as a best practices kind of thing. Here's a snippet: #====================================================================== if __name__ == '__main__': parser = optparse.OptionParser(usage='usage: %prog ') parser.add_option('-c', '--config',
2
3379
by: dmitrey | last post by:
Hi all, I have Eric 4.1.1, pylint and Eric pylint plugin installed, but I cannot find how to use pylint from Eric IDE GUI. Does anyone know? Thank you in advance, D.
1
222
by: Stefan Rank | last post by:
on 31.07.2008 11:29 Diez B. Roggisch said the following: <snip> <snip> Three installations of pylint 0.14.0 that I have access to from here: pylint.bat 0.14.0, astng 0.17.2, common 0.27.0 Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45)
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.