How to choose between different ways to write some code?

I would like to validate point in a two-dimensional rectangular Cartesian coordinate. I found following regular expression solution on stackoverflow ^(-?d+(.d+)?),s*(-?d+(.d+)?)$
. Another solution is using little PHP controls:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> $xy = explode(",", "-3,3");
$return = array(0,0);
if(is_array($xy) && count($xy) == 2) {
$return = $xy;
foreach($xy as $digit) {
if(!is_numeric($digit)) {
$return = array(0,0);
break;
}
}
}
</code>
<code> $xy = explode(",", "-3,3"); $return = array(0,0); if(is_array($xy) && count($xy) == 2) { $return = $xy; foreach($xy as $digit) { if(!is_numeric($digit)) { $return = array(0,0); break; } } } </code>
    $xy = explode(",", "-3,3");
    $return = array(0,0);

    if(is_array($xy) && count($xy) == 2) {
        $return = $xy;
        foreach($xy as $digit) {
            if(!is_numeric($digit)) {
                $return = array(0,0);
                break;
            }
        }
    } 

Although, seconds solution is “larger”, I prefer this for readability. What elements should I use to make the right choice between those two solutions?

2

Here are some points you should take in consideration:

  • Readability: long regular expressions are difficult to understand. Use comments (and eventually break one long expression into several small ones, if possible) to make it easier to read. At the same time, the piece of PHP code in your question can (and should) be refactored to make it more readable.

  • Readability by beginner programmers: if your team has beginner programmers, they may be completely lost and horrified when facing a long regular expression. In the same way, beginners may prefer imperative style over functional one.

    In a case of doubt, talk with your team. Ask if everyone is fine using regular expressions. Show this one (with comments) and ask if they understand it. If you work with experienced developers, you may also add comments, then introduce a error into the expression itself, and ask them if they notice anything wrong.

  • Performance: benchmark extensively both solutions. It might be that the one is terribly slow compared to another one. Don’t guess—measure.

    Note that if both conform to your expectations, you shouldn’t take the performance element into consideration. For example, if you expect the code to run in 5 ms., and one piece of code takes 14 µs, while the second one takes 960 µs, don’t take the first one just because it is faster.

  • Maintenance: if you expect the code to be modified, think twice. Would it be easy to modify the regular expression (once it is properly commented)? Is it safe to modify the piece of code without breaking anything?

    Having regression testing ensures you can modify code without breaking it, but that’s not the point. If the code is difficult to modify, nobody will dare touching it, even with regression tests.

Finally, you must obviously understand both to pick the right one for you/your project/your team. This means that:

  • You should be able to add relevant comments to the regular expression,

  • You should be able to see something wrong in the pice of PHP code you copy-pasted and be able to refactor this.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật