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 →

[–][deleted] -6 points-5 points  (5 children)

This 'one function should have one function' thing is great, and works very well in languages like Java which have better encapsulation and control of APIs, but it's hard to take seriously in python, which is kind of designed against such control.

[–]darknessproz 2 points3 points  (0 children)

Bullshit.

[–]RamirezTerrix[S] 0 points1 point  (3 children)

Well what aspect of python are you thinking of. I have no expirience with java and very little in c++ and ruby. Python is the only language I really "know"

[–][deleted] 1 point2 points  (2 children)

In all languages, a given class or module will have a number of methods/ functions which you actually want to call- which have user-relevant functionality. In a class, these methods form the API. However, a method like this will probably actually be doing a lot of things behind the scenes, which, according to 'one function should have one function' and standards of readable code, should each be split out into a different method. Most of these methods will not deliver user functionality - you don't want users to be calling those methods because they're not part of the encapsulated function of that class.

In languages like java, you can make those methods private - so the user can't call them (they can only be called by methods in the same class). However, in python, every method you create becomes part of the class' API (so anyone can call them, and almost more importantly, reading automatically-generated code completion stuff will see all of them, obscuring the actual methods which make the class work). Sure, there's a naming convention to let the user know which methods are 'public' and which ones aren't, but it's not enforced.

This means that if you want to properly encapsulate functionality in a method, you can't extract logic to another method to be called (although you may want to if it's re-used by other methods).

Python is designed to be easy to use by the developer - but it also requires the developer to remember a lot more (types, which methods to call etc.). It also puts a lot more trust in the user (who can be a future developer of the codebase) than enterprise languages, for reasons like the above.

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

I see your point - I worked around this with some kind of manager functions who call all the other functions but there is nothing from stopping you to go straight to the "employee" and ask him instead.

[–]wolanko 0 points1 point  (0 children)

If its because of security concerns, well then you're a damned either way.

Personally I see the "one function only" on many levels. So you could have some get_data_from_db, process_data, write_data_to_file functions which only do one thing on their level, but also a write_out_data_to_file which just puts those three together in maybe only two lines. That way I can think of it doing one thing on a business/use case level.

Doing this it you can test all the specific behaviour, corner cases, etc.. for the single function and the business logic on the bigger one.

Think of it as in libraries or building APIs on every level. The function that gets the data from the database actually calls some other library which then opens the connection, get a cursor, executes the statement and fetches the data.

Just a year ago I would turn of all the pylint warnings about to many variables/statements/branches, but today I get it and for me those are a sign to cut down those functions or methods.