Friday 29 April 2016

Haskell: Naming, coding conventions

a. Use Camel case for function and variable names.
Ex:
employeeAge, placeOfBirth, stateOfProvince etc.,

b. Since there is no limit on number of characters in variable, function names, use meaningful names.
Ex:
principle, rateOfInterest

c. Don’t use tab characters, Haskell cry :) at the time of loading.
For Ex, in following program, I used tabs instead of space.

sample.hs
add a b c
 | (a > 0) = (a+b+c)
 | (b > 0) = (a+b-c)
 | otherwise = a*b*c


When you try to load above program, you will get following warnings.
*Main> :load sample.hs
[1 of 1] Compiling Main             ( sample.hs, interpreted )

sample.hs:2:1: Warning: Tab character

sample.hs:3:1: Warning: Tab character

sample.hs:4:1: Warning: Tab character
Ok, modules loaded: Main.

d. Try to keep maximum 80 characters per line, By following this rule any one can read your code easily.

e. Always give type signatures to function, variables.

For example,

sample.hs

add :: Int -> Int -> Int -> Int
add a b c
    | (a > 0) = (a+b+c)
    | (b > 0) = (a+b-c)
    | otherwise = a*b*c


add :: Int -> Int -> Int -> Int

Above signature tells that, add function takes 3 arguments of type Int and return result of type Int. Type signatures enhance documentation.

f. If your function name is not meaning full (or) requires some information to understand, always provide comments to that function. Same is applicable to variables also.

g. Always divide the complex function into small sub functions. Use these sub functions to solve complex function.

h. Type names always start with a capital letter, where as variable names always starts with lower case letter. This is not a convention, Compulsory rule enforced by Haskell.


Previous                                                 Next                                                 Home

No comments:

Post a Comment