We all want to write clean code.
I was attending Mike Hichwa's session at Kscope18's Sunday symposium, and he revealed numbers about language usage within the frameworks we use.
APEX is made up of over a million lines of JS.
ORDS contains over 300 000 lines of JS.
Tip #1: Keep It Out of APEX
In all my years of APEX development, I found that APEX is best used when the least amount of code is stored in the application itself.
It's common practice to wrap PL/SQL code in packages and SQL into views. Once we have all that business logic in the database, all APEX should be doing is referencing those objects. It's easier to manage and it's easier to version control.
One lazy excuse I hear too often is:
Let’s look at the above. Observations:
- We have a simple button
- It has a dynamic action attached to its onClick event
Then, in page designer we can invoke function
- APEX is best used when the least amount of code is stored in the application
What's the purpose of a PL/SQL package? Giving context to a function or a procedure. So when invoking a PL/SQL API, we would use
Having such a structure in your functions makes your files MUCH cleaner.
- Makes files much cleaner
Tip #3: Use a Modern Code Editor
There are so many good code editors today. I think it's fair to say that the most popular ones are (in no particular order)
Why would you want to use them? Here's an important rundown:
- IntelliSense: is a term referring to a variety of code features like code completion and parameter information.
- Multi-cursor editing: Why make one change at a time when you can make ten? This feature alone should be enough to persuade you to switch.
- Plugins: These editors have rich plugin ecosystems. The editor's out-of-the-box features are just the start. Plugins let you add new languages, code linters, text transformations and way more. Anything you can think of applying to your code, there’s a plugin for that.
- Command palette: Being efficient at keyboard shortcuts is important, but if you can just remember the shortcut for the command palette, then you have every possible action at your fingertips, through a searchable list popup.
forEachor otherwise you can add your own APEX Snippets (
Being comfortable with a new code editor does take time, but don't be discouraged by the first few days. The productivity benefits are worth it.
- They've got a plugin for everything
- Command palette: one shortcut to access all available actions
- Custom code snippets (your own snippets, APEX snippets perhaps?)
Tip #4: Use a Style Linter
I am using XO, and it mercilessly tells me where my coding style is inconsistent. We should not spend any time developing and maintaining our own coding style. Let's hand that over to linters.
To use XO in my project directory, I would simply do:
Or even better, if I want XO to fix my code automatically, I can do:
Needless to say, there is more to writing clean code than these 4 tips, so this blog post is subject to be updated in the future.