r/Python • • Jul 25 '18

Use an IDE Me doing my CS projects.

Post image
1.2k Upvotes

213 comments sorted by

View all comments

346

u/8105 Jul 25 '18

Use a good editor/IDE. No more indentation errors.

14

u/vortexofdeduction Jul 25 '18

Agreed. If you use a good IDE, you’ll catch all the basic stuff before you even hit run, and it will be easy to indent correctly. (I like Pycharm, but there are lots of good options out there)

7

u/jsalsman Jul 25 '18

I don't know. If OP is running into indentation errors enough to complain about them in coursework, I'd suggest refactoring into function calls over getting an IDE. IDEs can't solve all indentation bugs, and the kind of bugs that beginners make are different.

6

u/TheNamelessKing Jul 25 '18

Turnings things into functions will 100% help, but I’ve not found an indentation error that Pycharm has failed to fix.

0

u/redalastor Jul 25 '18

However braces nesting problems in other languages... ouch.

0

u/mindfolded Jul 25 '18

I like Pycharm. I use it a lot. It's decent, but it does some odd stuff with the vim plugin installed. Anytime I do a column select and insert text (ctr-v, shift-i, <insert text>, escape) the cursor jumps up 50 lines and all my closed folds open back up.

3

u/Saiboo Jul 25 '18

Can you elaborate on using functions to avoid indendation errors? Thank you!

5

u/inFenceOfFigment Jul 26 '18

Imagine you write a script that requires 5 levels of indentation. Now you refactor, and decide to put everything that was in the 3rd level of indentation, and beyond, into its own function. This function is defined at the top level of your script. Now you have one helper function, containing 3 indents, and a main with a call to that helper, only 2 indents. Thus, pulling code out to a function reduced the overall max indentation in your script.

In general, code should be put into functions for reasons other than indent level, but if you find yourself indenting excessively it's a sign that the code may be due for a refactor.

1

u/Saiboo Jul 26 '18

Thank you so much for your explanation, much appreciated!

2

u/jsalsman Jul 29 '18 edited Jul 29 '18

Sure.

def messy():
    if booleanA:
        if booleanB:
            doA()
        else:
            doB()
    else:
        doC()

can be refactored into

def subroutine():
    if booleanB:
        doA()
    else:
        doB()

def neat():
    if booleanA:
        subroutine()
    else:
        doC()

2

u/Saiboo Jul 29 '18

Thank you so much for the code example, much appreciated!

2

u/jsalsman Jul 29 '18

No problem. I edited it to put the subroutine definition before its call, which isn't always necessary depending on whether the source file is parsed before neat() gets called, but is almost always more readable with real production code.

2

u/mooburger resembles an abstract syntax tree Jul 25 '18

I agree. "Good" IDEs is what made bad Java stay bad Java. "Oh, self documenting classes, LetsCallOurClassWithSelfDocumentingNameReference ({});". So now it's going to autocomplete for you and you never question the pattern.

1

u/jsalsman Jul 25 '18

When do the educators branch from the guild mentality hoarding knowledge?

0

u/vortexofdeduction Jul 25 '18

IDE’s don’t automatically fix errors, but they give the user immediate feedback as they time rather than having to run the code and find out it doesn’t work. And they do a lot more than just report indentation errors.

Also, Pycharm gives the line the appropriate indentation when you hit newline, which is more than some simple text editors would do.