XNSIO
  About   Slides   Home  

 
Managed Chaos
Naresh Jain's Random Thoughts on Software Development and Adventure Sports
     
`
 
RSS Feed
Recent Thoughts
Tags
Recent Comments

Functions Aren’t Procedures

Most people seem to use these 2 terms interchangeably. The key difference between the two is that functions are Referentially Transparent. Which means I can safely replace the return value of the function with its method call.

Why is this important? This is very important because, functions by definition are safe and have no side-effects. No matter how many times you call that function, in any random order, its always guaranteed to return the same value. While procedures typically update the state of a class/global scope variable or interacts with an external sub-system. Hence they have side-effects which might not be very obvious by looking at the procedure’s definition.

Functional Interfaces make your code very readable and removes any order dependencies and related complexity. Also from a testing point of view and from a decoupling point of view, functions are always preferred over procedures.

One of the visual cues you can adopt to differentiate between functions and procedures is to have functions with a well defined return type, but procedures won’t have a return type. For Ex: In Java, if the return type of a method is “void”, then its a procedure.

Interesting when you have visual cues, most of your methods become functions and you have a very small set of methods (procedures) with side-effects. And procedures are very clearly highlighted and separated out.


    Licensed under
Creative Commons License