Some thoughts on Angular Style Guides.
I read one from one of my new favorite software bloggers, Todd Motto! toddmotto.com .
Also I read this much esteemed gentleman John Papa’s Angular Style Guide.
Lastly, we cannot ignore the mad scientists at Google that created Angular and their OG style guide.
Start at the Start
From the Google styleguide, the suggestions are (not at all comprehensively listed) as follows:
- Main application module should be in root
- Only alter modules where they are defined
- Keep modules consistent (in case you reuse it elsewhere) so define them in the same file as their component pieces so they can travel as a discrete, autonomous (dependency-free) unit
- Methods should be defined in controllers.
- In the Template, reference the controller with the
as controllerNameconvention clears up confusion when using multiple controllers in a template.
- Don’t name your variables with a leading ‘$’
Call Me Big Papa
For extra credit check out his course on Angular JS patterns.
Before hopping into another incomplete but somewhat accurate list, I just want future biographers to note that I really enjoyed reading the Angular 2 guide. It has a lot of good, practical advice for writing code in general.
- Define 1 component per file, ideally < 400 lines of code
- Keep functions small, idealling < 75 lines of code
- Avoid naming collisions with unique names and descriptive folder names
- Use a consistent sequencing of components - properties, public functions, private functions. Each is alphabetized by name within its group.
- Put logic in services and not a component
- Use factories to make reusable components that are put into views
- Services should have a single responsibility.
- Names should follow a pattern that describes the feature then the type - so,
- Dashes should separate words and dots separate the name from the type
- Add suffix of spec to a test that matches the thing it is testing - so,
- Components should have the name
Componentattached to the suffix - ex.
- Services ought to use CamelCase and end in Service -ex.
- LIFT Principle should guide you - as if you can do the following:
- Be able to Locate your code quickly
- Identify your code at a glance
- Keep the Flattest structure possible
- Try to stay DRY
- Keep 3rd party code in a folder in root separate from code you write
- If a folder has more than 7 files, consider making a new folder or subfolder
- Keep route configuration in a separate file
Thats the Motto Yolo
Todd Motto has a great style guide as well with some very helpful code samples. I like his writing style, also, which is more rare than the meat of fresh roadkill. One might say his direct, clear style is the opposite of my meandering, dad-joke-full style of overlong sentences peppered with non sequitors.
There is overlap here with Mr. Papa, but nevertheless some of the style concepts include:
- Use the getter syntax for declaring modules
- Wrap your module functions in an IIFE - Immediately Invoked Function Expression
- Be sure to use the ControllerAs syntax with controllers
- Avoid logic in Controllers - push to services and/or factories.
- DOM manipulation should only occur inside a directive.
- Factory-made Objects should have the same name as the Factory
- Services should be class-like, utilizing the
- Do not name services with the
- Directives must be camelCased
- One role, one file. Keep everything encapsulated and bite-sized.
- Dont use $ or $$ to start a variable or other custom name
I am just beginning my Angular journey and these posts are far more for me than for you. I think the newest thing to get used to is the getter syntax and being more strict about keeping functions short and making more files. For whatever reason I thought there was a performance price to be paid there but none of these far, far more accomplished and knowledgeable people seem to give a care.