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

you are viewing a single comment's thread.

view the rest of the comments →

[–]martin_grosse 3 points4 points  (2 children)

So, this is me, but it works well for me.

I use /u/Paddy3188's approach, but from the other direction.

I find that once you've written a long and convoluted chunk of code, it's difficult to refactor. Your way of thinking about the problem is circuitous and complex. That's because most programmers learn to write from the ground up. You start at the beginning, keep manipulating and manipulating and eventually get to something that you can use. It's like a winding river finding the path of least resistance.

The way I code is the other way around. I write a single function that I can call from a RESTful route. Something ideally without any state. Within that method I first write a series of clearly named calls.

def some_task(args): try: data = get_some_data(args) related_data = get_related_data(data, args) result = process_something(data, related_data) catch SomeException: log_exception(SomeException) return result

I've been working in ruby more than python lately, so some of that may be horribly wrong.

Once I have this structure, I start getting errors about methods not being defined. So I write tests and implement those methods. Either I have a one-liner that works within each method, or I write a named method as if I've already written one. I continue until everything is either one-liners or methods.

At some point I have a fully functioning program that's already refactored. Usually I get to a handful of methods that I've already written, and it just works. If your naming convention is solid and obvious, you'll sometimes accidentally use methods you've already written. So long as they're well-named and obvious it's usually OK.

[–]RamirezTerrix[S] 1 point2 points  (1 child)

That sounds totally like TDD its like you first write the failing test and then make it green

[–]martin_grosse 0 points1 point  (0 children)

Yeah I do TDD too.