Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming approach theory. When to write single-purpose functions?

I am very sorry, but I am unable to define this "thing" that I'm trying to figure out.

When writing functions we can take different approaches, I have made some "placeholder" examples:

--------A---------
getImageSmall();
getImageLarge();
getTextSmall();
getTextLarge();
--------B---------
getImage('small');
getImage('large');
getText('small');
getText('large');
--------C---------
get('image','small');
get('image','large');
get('text','small');
get('text','large');
--------D---------
get(array('type'=>'image','size'=>'small'));
get(array('type'=>'image','size'=>'large'));
get(array('type'=>'text','size'=>'small'));
get(array('type'=>'text','size'=>'large'));
--------E---------
get('{"type":"image","size"=>"small"}');
get('{"type":"image","size"=>"large"}');
get('{"type":"text","size"=>"small"}');
get('{"type":"text","size"=>"large"}');

I could of included objects too, but I prefer to keep it simple for now.

The array in "D" is a php array to show the difference between example "E" that uses json.

You can probably see how the approach gradually shifts from one way of thinking to another, where function definitions turn to information exchange. This does not mean that the "get" function is a master function that does everything, it might be a messenger function that calls other functions, its sole purpose might be to turn the app into a service.

The questions are:

  • What is this shift in coding style called? (main question)
  • When is it best to use which?
  • Where can I read more about this?

Clarification and comments/questions/answers in order to improve this question are welcome.

like image 773
Timo Huovinen Avatar asked Mar 15 '13 19:03

Timo Huovinen


Video Answer


1 Answers

Naming function and passing values to functions has always been one of many topics of discussion. I would recommend you to look at what's commonly called "Clean Code". It contains theory for naming and constructing functions. This is a good book to take a look at

http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

and check YouTube for Clean Code and you will probably find a few good talks.

My thoughts is that you can do a million different ways when constructing functions. You could, in theory, besides your examples have a function called Get that takes a pointer to a function that returns the type of the thing to get. But the only thing this adds is complexity. What we want to achieve is basically self commenting code that is easy for other people to read and understand. For this, every function should follow set rules in regards to how it is named in accordance to what it performs, what it is allowed to change and return etc. This would make for much easier code for someone to get into.

This goes for classes to. Don't have some class that sets up a million things in it's constructor. If that is needed, then create functions to encapsulate the behavior and call these functions from the constructor.

like image 179
inquam Avatar answered Oct 27 '22 03:10

inquam