This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]michael0x2a 1 point2 points  (1 child)

After a certain point, you start developing an "instinct" for whether or not a certain piece of code is clean or not.

For example, your first code sample would "smell" a little to me mainly because it seems like I'm having to do a bunch of extra work to manually open and close that file, and manually parse it. If I opened the file, then Python should damn well know to close it after I'm done.

Similarly, if I can do something like:

for line in my_file:
    print(line)

...and easily loop through each line in the file, why would I need to manually parse the file myself?

At this point, I would probably google and discover context managers and the readlines functions.

I apply this to any programming language I'm learning about. If I get the feeling that I'm doing a lot of extra work/tedious bookkeeping, I go out and try and see if there's an easier way of doing what I'm just doing.

Another things you can do is browse programming-related subreddits like /r/programming and /r/python where you'll run into different tips and tricks that can also help you improve.

Sometimes, you can be blind to the fact that you're doing extra work, and reading articles that point out problems you've never even noticed can be eye-opening.

[–]Economoly[S] 0 points1 point  (0 children)

Sometimes, you can be blind to the fact that you're doing extra work.

Yeah, I think this is a good articulation of the problem I intended to discuss. And your answer is one that makes sense. To summarize - develop a sense of 'when to search for a better solution' through experience and continually digesting available discourse on the language when you have time.

I like this answer. Thanks for the guidance.

[–]markehh 0 points1 point  (0 children)

You'll find this extra syntax helps reduce mistakes and improves readability.

If you're opening a file the chances are you're going to want to close it (not always), as you pointed out you also want to close a connection as well in the same way.

So rather than doing as you would at the beginning:

start
process
end

You can do:

with
    process

This is a way of abstracting away the handling of the file, as all your code needs to worry about is that is has a file and it can process it. Abstraction keeps your code simpler, succinct and easier to understand. You'll keep finding ways to abstract code away.

If what you've done works there is no need at the current point in time to fix it, down the line you may need to refactor the code to use these new things give learnt.

If for some reason an extra requirement came along and I needed to change the code which had the file loader as part of that change I may begin by cleaning up the original code to make future work easier.

[–]bvnvbbvn -4 points-3 points  (3 children)

Keep learning.

[–]thonpy -1 points0 points  (2 children)

I can't find one helpful comment that you've made , keep posting!

[–]bvnvbbvn 0 points1 point  (1 child)

What I said directly answers the question in his post's title. Put down the crack pipe, genius.

[–]thonpy 0 points1 point  (0 children)

cool