Avoiding Common Pitfalls in Frontend Development: Insights Unveiled
Written on
Chapter 1: Recognizing Bad Code
Understanding what constitutes poor coding practices is essential for crafting quality code.
Today, I found myself quite frustrated with a senior colleague who has seven years of experience in frontend development. The code he produced was shockingly substandard, completely ignoring established coding conventions. This negligence forced our team to invest significant extra time and effort during maintenance.
My frustration culminated in a confrontation about the obvious flaws in his code. In the process, I decided to document my observations in this article.
Section 1.1: Inconsistent File Naming Conventions
Within a single feature, authored by the same individual, I encountered three different naming conventions scattered across various folders. The third method was particularly puzzling—its purpose was unclear, leading me to reassess its logic each time I accessed the code.
Subsection 1.1.1: Confusing Component Responsibilities
Returning to the aforementioned component, the "components" directory should ideally house various components. Yet, a "RecordDialog.jsx" strangely sits at the root level. What justifies such a filename in a components folder?
Section 1.2: Improper Conditional Rendering Placement
Despite the presence of multiple rendering functions tailored to customer needs, the logic should be positioned outside these functions, closely linked to the function calls. This setup allows for various rendering functions to be invoked based on conditions, enhancing code cohesion and minimizing scattered logic—aligning with the principles of high cohesion and low coupling.
Chapter 2: Common Code Issues
This video explores essential knowledge every frontend developer should possess, shedding light on fundamental coding principles.
The code is plagued by excessive amounts of commented-out sections. While I attempt to remove them with every new requirement, this task grows more arduous across numerous projects.
Section 2.1: CSS Chaos
The CSS is a mess:
- Unsightly: Lacks proper spacing, line breaks, or indentation.
- Problematic: Contains numerous CSS rules that override the original styles from the component library, failing to use appropriate class names as namespaces.
- Redundant: An extensive amount of CSS filled with commented-out sections and class names that lack corresponding styles.
- Chaotic: A disorganized mix of global, local, and component-specific CSS imports, leading to overly coupled styles.
Section 2.2: Overly Complex Code Structure
The complexity of chained value retrieval and assignment speaks for itself—additional commentary seems unnecessary.
Unreasonable or absent code splitting has led to excessively long files. The original codebase alone features nearly 2000 lines. Although subsequent maintainers have added new code, the foundational code remains overwhelming.
This file encapsulates the data flow of a particular feature. Even with Dva's management, the codebase is still smaller than what Redux would entail. However, the code is still overly consolidated in one file, with few daring enough to break it apart.
This video discusses how much HTML, CSS, and JavaScript is sufficient, setting realistic expectations for frontend development.
Prior to my involvement, the project was cluttered with unnecessary Markdown documents, unused JavaScript files, and an abundance of test HTML files. Organizing these into a dedicated folder provided a much-needed sense of clarity.
Section 2.3: Mixed File Types
A bizarre combination of Less and SCSS adds to the confusion.
Section 2.4: Peculiar Variable Renaming
One individual, a true expert responsible for the project's backbone, showcases impressive contributions with minimal bugs. Yet, this expert has a peculiar habit—renaming the 'window' object to 'G'. While this isn't a major flaw, I strongly advise against this practice due to the special significance of the 'window' object.
const G = window;
const doc = G.document;
Section 2.5: Chaotic Import Practices
Import statements should follow a conventional order: third-party libraries first, followed by internal component libraries, utilities, helper functions, and styles last. Disorderly imports are visually unappealing, and the unconventional practice of importing components directly from the 'lib' folder is equally troubling.
In Conclusion
This critique underscores common mistakes that can easily occur during development. Pay attention and avoid falling into these traps. To maintain healthy coding habits, remember that your code will be reviewed by others. If you wish to steer clear of criticism, aim for clarity and cleanliness.
In my opinion, grasping algorithms and design patterns is secondary; what truly matters is crafting clear code with coherent data flow, well-chosen variable names, and adherence to 80% of best practices.
Ultimately, success hinges on the individual. I've also penned an article on how to create clear and concise code—an invaluable lesson for both newcomers and seasoned developers alike.
Thank you for reading through to the end. I encourage you to follow the writer and this publication. Visit Stackademic to learn more about our mission to democratize programming education globally.