We will adhere to the rules described below when writing code in CSE 331. These rules should help prevent mistakes when learning JavaScript. They are not intended to represent the style used most often in industry or elsewhere.
If you write code like you've seen in class, your code should meet our code quality expectations as our code satisfies these conventions. (And if you do notice any mistakes in our code aligning with these conventions, let us know, so we can get it fixed!) If you choose to use features/methods/etc. outside of course content, make sure they follow these conventions as you could receive deductions (variable in severity), otherwise.
The comfy-tslint linter enforces a set of conventions that includes some of this list as well as other minor rules. You should use both this guide and linter warnings to inform you of our conventions as a whole.
any
type.bigint
(not number
) for values that are always integers.obj['name']
syntax. Use obj.name
instead.obj.name
notation better indicates that "name" is an attribute of the
"obj" record. The obj['name']
notation implies that you're trying to access
an element of "obj", which is not the case. If you want to represent a
collection of (key, value) pair relationships, a Map
is more appropriate.tuple[0]
. Use a destructuring assignment to
access elements with a variable name: const [a, b] = tuple;
.var
keyword. Use let
or const
instead.const
.==
or !=
. Use ===
and !==
instead.if
statements should evaluate to a boolean
.if(x !== undefined)
instead of if(x)
)undefined
, null
, NaN
, etc. as conditions.&&
should only ever be used between booleans in a conditional. Do not use
&&
between a boolean and some operation or result to replace an if
statement.function
keyword. Declare a function with
const f = (..) => { .. }
syntax instead.{}
to contain the contents (() => { ... }
), or write a 1-liner
that exceeds 100 characters, create a named function instead.map
, filter
, forEach
, and find
should only be used only in simple cases. If the
function you provide is more than 1 line, meaning you need to use {}
to
contain the contents (map(() => { ... })
or passing a reference to a named
function), you should write your own loop.package.json
).f(..) { .. }
syntax. Use f = (..) => { .. }
syntax instead..prototype
.console.log
s.//
) comments when they're helpful. Generally
these should be brief. Below are guiding ideas for what is "helpful" though
you're welcome to add more as long as they don't clutter your code.for (const item of arr) {}
that says "Iterate
through every item of the array" is not helpful. The code syntax itself
tells a reader the exact same thing.More on comments to come...
constructor
or render
. If a request
is required on component start-up, use componentDidMount
instead.Avoid a cluttered render
method.
State update conventions:
render
. State updates trigger
a rerender, which then triggers the state to update again, causing an
infinite cycle.setState
s in a method. setState
is not
immediate, so this may cause bugs, and makes it less clear how a
method effects the state. setState
.setState
.Do not use React hooks. We teach the class-based components approach because it's fundamental and (believe it or not) easier to debug in many cases because state is more explicit.
window.location.reload
, document.getElementById
,
element.innerHTML
, etc.bind
.string
error message with a 40X
status code. Make sure error messages are descriptive.200
(default) status
code sent using .json
.(If you follow the fetch function structure shown in class, you will meet these requirements and your code will be well organized and readable.)
fetch
, json
, text
) you create should have a catch
handler. catch
, log out a descriptive error message.Consistent, descriptive naming improves readability and removes the need for some comments because they can convey what a function/variable does immediately. We expect you to generally follow these conventions, but if your names add detail, that's likely okay.
Event handler and callback naming ("X" being a name/descriptor for the HTML element that fires the event and "Y" being the type of action ("click" or "change")):
onY
". onChange
or onClick
as in standard
HTML components.doXY
".Fetch request naming ("X" being the name of the endpoint accessed.):
doXResp
".doXJson
".doXError
".