Earlier this month at TDX Bengaluru, I had the opportunity to speak on a topic close to every Salesforce developer’s heart — optimizing the way we build on the platform. My session, titled “20 Minutes – 20 Best Practices and Tips for LWC and Apex”, was a power-packed walkthrough of essential tips that can help both beginners and experienced developers improve code quality, maintainability, and performance.
In case you missed it or want to revisit the key points, here’s a quick summary of the takeaways.
⚡ Lightning Web Components (LWC) – 10 Best Practices
-
Use Base Components
Use Salesforce-provided base components whenever possible—they're optimized, tested, and customizable. -
Avoid Over-Modularization
Modularization is good, but too much can increase complexity. Always evaluate if breaking down is truly adding value. -
Build Reusable Components
Design your components with reusability in mind. Divide based on logical functionality. -
Follow Kebab-Case Naming
Naming conventions matter. LWC component names should followkebab-case
(e.g.,my-first-component
). -
Render Multiple Templates
Use conditional rendering to switch between templates and maintain code clarity for different UIs. -
Standardized Error Handling
Use reusable error panel components and propagate errors to parent components where applicable. -
Use Lightning Data Service (LDS)
Avoid Apex where possible—use LDS for basic CRUD operations, ensuring field-level security and sharing rules are respected. -
Implement Lazy Loading
Load components or data only when needed to improve performance. -
Communicate via Events
Understand and implement the right pattern—child-to-parent, parent-to-child, or pub-sub—for effective component communication. -
Be Careful with Case Sensitivity
Property names, object fields, and variable references in LWC are case-sensitive. Double-check your references!
💡 Apex – 10 Best Practices
-
Handle Batch Apex Responsibly
Don’t fire new batches if existing ones are running. Monitor usingAsyncApexJob
. -
Control Trigger Recursion
Use static variables wisely and reset them post execution. Pay attention totrigger.old
vs.trigger.new
. -
Bulkify Everything
Always write code that can handle multiple records at once. -
Avoid DML and SOQL Inside Loops
Move DML and SOQL operations outside of loops to avoid hitting governor limits. -
Use
Limits
Methods
Use Salesforce'sLimits
class to stay under governor limits and diagnose issues. -
Use
Database
Class Methods for DML
These offer better control and error handling options compared to simple DML statements. -
Adopt an Error Logging Framework
Implement a centralized error logging strategy to capture and analyze runtime issues effectively. -
Leverage Custom Settings & Metadata
Store config data in Custom Settings or Metadata instead of hardcoding values in Apex. -
Write Quality Test Classes
Test for bulk records, edge cases, restricted users, and avoid usingseeAllData=true
. -
Use Return Early Pattern
Improve method readability and reduce CPU time by returning as soon as your logic is complete.
📎 Get the Full Session Notes
I've made my complete session deck and notes public. You can find them here:
👉 GitHub: TDX Bengaluru Session Notes
🙏 Thank You!
A huge thank you to everyone who attended and packed the room! The questions, energy, and discussions truly made this session memorable. Let’s continue to share and grow together in this amazing ecosystem.
#TDXBengaluru #Salesforce #LWC #Apex #SalesforceDevelopers #TrailblazerCommunity #KapilBatra #DeveloperBestPractices
Checkout Complete Video Below
0 Comments