Whether you are a newbie or a working professional, software design is something that you will have to think about if you want to become a better software developer. There are many parts when designing and writing software, functions is one of the most overlooked. While functions may seem not a big deal at first, there are many details to it that you might not know. Some people abuse functions by using them unnecessarily and some don’t even use them. Creating a function at the right time can improve the maintainability and structure of a program. So here are some of the reasons you should consider before creating a function.

Reducing Duplicate Code

One of the well known reason to create a function is that it reduces duplication of code. Rather than writing 10-15 lines of code at multiple places, we wrap this repetitive code into a function and use the function at multiple places.

For example, lets consider a piece of code that saves an image to a file.

//Saving an image to file File f = new File(context.getCacheDir(), filename); f.createNewFile(); Bitmap bitmap = getImageFromURL(url); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, bos); byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //Saving an image to file File f = new File ( context . getCacheDir ( ) , filename ) ; f . createNewFile ( ) ; Bitmap bitmap = getImageFromURL ( url ) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; bitmap . compress ( CompressFormat . PNG , 0 , bos ) ; byte [ ] bitmapdata = bos . toByteArray ( ) ; FileOutputStream fos = new FileOutputStream ( f ) ; fos . write ( bitmapdata ) ; fos . flush ( ) ; fos . close ( ) ;

You don’t need to understand the details of this code, just get a general idea. What this code is doing is creating a file, requesting image from a server and saving it to the file. Suppose you need to save images at multiple places in your application, you would have to write these lines over and over again. A better approach is to wrap this code into a function.

//Saving an image to file public void storeImage(String filename, String url) { File f = new File(context.getCacheDir(), filename); f.createNewFile(); Bitmap bitmap = getImageFromURL(url); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, bos); byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //Saving an image to file public void storeImage ( String filename , String url ) { File f = new File ( context . getCacheDir ( ) , filename ) ; f . createNewFile ( ) ; Bitmap bitmap = getImageFromURL ( url ) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; bitmap . compress ( CompressFormat . PNG , 0 , bos ) ; byte [ ] bitmapdata = bos . toByteArray ( ) ; FileOutputStream fos = new FileOutputStream ( f ) ; fos . write ( bitmapdata ) ; fos . flush ( ) ; fos . close ( ) ; }

Use the function signature wherever you want to save an image.

storeImage(filename, url); 1 storeImage ( filename , url ) ;

Now there might be times where you may write 10-15 or more lines of code that achieve a certain task, and might not feel the need to put it into a function, well you better make a function for it, by putting that code into a function you are future proofing yourself. So before writing a bunch of lines you should consider whether there are possibilities of the logic being reused somewhere else.

Hiding Code Complexity

By putting code into a function you are reducing complexity, although you have to think about it while writing the function, you don’t have to think when using it. In the previous example to save an image a stream must be opened and closed every time. Putting this into a function relieves you from the headache and you can use the function wherever required with having just a general idea of what the functions does i.e. it saves an image to a file.

Portability

Most of the times there are external software or hardware dependencies in a project and it might be difficult to write code that can be easily portable, but at many occasions there is a possibility of writing functions that are easily portable to other projects or other platforms for that matter. For example, the code above is written in JAVA, specifically for Android platform, it only uses internal constructs of Android and is free from any external libraries. This code can be easily ported to another Android project but cannot be ported to other platforms based on JAVA.

Hiding Order

Hiding the order in which events are being executed can simplify a program. For example, in order to do caching your application might fetch data from database before fetching the latest data from network. You could wrap these events into a function and just use the simplified name fetchPostsAndDisplay().

//A function that fetches posts first from database and then from server function fetchPostsAndDisplay() { Database db = getDatabase("my_database"); Table table = db.getTable("posts"); List<Post> posts = table.getAll(Post.class); updateUI(posts); String url = "http://someurlwithposts.com"; NetworkRequest request = new NetworkRequest(url, "GET"); String data = request.execute(); List<Post> posts = PostConverter.getPostsFromString(data); updateUI(posts); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //A function that fetches posts first from database and then from server function fetchPostsAndDisplay ( ) { Database db = getDatabase ( "my_database" ) ; Table table = db . getTable ( "posts" ) ; List <Post> posts = table . getAll ( Post . class ) ; updateUI ( posts ) ; String url = "http://someurlwithposts.com" ; NetworkRequest request = new NetworkRequest ( url , "GET" ) ; String data = request . execute ( ) ; List <Post> posts = PostConverter . getPostsFromString ( data ) ; updateUI ( posts ) ; }

By no means are these the final reasons from god to create a function, these are just to give you an idea and get started with good software design.

Read Code Complete 2, one of the best books for software developers written by “Steve McConnell”.