These common functions solve easy programming problems

Functions have a lot of relevance in programming. When developing code you might find these common usage of functions that solve some of your common problems. There is a right way to use functions but in the wild a lot of things are used differently that expected. Once the results and security is still in best practice we should be good to go. Right?

I don’t know how this works?

I don’t know what is inside

You have a piece of code and you wrap it in a function even though you are not sure how it works exactly. Welcome to the I don’t know how it works function.

This function might come to bite you in the but. But for now it will save you a lot of time and energy. Whether it is code from stack overflow or a colleague if you don’t fully understand it you wrap it within a function and ensure you get one output. In truth this works best if there is one input and output. Meaning the code doesn’t interact with your application outside its own scope. You pass some arguments in and you get some data out. You don’t need to understand how this function fully works yet. (yet being the key word) What is great about wrapping foreign code within a function is that you can change this afterwards. You can update it, modify it or totally remove it if you like in the future. This is a much better solution that having it spread throughout your application waiting around to screw you up.

How many things can it do?

A function with to much code origins story

Here we have a function in which we have 1000 lines of code. Alrite that’s exaggerated. But its a lot more than usual. Welcome to the does too many things function.

This is apparently bad design practice but sometimes you can’t make little cute functions for everything. At number two we have the does to many things function. This usually happens when you run out of time to optimize your code. You ran through a business process step by step within one function and didn’t follow up after to optimize it once it works. The function then tends to end up being way longer that 10 lines. But hey it does the job.

Hey what is today’s date?

Convert my date

Sometimes you need to convert to different date formats regardless of what language you use. What you need is a function to deal with it all.

At number 3 we have the date function. How can you use this function. Easy. You can create a utility function or as many functions as you need to format your dates. Why do this? Formatting dates can go wrong quite easily so it’s often best to create your own helper functions(spoiler alert) to do this. There are often a lot of ways this can go wrong so you need your helper functions to walk you through this.

Hold my function? Please 🙂

You need a cup holder for a function or  function for a cup holder. What! 😂 At number four we have the function holder. This function is used as a placeholder for many other functions. Maybe you  have an operation that requires you to call a lot of smaller functions.

This function wraps all these actions under the umbrella of one function. What problem does it solve. Well firstly this probably happens naturally while programming but if you think it doesn’t then I’m just here to tell you it does. Wrap away and have fun with the function holder.

Help me!

Helper functions in a file

The helper function is quite common. Its more a list of functions that does small tasks. This helps with code reuse. You don’t want to be rewrite the same code throughout your application. To solve this you create one file with all your functions and reference that file. Now you access to your helper functions every. What might be added inside this file, well it really is up to you but i’ll list some below:

  • date formatting
  • authentication, user access and permissions
  • logging and auditing
  • general formatting
  • cleaning input

The list can really go on.

Conclusion

Have you seen these uses before. Would you use functions in this way. What do you think? What are some of your common problem solving solutions with the use of functions. Let me know what you think.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s