Is it good practice to use functions just to centralize common code?

Posted by EpsilonVector on Programmers See other posts from Programmers or by EpsilonVector
Published on 2011-11-17T09:24:22Z Indexed on 2011/11/17 10:11 UTC
Read the original article Hit count: 310

I run across this problem a lot. For example, I currently write a read function and a write function, and they both check if buf is a NULL pointer and that the mode variable is within certain boundaries.

This is code duplication. This can be solved by moving it into its own function. But should I? This will be a pretty anemic function (doesn't do much), rather localized (so not general purpose), and doesn't stand well on its own (can't figure out what you need it for unless you see where it is used). Another option is to use a macro, but I want to talk about functions in this post.

So, should you use a function for something like this? What are the pros and cons?

© Programmers or respective owner

Related posts about best-practices

Related posts about coding-style