MSBA-340 · Lesson 11 of 12 · 20 min
Best practices and code reviews
▶ Video coming soon A video version of this lesson is on the way.
By the eleventh clinic Prudence had written enough code to have habits, and Desmond wanted to look at them before they set. He opened her first pull request on the big screen. It contained the warranty table, the calculator, the form, the menu items, the labels and fourteen best-practice warnings. The session went through each warning, then through the review checklist that every Cobalt Ridge pull request would follow, and ended with the unit tests that would have caught the end-date bug from lesson 10.
Best-practice checks
Visual Studio runs best-practice checks as part of the build. The checks are a large set of rules, maintained by Microsoft, that look for problems the compiler accepts but that experience says cause trouble: missing labels, fields without extended data types, forms that break their pattern, missing help text, unused variables, select statements inside loops, and many more. Each finding appears in the error list as a warning with a rule identifier, and many link to documentation. The MB-500 outline asks candidates to resolve best-practice warnings, and Microsoft requires some checks to pass before code can be certified in certain programs.
| Warning in Prudence's pull request | Rule area | Fix |
|---|---|---|
| Label is hard-coded text in validateWrite | Labels | Use @CRCustom:InstallDateInFuture (lesson 9) |
| Field Notes has no extended data type | Data model | Create CRWarrantyNotes EDT based on a memo string type |
| Form pattern errors on the Service history tab | User interface | Move fields into a group bound to a field group (lesson 7) |
| Missing help text on menu item | Labels | Add @CRCustom:PumpWarrantiesHelp |
| Variable declared but never used | Code | Remove it |
| Display method performs a query | Performance | Replace with a joined data source |
| Table has no replacement key | Data model | Set ReplacementKey to SerialIdx |
| Table has no AutoLookup field group fields | Data model | Add SerialNumber, CustAccount and ItemId |
Warnings can be suppressed, either in the project's settings for a rule or in code with an attribute or comment that names the rule and gives a justification. Suppression is sometimes right, for example when a rule cannot apply to a particular generated method. Desmond's policy was that a suppression needs a reason a reviewer would accept, written next to it, and that suppressions are reviewed like code. Prudence's first pull request had none, and it ended with none, because every warning had a real fix.
Best-practice settings let a team choose which rules run and whether some warnings are treated as errors. Cobalt Ridge treated three rule groups as errors in the build pipeline: hard-coded labels, missing extended data types on new fields, and form pattern errors. Everything else was a warning that reviewers had to see addressed or explained.
Naming and structure conventions
Lesson 2 introduced Cobalt Ridge's naming convention. The review checked it on every element. New elements carry the CR prefix; extension elements are named after the extended element with the model suffix; new members added to standard elements, such as fields on a table extension or methods in a class extension, also carry the prefix. Names describe what something is, in the standard style: subject then role, such as CRPumpWarrantyTable, CRPumpWarrantyService and CRWarrantyStatus.
Structure conventions mattered as much as names. Business logic lives in classes, not in forms or tables. Table and form methods are thin and call classes. Classes are internal unless another package needs them. Each class has one responsibility, and long methods are split. Every user-facing string is a label. Every new field uses an EDT. These rules repeat what earlier lessons said, which was Desmond's point: a review checklist should contain nothing new, only the things everyone agreed and might forget under pressure.
Designing for extensibility
Cobalt Ridge's code will one day be extended by someone else: another developer, a partner, or Cobalt Ridge itself in a later phase. Microsoft's own code is written to be extended through chain of command and events, and the same principles make custom code easier to live with. Methods that others may need to change should be small, protected or public, and focused on one decision. Logic that might vary should be behind a method or an interface that can be replaced, as the warranty terms were in lesson 4. Delegates mark the moments when other code might want to react, such as a warranty being registered.
The MB-500 outline asks candidates to assess the extensibility of code and apply design patterns. Desmond's practical test was to ask, for each public or protected method in a pull request: if the mining division needed different behaviour here next year, could they get it by wrapping this one method, without copying a hundred lines? If not, the method was too big or too private.
Code review through pull requests
Cobalt Ridge's code reviews happen in Azure Repos pull requests. A developer pushes a branch, opens a pull request into main, and the pull request runs a build pipeline that compiles the package and runs the best-practice checks and unit tests. At least one other developer must approve, and Desmond reviews every pull request that changes a table, a security element or a standard object extension. Comments are resolved before merging, and the branch is deleted after merge.
The review checklist had ten lines, and Desmond insisted that reviewers work through all ten rather than reading the code for whatever caught their eye.
| Check | Question the reviewer asks |
|---|---|
| Build | Does the pipeline build pass with no new best-practice warnings? |
| Tests | Are there unit tests for new logic, and do they pass? |
| Names | Do new elements and members follow the prefix and naming conventions? |
| Labels | Is every user-facing string a label, with comments for placeholders? |
| Data model | Do new fields use EDTs; are indexes, relations and delete actions right? |
| Logic placement | Is business logic in classes, with thin form and table methods? |
| Transactions | Are transactions short, with exception handling outside them? |
| Performance | Any selects inside loops, expensive display methods or missing field lists? |
| Security | Are there privileges for new entry points, grouped into duties? |
| Leftovers | Any temporary info calls, commented-out code or test data? |
Reviews were meant to be kind and specific. A good review comment says what is wrong, why it matters and what would fix it, such as: this select runs once per grid row; join CustTable as a data source instead, as in lesson 7. Prudence noticed after a few weeks that her pull requests drew fewer comments, not because Desmond had relaxed, but because she had started running the checklist on her own code before pushing.
Unit tests with SysTest
The SysTest framework is X++'s unit testing framework. A test class extends SysTestCase, and each public method whose name starts with test, or that carries the SysTestMethod attribute, is a test. Inside a test, assertion methods such as assertEquals, assertTrue and assertFalse check results. Tests are discovered and run in Visual Studio's Test Explorer, which shows passed and failed tests and lets a developer debug a failing one. The MB-500 outline asks candidates to create unit tests with the SysTest framework and Task recorder and to run them in Test Explorer.
[SysTestTarget(classStr(CRPumpWarrantyCalculator), UtilElementType::Class)]
public final class CRPumpWarrantyCalculatorTest extends SysTestCase
{
[SysTestMethod]
public void testEndDateIncludesAnniversary()
{
CRPumpWarrantyCalculator calculator = new CRPumpWarrantyCalculator(mkDate(31, 10, 2026), 24);
this.assertEquals(mkDate(31, 10, 2028), calculator.endDate());
}
[SysTestMethod]
public void testEndOfFebruaryInstall()
{
CRPumpWarrantyCalculator calculator = new CRPumpWarrantyCalculator(mkDate(29, 2, 2028), 12);
this.assertEquals(mkDate(28, 2, 2029), calculator.endDate());
}
[SysTestMethod]
public void testIsActiveOnLastDay()
{
CRPumpWarrantyCalculator calculator = new CRPumpWarrantyCalculator(mkDate(1, 11, 2026), 12);
this.assertTrue(calculator.isActive(mkDate(1, 11, 2027)));
this.assertFalse(calculator.isActive(mkDate(2, 11, 2027)));
}
}The leap-year test came from Teodora, who asked what happened to a pump installed on February 29. dateMthFwd moves to the last valid day of the target month, so 12 months after February 29, 2028 is February 28, 2029, and under the anniversary-inclusive rule that is the last day of cover. She agreed the result and it was written down as part of the rule.
Good unit tests are fast, independent and repeatable. Tests that only exercise calculations, like these, need no data. Tests of code that reads and writes tables need data, and the SysTest framework supports isolating tests so that data created in a test is rolled back afterwards, keeping environments clean. Desmond's guidance was to test logic in classes directly, with as little database work as possible, and to leave end-to-end checks of whole business processes to the regression tests described next.
Task recorder and regression tests
Task recorder records a user's steps in the browser as a task recording. Recordings are used for task guides and training, as lesson 9 mentioned, and also as the basis of automated tests. The Regression suite automation tool, RSAT, runs task recordings as tests against an environment with test data supplied in spreadsheets, and can be connected to Azure DevOps test plans. Recordings can also be turned into X++ test code in Visual Studio. MSBA-343 covers regression testing strategy; for now the point is that a business process such as Register a pump warranty manually can be recorded once and replayed after every change to catch breakage that unit tests would miss.
Static analysis beyond best-practice checks
Best-practice checks are not the only automated help. The compiler itself flags many problems as errors. Some teams add further checks to their pipelines, such as counting suppressions or checking that every new table has the audit fields turned on. Cobalt Ridge added one custom pipeline step: a script that fails the build if a label ID in the English file has no matching entry in the French or Spanish files, which turned the translation checklist from lesson 9 into an automatic gate.
AI assistance in reviews
GitHub Copilot can summarize a pull request, explain unfamiliar code and suggest changes, and Cobalt Ridge's developers used it, within limits. Desmond's rules were that a Copilot summary never replaces reading the diff, that suggested code is reviewed like any other code, and that nothing from a customer's data goes into a prompt. Copilot is often helpful for explaining standard X++ patterns to a newcomer and for drafting unit tests, which Prudence found useful for the boundary cases around dates. It is less reliable on finance and operations specifics, such as which methods can be wrapped or what a framework class expects, and it sometimes invents method names that do not exist. The compiler catches those quickly, which is one more reason to build before pushing.
The definition of done
A pull request is not done when the code works on the developer's screen. Cobalt Ridge's definition of done for a work item had six parts, and the review confirmed each one.
- The code builds with no new best-practice warnings, and unit tests for new logic pass in the pipeline.
- The change has been deployed and tested in the developer's environment as a user with the target role.
- All labels exist in every language file, with comments where needed.
- Security elements exist for new entry points and are in the right duties.
- The work item records what changed, which standard objects were extended, and any feature flags relied on.
- The task recording or test case for the business process is updated if the process changed.
The fifth point was the one most often missed, and the one that mattered most a year later. When Microsoft changes a standard object in a service update, the first question is which custom code touches it. The cross-reference database answers that for code, but only the work item history explains why the extension was written. Desmond asked for one or two sentences in plain language, such as: extends SalesLine posting to register pump warranties for serial-tracked items; requested by quality after the 2026 seal investigation.
Refactoring and technical debt
No codebase stays clean on its own. Reviews catch new problems, but old code drifts as requirements change, and every project accumulates shortcuts taken under deadline. Desmond kept a short technical debt list in the repository's Docs folder, with each item's cost and the risk of leaving it, and reserved part of each sprint for paying items off. Refactoring, changing the structure of code without changing its behaviour, is much safer with unit tests in place, because the tests confirm that behaviour did not change. That was another argument for writing tests early: the calculator could be reshaped freely because its tests would catch any change in its results.
Refactoring also has limits in finance and operations apps. Renaming a public method or a table field is a breaking change for any other code that uses it, and renaming a table field drops and recreates the column, losing data unless handled carefully. Such changes need planning, data upgrade steps and a release note. Internal and private members can be refactored freely, which is one more reason to keep them that way.
Worked problem
Reviewing a pull request
Prudence reviews a pull request from another partner developer that adds a field to the standard CustTable through an extension, fills it in a class extension of the sales order posting, and shows it on the customer form. What should her review check, using the checklist?
Show solution
- Build and tests: the pipeline passes, but there is no unit test for the new posting logic, so she asks for one.
- Names: the new field is called PreferredPartner without the CR prefix; she asks for CRPreferredServicePartner.
- Labels and data model: the field uses a new EDT with a label and help text, which is right.
- Logic placement and transactions: the posting extension does a select inside a loop over sales lines; she suggests reading the customer once before the loop.
- Security: the field is on a standard form the users already have access to, so no new privilege is needed, but the reviewer confirms the field is in the right field group.
Answer: Approve after three changes: add a test, add the prefix to the field name, and move the customer lookup out of the loop.
Common mistakes
MistakeSuppressing best-practice warnings to make the build clean.
InsteadFix the cause, and suppress only with a written reason a reviewer accepts.
MistakeReviewing code by reading for whatever stands out.
InsteadWork through an agreed checklist so important checks are never skipped.
MistakeWriting unit tests that depend on existing data in the environment.
InsteadTest logic directly with known inputs, or create and isolate test data inside the test.
MistakeMaking every method private to keep classes tidy.
InsteadExpose small, focused protected or public methods where others may need to extend behaviour.
Summary and key terms
- Best-practice checks run with the build and find problems the compiler accepts; fix warnings and suppress only with a reason.
- Naming, structure and extensibility conventions make custom code consistent, readable and safe to extend.
- Pull requests in Azure DevOps or GitHub combine automated builds, checks and tests with a human review against a checklist.
- SysTest unit tests extend SysTestCase, use assertions and run in Test Explorer.
- Task recorder recordings support task guides and automated regression tests with RSAT.
- Best-practice check
- An automated rule run during the build that flags code and metadata problems.
- Suppression
- An explicit, justified instruction to ignore a best-practice rule for a specific case.
- Pull request
- A request to merge a branch, used for automated checks and code review.
- SysTestCase
- The base class for X++ unit test classes.
- Test Explorer
- The Visual Studio window that discovers, runs and debugs tests.
- Task recorder
- A tool that records user steps in the browser for guides and tests.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Name two of the rule groups Cobalt Ridge treats as errors in the build.
Show answer
Any two of hard-coded labels, missing EDTs on new fields, and form pattern errors.
What does a test method assert in testEndDateIncludesAnniversary?
Show answer
That a 24-month warranty from October 31, 2026 ends on October 31, 2028.
What is RSAT used for?
Show answer
Running task recordings as automated regression tests with test data.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.