Hi David,
I am new to your blog and have really enjoyed taking my knowledge beyond the declarative skill set I used to have.
As we grow to learn how to code in Apex, without a pure coding background, I am ever mindful that the code I am creating performs correctly, efficiently and wont be laughed out of town if ever viewed by seasoned coder.
Therefore what are your top tips for coding best practice to ensure the code looks and performs just right?
Thanks a lot,
Best Practice Bob
———————-
Dear Best Practice Bob,
This is the type of question that many great coders ask themselves every day! A good coder always asks him or herself how they can write better, more efficient, scalable code.
A few tips here for you:
1. Read a lot of code! The key here is recognizing the patterns that are riddled in almost every block of code you’ll find. Luckily, there are tons of Apex code samples across StackExchange, YouTube, AppExchange un-managed packages, Github, etc. Keep an open mind!
2. Minimize lines of code. This can be a good indication of how simple and understandable your code is, especially for newer coders. There are tons of objects/functions in Apex that’ll make your life easier – most people just aren’t aware of them. There’s too many to teach however so a good coder will proactively discover these tools just like an Excel user might search to see if there’s a Standard Deviation function before attempting to calculate it manually.
3. Write a lot of comments in your code! Many lines of code dedicated to comments is a good thing! The truth is, 10 master programmers might all code a simple program very differently. As long as the next person reading your code doesn’t get out of this world confused, your code is likely good enough. Comments will go a long way to helping others understand what you’re trying to do, and worst case scenario, your comments will make it easier for others to give you tips!
4. Google everything! Most people don’t realize that ~70% of code that a great coder writes is plagiarized from the internet. Why reinvent the wheel when someone else has already written it better and put it up online! Make sure to brush up on advanced Google search methods too!
5. Stay tuned for my next post!
Hope this helps!
David
Join the discussion in the comments section below! Got a question? Send it to the mailbag: sfdc99.mailbag@gmail.com
The Clean Code book is a good read on best practices but unfortunately it goes against David’s third tip.
Different strokes for different folks!
Ideally your code is so clean that it speaks for itself, but for beginners that’s rarely the case!
Really Nice explanation for best Coding practices—Mr.David
Thanks David for responding with some easy to digest starting points. Salesforce themselves have produced two useful Apex best practice pages, although they may be leaning towards the more advanced level of coding. Still useful to read through though.
– The 15 Apex Commandments – https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html
– Apex Code best practice – https://developer.salesforce.com/page/Apex_Code_Best_Practices
Regards,
Allezcorp
Great resources!
Hi David
About point 3. I believe you mean doing that only if code is not understandable? I think this approach should be balanced with proper using of naming conventions for variables, methods, classes.
It is great to have you back !!!
Yup! Use comments to explain your general plan of attack – the very basic stuff is self explanatory.
Although I do think it’s safe to assume your code is not understandable in most scenarios!
“plagiarism is the highest form of complement !!” ~ ThatDarnWoman We have to keep in mind that the goal is to complete the task at hand and if you find some code that works, copy it and move on. There are no gold starts or badges for working long hours reinventing what has already been created.
Gosh, I am so glad your back dear David!!