Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, July 6, 2017

What is functional programming?

Functional programming (FP)is one of the programming style like procedural or object oriented programming. It will follow Declarative programming paradigm in which, programming will be done using expressions instead of statements. Where as Imperative programming language uses statements(e.g C, Java). Below are the some of the functional programing concepts.
  • Pure functions
  • No Side effects
  • No shared state
  • No Mutating state
  • Function composition
Pure Functions: A pure function is a function which will return always same value for the same input. The result of a pure function is always depends on input values and its internal logic. Pure function will not read/write anything from outside of that function. A pure function will eliminate side effects and maintains referential transparency. A pure function can be referential transparency if function call can be replaced with its result. Below are some of the examples.
  • 'chandu'.length is always 6, so its pure function. And instead of 'chandu'.length, you can use 6 as well, so this is also a referential transparency.
  • Max(3,7) is always 7, so Max is a pure function
  • sqrt(9) is always 3, so its a pure function.
No Side effects: As stated in pure functions, there will not be any out side change due to a particular function call. Should avoid shared variables, global variables to eliminate side effects.

No Shared State: Shared state is sharing variable or object or memory location. In functional programming there will not be any shared variable or object. If shared state is there, it violates pure function definition as some other function may change shared object. If we eliminate shared state, there wont be any change in the order of the function calls as well.

No Mutating state: A mutable object can be modifiable after its creation. In functional program, modification of an object is not allowed after its creation. So that there wont be any side effects. SO Immutability is the another feature of functional programming.

Function composition: Composition is a combination of two or more functions to make a new function. As stated earlier Functional programming is declarative language, so it uses expressions instead of statements. This function composition is very useful to make expressions using functions.

Functional programming languages are used mostly on mathematical calculations and pattern matching and AI. Haskell and Lisp are some of the FP languages. JavaScript and Python will follow some of the FP features.

Happy Learning!! References

Thursday, December 1, 2016

How to write safe and secure code!!!

Coding is very easy task to do, but writing safe and secure code is difficult. In this post I will try to explain some of the rules/steps to make safe and secure code. If we follow these steps we can eliminate most of the failures in our software. As a coder, we need to find out all possible failure cases first and handle them. Some where I read statement like "A developer is like a cab driver in India who sees both sides in one way road". I think this statement is very true. In software application anything can happen, no software is secure and any software can crash at any time due to some simple mistake in the code. So to avoid all simple and silly mistakes and making more safe and secure code, below are the rules with no specific order you need to follow without fail.

  • Static Code Analysis(Static analyzer)
  • Test Driven Development(TDD)
  • Code Review
  • Pair Programming

Static Code Analysis: Basically what static analysis will do is, it just scans our code and find out possible errors. There could be some copy-paste errors, some human errors etc. All these can be identified by Static analyzers. Static analysis done by static analyzer which is simply another software which can scan our code and generate report with all errors and warnings.There are lot of open source and commercial static analyzers available on the web. Here are some of simple errors which identified by static analyzer.

Test Driven Development(TDD): In TDD, first instead of writing code for functionality, need to write all possible test cases for that functionality. After finishing all test cases, run those test cases once. All these test cases will fail as there is no code available for the functionality. Now start writing the code to pass all these test cases. Believe me, It helps a lot in eliminating most of the bugs in the initial stage. Writing test cases for existing code is difficult. So always start tests cases before writing actual functionality. It will take some extra time, but it helps a lot.

Code Review: This is one of the traditional way of finding out silly mistake done by developers. Always make sure that your code is reviewed by some one. Some others reviewing your code doesn't mean that you are not good in coding, it eliminates if any mistakes and it boasts your confidence levels if there are no comments :-) So Always go for the code review and don't skip it.

Pair Programming: This is another new way of coding. Most of the developers thinks that if they are alone, they can write code quickly and efficiently. Yes that is true. But occasionally do pair programming. If possible code with new developer and some times with senior developer. While doing pair programming, basically two developers are seeing that code and two brains are working right!!. In this case, if any mistakes done, another developer identifies it and s/he may give another better way of writing the same code.

Till now I have not specified any secure programming techniques rite? If you follow above rules, you can easily eliminate lot of common security related issues. All these steps are not specific to any particular programming language. In whatever language you are going to write, always follow these steps. Nowadays we have lot of IDE's (like XCode, Eclipse) which are supporting inbuilt frameworks to support static analyzers and TDD.

Enjoy Coding!!!
Happy Coding!!!


References:
Click
Subscribe to: Comments (Atom)

Popular Posts

AltStyle によって変換されたページ (->オリジナル) /