-
Notifications
You must be signed in to change notification settings - Fork 6
Naming Conventions
This is a collection of naming conventions used by the Sunroof project.
Whan writing function in Sunroof the following naming conventions should comply:
-
No Prefix/Suffix: General combinators and API methods that return a
JS t a
do not get a suffix or prefix.Examples:
apply
,function
,new
; -
Suffix
JS
: Everything that takes aJS t a
as argument has the suffixJS
.Examples:
forkJS
,sunroofCompileJS
; -
Prefix
JS
: Types that directly represent Javascript values (generaly all that implement Sunroof) should haveJS
as prefix.Examples:
JSBool
,JSString
,JSDate
; -
Prefix
Sunroof
: Classes that are tightly connected with Sunroof should have the prefixSunroof
.Examples:
SunroofArgument
,SunroofThread
;
When providing bindings for Javascript APIs use the following naming conventions:
-
General Rule: In general name the API functions and bindings exactly as the functions or objects are named in Javascript. Also be as specific about the types as possible in Sunroof without reducing the usability (there is always the possibility to cast values, but this should not happen all over the place).
Example:
document
becomesdocument :: JSObject
-
Group Arguments: Methods in Javascript may take many arguments. If there is a logical way to group them (e.g. coordinates as a tuple) do so. This will improve the usability of the API.
Example:
fillText(t,x,y)
becomesfillText :: JSString -> (JSNumber, JSNumber) -> JS t ()
(example from Canvas API) -
Getters & Setters: Often Javascript objects provide a function
o.foo()
as getter ando.foo(v)
as setter for a certain valuefoo
ofo
. In the Haskell version of the API create afoo
function for the getter and asetFoo
function for the setter.Example:
html([htmlString])
becomeshtml :: JSObject -> JS t JSString
andsetHtml :: JSString -> JSObject -> JS t JSObject
(example from jQuery) -
Optional Arguments: Javascript functions allow for optional parameters. In the Haskell version of the API there are two ways to solve this:
- If the Javascript function is very flexible (i.e. no constraints on the types and number of parameters)
directly expose the arguments as a
SunroofArgument
. - If the Javascript function is not flexible (i.e. arguments are constrained in type and number) create a version that offers all parameters and one with only the most commonly used parameters. The version with all parameters has a prime at the end of its name.
Example:
new Array(...)
becomesnewArray :: (SunroofArgument args, Sunroof a) => args -> JS t (JSArray a)
Example:
fillText(t,x,y[,m])
becomesfillText :: JSString -> (JSNumber, JSNumber) -> JSCanvas -> JS t ()
andfillText' :: JSString -> (JSNumber, JSNumber) -> JSNumber -> JSCanvas -> JS t ()
(example from Canvas API) - If the Javascript function is very flexible (i.e. no constraints on the types and number of parameters)
directly expose the arguments as a
-
Keywords: If the name of a object or function collides with a keyword or common function in Haskell, append a prime to the name.
Example:
data
becomesdata' :: JSSelector JSObject
(example from Canvas API) -
Higher Order Functions: If a function or method takes a function or continuation as argument, the parameter is given as a Haskell function, not as
JSFunction
orJSContinuation
.Example:
setTimeout :: (() -> JSB ()) -> JSNumber -> JSObject -> JS t JSNumber
instead ofsetTimeout :: JSContinuation () -> JSNumber -> JSObject -> JS t JSNumber
(example from JavaScript API)