Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I write very long lines of code?

Tags:

python

pep8

if i have a very long line of a code, is it possible to continue it on the next line for example:

 url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
+ '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:'
like image 812
Alex Gordon Avatar asked Aug 03 '10 23:08

Alex Gordon


People also ask

What do you do when code line is too long?

If you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation.

How many lines of code is too much?

My advice is to avoid having many statements (or lines of code) in a single method or function. I would recommend having less than a thousand lines of code in each of them (and usually, just a hundred or two).

How do you write long lines in Python?

You can also wrap long lines in Python using the '\' operator. It looks better if you use a backslash. Make sure the continuation line is properly indented.

How do you break a long line in C++?

The \n Character The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier.


1 Answers

I would write it like this

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
     '100,000|1,000,000&chxp=1,0&chxr=0,0,%(max_freq)s300|1,0,3&chxs=0,676767'
     ',13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465'
     '&cht=bvs&chco=A2C180&chds=0,300&chd=t:'%{'max_freq': max(freq)})

Note that the + are not required to join the strings. It is better this way because the strings are joined at compile time instead of runtime.

I've also embedded %(max_freq)s in your string, this is substituted in from the dict at the end

Also check out urllib.urlencode() if you want to make your url handling simpler

like image 119
John La Rooy Avatar answered Oct 14 '22 12:10

John La Rooy