TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A small guide for naming stuff in front-end code

98 pointsby damirover 3 years ago

14 comments

dvhover 3 years ago
I love when I return to some project after few years, realize I need some new feature, write the function name only to find out that the exactly the same function already exists and does what I want. When I choose the same name after so long it means there is some serious consistency in my naming conventions. Feels good.
评论 #29920950 未加载
stickfigureover 3 years ago
I have one frontend naming habit that might be worth sharing. The root element of my react components always have a className that matches the component name exactly (ie capitalized). All other classNames are uncapitalized. Makes navigation pretty easy in browser dev tools.
评论 #29920408 未加载
评论 #29919058 未加载
评论 #29918843 未加载
评论 #29918367 未加载
seanwilsonover 3 years ago
&gt; If you’re the first person on the project, use camelCase; it’s easier to select camelCased text in an IDE than it is hyphenated text<p>Doesn&#x27;t this go against the conventions of the most popular CSS libraries that your code will likely be used with? It&#x27;s a pain and ugly that JavaScript and CSS don&#x27;t use the same conventions though.<p>The IDE select issue is probably fixable e.g. <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;45590695&#x2F;visual-studio-select-word-with-hyphen" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;45590695&#x2F;visual-studio-s...</a>
评论 #29933264 未加载
评论 #29923945 未加载
llimosover 3 years ago
Agree with most of this, but I think using &quot;i&quot; for the iteration index is ubiquitous enough that everyone knows it (or should learn it).
评论 #29919944 未加载
Vinnlover 3 years ago
&gt; Does a Test need it?<p>&gt; Use .qa<p>Generally, I&#x27;d recommend trying to query elements in a test using accessible selectors, if possible. For example, look for the input[type=email] or button[aria-label~=&quot;Submit&quot;]. That helps prevent people from forgetting to update or accidentally removing these often invisible properties.<p>I&#x27;d especially recommend testing-library for this purpose, which allows you to easily query by e.g. role and the accessible name of an element: <a href="https:&#x2F;&#x2F;testing-library.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;testing-library.com&#x2F;</a>
评论 #29933136 未加载
评论 #29920831 未加载
graupover 3 years ago
The general ideas are fine, but the examples are pretty outdated. For modern SPA, CSS naming has become largely obsolete. A lot of the JS code shows the principles it is supposed to well but sadly includes other cruft.<p>Here&#x27;s a guide I would recommend instead: <a href="https:&#x2F;&#x2F;github.com&#x2F;labs42io&#x2F;clean-code-typescript" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;labs42io&#x2F;clean-code-typescript</a>
评论 #29917819 未加载
评论 #29919032 未加载
评论 #29920893 未加载
评论 #29919321 未加载
评论 #29918030 未加载
mrblampoover 3 years ago
Interesting but a bit short on reasons. For example, why is it important for class names used by JS to find elements have a special prefix?
评论 #29918328 未加载
评论 #29919093 未加载
评论 #29918442 未加载
not2bover 3 years ago
Mostly good but I hate getProperty names. If a method returns the temperature call it temperature() not getTemperature(). If the private data member has to have a different name, _temperature or some similar convention will do.
评论 #29917007 未加载
评论 #29918200 未加载
评论 #29918967 未加载
isleyaardvarkover 3 years ago
For js and qa needs I’d recommend custom html data attributes instead of using classes (data-js=, data-qa=). It’s a much clearer separation of concerns.
评论 #29933083 未加载
SavantIdiotover 3 years ago
Packagers solve the caching issue. Despite their complexity and annoyances, it&#x27;s hard to go back now.
kenjackzover 3 years ago
It is easier to put and name the functionalities in a same category
reidjsover 3 years ago
People still think about naming their CSS classes? I thought we were all on Vue or Styled Components in React and tailwind?
评论 #29916661 未加载
JamesAdirover 3 years ago
any thing similar for back end code - mainly databases and such?
toastalover 3 years ago
&gt; Functions that do things (don’t have a return)<p>These aren’t functions; these are effects.
评论 #29918355 未加载