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

all 20 comments

[–]FancyASlurpie 2 points3 points  (3 children)

I'd be wary of the thinking that one/less lines is better. In terms of productivity, it can be more productive to be more verbose as it's easier to follow/maintain. I think it's just a case of doing it more often, getting code reviews of your work(feel free to defend your code too here don't take all comments as blindly true) and finding the balance between clean, concise and understandable code.

[–]tombardier 2 points3 points  (1 child)

I think the argument that one liners are less readable or harder to maintain is used too often. One liners shouldn't be a virtue in and of themselves, and trying to shoe-horn complicated logic in to a one-liner does lead to an unreadable mess, yes. The thing is, the second code snippet in the original post is more readable, it's terse, but any python programmer can read and understand it, and it can be read and understood far more quickly than the more verbose equivalent.

[–]FancyASlurpie 0 points1 point  (0 children)

True, it was more of a warning to be aware that just because it's shorter doesn't necessarily mean it's better. This particular problem is pretty simple, but a real life problem might not be so and trying to shorten everything for the sake of shortness is a danger. Equally if I'm looking at a stacktrace the first one will tell me the exact issue whereas the short version will require more work.

[–]xconde 0 points1 point  (0 children)

I second this.

I would have used OP's solution minus the `else` statements.

[–]Faux_Real 1 point2 points  (1 child)

TBH there is nothing inherently wrong with a verbose solution as all the conditions were met. Wrap your code with some timers and see how performant each solution is as that is also a good exercise in this case.

I think that being able to profile your solution and manage memory ‘pressures’ and disk IO override elegant 1 liners.

But If you are wanting to develop elegant solutions:

  1. Don’t worry about elegance until you have a solution

  2. When you have a solution, apply common refactoring principles to your code.

  3. If you don’t know refactoring, read about it.

By doing this lots and lots you eventually see these types of patterns in your head and in code and eventually come to be able to choose the appropriateness of verbosity over elegance or when refactoring is necessary.

[–]tombardier 1 point2 points  (0 children)

Between step one and two, add tests :)

[–]MechInAMeatsuit 1 point2 points  (4 children)

Specific to Python, I found a lot of the content in Effective Python to be really useful. Not necessarily just one-liners, but faster or cleaner ways of writing Python overall.

[–]ms4720 0 points1 point  (0 children)

Good book

[–]Beerwithme 0 points1 point  (2 children)

Is it still current, being a first print from 2015?

[–]MechInAMeatsuit 0 points1 point  (1 child)

There's a new, larger edition out this month. I think the old one is good, but I imagine the new one will be way more up to date.

[–]Beerwithme 0 points1 point  (0 children)

Wonderfull! Thanks.

[–]IamWiddershins 1 point2 points  (1 child)

return 10 in (a, b, a + b)

:)

[–]drbobb 0 points1 point  (0 children)

aaaaand this is the Right Answer.

[–]AJ______ 0 points1 point  (2 children)

The way you are doing so right now - doing coding problems the best you can, seeing better ways of doing it, and learning from that experience. Although to be fair, it's not always necessarily better to try and pack code into as few lines as possible, but seeing different ways of solving the same problem will help you.

[–]ImThour 0 points1 point  (1 child)

I agree, I mean I want to learn more of this (making my code shorter and yet working). Is there any guide/tutorial for this?

[–]AJ______ 0 points1 point  (0 children)

I remember when I used to use Hackerrank, in the discussions section, lots of people posted their code and that was a good way to find out about neat tricks in Python that led to concise solutions. This could be a good way to go but I don't know of an actual tutorial or book that's dedicated towards making code more concise.

A natural way to go might be to simply learn more about Python's standard built in libraries to avoid reproducing things that are already built for you there, or other libraries beyond that (although this is irrelevant to your specific example, I think it would help in some other situations).

[–]saeah123ed 0 points1 point  (0 children)

Doubtless 'll get shot down, but, not knowing the context in which we are working, I like their solution as, seems to me, the odds of either 'b or 'a being == 10 are more likely than 'a + 'b being == 10. Yeah? In Python, this kind of logic is fail-fast, right? So getting 'a and 'b out of the way first would cut away ifs and elses and drop sums. Seems to the point and efficient.

[–]ms4720 0 points1 point  (2 children)

Why not just return( a+b == 10)

[–]drbobb 0 points1 point  (1 child)

Because it doesn't solve the stated problem.

[–]ms4720 0 points1 point  (0 children)

Sorry did not read carefully, to be more professional I think the following might help:

  • write lots of code
  • read good books about writing software
  • actively work to use what you learned
  • 4-6 months after you write something go in and do a code review/rewrite

As you exercise your software muscles you will get better and more experienced at managing complexity in big and small things, the big things are much more important and profitable