Skip to content

Instantly share code, notes, and snippets.

@ninadhatkar
Last active September 17, 2024 05:41
Show Gist options
  • Save ninadhatkar/12484b2b4546c8e28a794b785e993958 to your computer and use it in GitHub Desktop.
Save ninadhatkar/12484b2b4546c8e28a794b785e993958 to your computer and use it in GitHub Desktop.

Functions:

Side Effects

  • Side Effects are lies, promises one thing and does other hidden things unexpected changes to variable of its own class

Output Arguments

  • Avoid using output arguments

Command Query Separation

  • Function should either do something or ans something, but not both

Prefer exception to returning error code

  • Returning error code and then handling the exception in function is not good idea when required immediate action

    if(deleteFile() === "OK") {
        if(deleteReference() == "OK")
    	    if(deleteAfterMath() == "OK")
       } else {
           console.log("Failed to delete")
       }
     //Instead
     try {
         deleteFile()
         deleteReference()
         deleteAfterMath()
     } catch (exception) {
         console.log("Failed to delete")
     }

Extract try catch blocks

Error Handling is one thing

  • If try exists in function, it should be very first word in the function and that there should be nothing after the catch/finally blocks

The Error.java dependency magnet

Conclusion

  • Functions are verbs to the and classes are nouns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment