
#php
When we write code, it may be functional and working on our computers, but what about its robustness and readability?
For example, consider a simple PHP truncate function that is used to deal with content on websites. This function has only one property and is intended to return a string truncated to a defined number of characters, with an ellipsis added to the end. However, we can do a little refactoring and critical thinking to make it better.
We can improve the robustness of the code by adding error handling and testing it thoroughly to ensure it works correctly under different conditions. We can also improve the readability of the code by adding comments to explain the purpose and logic of the code, and by using descriptive variable names and clear formatting.
By making these changes, we can make the code more maintainable and easier to understand for ourselves and others who may work with it in the future.
function truncate($txt=null){
$len = strlen($txt);
return substr($txt, 0, $len <= 40 ? $len : 40)."...";
}
This code may work, but we can make it more complex and dynamic by adding properties to the function for the desired length of the string and the option to replace the ellipsis with something else. We can also add the empty() method inside the function to check if the $txt variable is null and exit if it is.
Declaring the type of the function's parameters can also be useful for debugging and making it more clear to other programmers how to use the function. For example, we could declare the type of the $length parameter as an int to indicate that it should be an integer, and the type of the $ellipsis parameter as a string to indicate that it should be a string.
This makes it obvious to other programmers how the function should be used, and it can help to catch errors and bugs more quickly
function truncate(string $txt, int $maxLen=40, string $elipsisOr="..."):string {
if(empty($txt)):
return "null";
endif;
$len = strlen($txt);
return substr($txt, 0, $len <= $maxLen ? $len : $maxLen) . $elipsisOr;
}
It seems like the function is looking pretty nice now. Notice that we have added default values to all the properties of the function. But we can go even further and make the function support an exact byte count, even for characters that are not part of the Latin alphabet, such as Japanese.
To do this, we can use multi-byte functions from PHP. For example, we could use the mb_substr() function instead of the substr() function to support characters from different encodings. This will allow the function to correctly truncate text written in languages other than English.
By making these changes, we can improve the flexibility and usability of the function, and make it more useful for a wider range of applications.
function truncate(string $txt, int $maxLen=40 ,string $elipsisOr="...",):string {
if(empty($txt)):
return "null";
endif;
$len = mb_strlen($txt);
return mb_substr($txt, 0, $len <= $maxLen ? $len : $maxLen, 'UTF-8') . $elipsisOr;
}
Voila! It is decent and ready to truncate our content to the perfect length.
input: truncate("こんにちは、世界!techtoapes.comを読んで楽しんで、これは素晴らしいサイトです");
output:
こんにちは、世界!techtoapes.comを読んで楽しんで、これは素晴らしい...
There, this is a tiny example of what refactoring is, we could use if/else statements instead ternary operators to make it more readable but since this is such a small function I find this neater.
[root@techtoapes]$ Author Admin Luka
Login to comment.