20 Best Practices and Tips for LWC and Apex – TDX Bengaluru Recap


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

  1. Use Base Components
    Use Salesforce-provided base components whenever possible—they're optimized, tested, and customizable.

  2. Avoid Over-Modularization
    Modularization is good, but too much can increase complexity. Always evaluate if breaking down is truly adding value.

  3. Build Reusable Components
    Design your components with reusability in mind. Divide based on logical functionality.

  4. Follow Kebab-Case Naming
    Naming conventions matter. LWC component names should follow kebab-case (e.g., my-first-component).

  5. Render Multiple Templates
    Use conditional rendering to switch between templates and maintain code clarity for different UIs.

  6. Standardized Error Handling
    Use reusable error panel components and propagate errors to parent components where applicable.

  7. Use Lightning Data Service (LDS)
    Avoid Apex where possible—use LDS for basic CRUD operations, ensuring field-level security and sharing rules are respected.

  8. Implement Lazy Loading
    Load components or data only when needed to improve performance.

  9. Communicate via Events
    Understand and implement the right pattern—child-to-parent, parent-to-child, or pub-sub—for effective component communication.

  10. 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

  1. Handle Batch Apex Responsibly
    Don’t fire new batches if existing ones are running. Monitor using AsyncApexJob.

  2. Control Trigger Recursion
    Use static variables wisely and reset them post execution. Pay attention to trigger.old vs. trigger.new.

  3. Bulkify Everything
    Always write code that can handle multiple records at once.

  4. Avoid DML and SOQL Inside Loops
    Move DML and SOQL operations outside of loops to avoid hitting governor limits.

  5. Use Limits Methods
    Use Salesforce's Limits class to stay under governor limits and diagnose issues.

  6. Use Database Class Methods for DML
    These offer better control and error handling options compared to simple DML statements.

  7. Adopt an Error Logging Framework
    Implement a centralized error logging strategy to capture and analyze runtime issues effectively.

  8. Leverage Custom Settings & Metadata
    Store config data in Custom Settings or Metadata instead of hardcoding values in Apex.

  9. Write Quality Test Classes
    Test for bulk records, edge cases, restricted users, and avoid using seeAllData=true.

  10. 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


 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments