
Recently I came across the "Clean SAP UI5" book, which focuses on adopting clean code principles that focus on maintaining code that is easy to read, understand, and maintain. This is where the concept of "Clean Code" comes into play.
In this blog, I would like to highlight the principles of clean code in SAP UI5 along with illustrative examples, and at the end, I will share some thoughts based on practical experience in projects😉.
Clean Code is a set of practices that aim to produce code that is easy to read, simple to understand, and effortless to maintain. Robert C. Martin introduced these principles in his book "Clean Code: A Handbook of Agile Software Craftsmanship." These principles are universal and can be applied to any programming language or framework, including SAP UI5.
SAP UI5 is a robust framework for building responsive, enterprise-level applications. Given its complexity and the collaborative nature of enterprise projects, following clean code principles becomes even more critical. Clean code in UI5 development can lead to:
Meaningful Names
// Bad Example
var p; // what does p stand for?
// Good Example
var productPrice;
Single Responsibility Principle
Avoid Magic Numbers and Strings
// Bad Example
if (user.age > 18) { /* logic for adults */ }
// Good Example
const LEGAL_ADULT_AGE = 18;
if (user.age > LEGAL_ADULT_AGE) { /* logic for adults */ }
Comment Judiciously
// Bad Example
// This function adds two numbers
function add(a, b) {
return a + b;
}
// Good Example
// Adjusts the user balance after a transaction
function adjustUserBalance(user, amount) {
user.balance += amount;
}
DRY Principle (Don't Repeat Yourself)
// Bad Example
function getUserFirstName(user) { return user.firstName; }
function getUserLastName(user) { return user.lastName; }
// Good Example
function getUserProperty(user, property) { return user[property]; }
Proper Error Handling
// Bad Example
var data = JSON.parse(input);
// Good Example
try {
var data = JSON.parse(input);
} catch (error) {
console.error("Invalid JSON input: ", error.message);
}
Let's look at a simple SAP UI5 example to illustrate some of these principles. Assume we are building a function to retrieve and display product details.
// Bad Example
function getProductDetails(id) {
var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("/api/products/" + id, {}, false);
if (oModel.getData().price > 100) {
alert("Price is over 100");
}
var oView = sap.ui.getCore().byId("myView");
oView.setModel(oModel);
}
// Good Example
const API_ENDPOINT = "/api/products/";
const HIGH_PRICE_THRESHOLD = 100;
function getProductDetails(productId) {
const oModel = new sap.ui.model.json.JSONModel();
const productUrl = `${API_ENDPOINT}${productId}`;
oModel.loadData(productUrl, {}, false);
displayHighPriceAlert(oModel.getData().price);
setViewModel("myView", oModel);
}
function displayHighPriceAlert(price) {
if (price > HIGH_PRICE_THRESHOLD) {
alert("Price is over " + HIGH_PRICE_THRESHOLD);
}
}
function setViewModel(viewId, model) {
const oView = sap.ui.getCore().byId(viewId);
oView.setModel(model);
}
While the principles of clean code are ideal for creating maintainable and robust code, the reality of software development can often make it challenging to adhere strictly to these guidelines. Here are some common obstacles developers might face:
From my perspective, it's essential to find a balance between ideal practices and practical constraints. and do so Here are a few tips:
Applying clean code principles to SAP UI5 development is essential for building maintainable, scalable, and robust applications. However, it's important to recognize that real world constraints can make it challenging to follow these principles perfectly. By understanding these constraints and striving for continuous improvement, developers can still achieve a high standard of code quality that benefits both the team and the end product.
Happy coding! 🤗
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.