Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:47:07+00:00 2026-05-27T08:47:07+00:00

I was analyzing my code with PHP Mess Detector when PHPMD reported some of

  • 0

I was analyzing my code with PHP Mess Detector when PHPMD reported some of my codes has high NPath Complexity. One example would be:

function compareDates($date1, $date2){
    if($date->year < $date2->year){
        return -1;
    }
    if($date->year > $date2->year){
        return 1;
    }
    if($date->month < $date2->month){
        return -1;
    }
    if($date->month > $date2->month){
        return 1;
    }
    if($date->day < $date2->day){
        return -1;
    }
    if($date->day > $date2->day){
        return 1;
    }
    // etc.. same for hour, minute, second.
    return 0;
}

The result would be that this function has very high NPath complexity. Is there a generic way of coding to reduce such control structures and NPath complexity?

Source Code: http://code.google.com/p/phpraise/source/browse/trunk/phpraise/core/datetime/RaiseDateTime.php#546

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:47:07+00:00Added an answer on May 27, 2026 at 8:47 am

    Your code actually is relatively simple, just poorly structured. I would recommend creating a subfunction that takes two parameters and handles the return of -1/1 and then iterating through an array of fields to check on, as this would be a bit easier, but a few things of note:

    1. Your way is OK. It’s not clean, but it’s clear and if it works there’s no pressing need to change it – any programmer who looks at it will be able to understand what you’re doing, even if they scoff at your implementation.

    2. Complexity isn’t a holy grail. It’s important, and as a programmer who does a lot of maintenance programming I think it’s really important that the people writing the code I maintain know about complexity, but you can’t entirely avoid complexity and sometimes the complex solution (using McCabe’s Complexity) is the easiest to read.

    The only change I would really recommend you making is having a single return call. Do something like:

    $compare_val = 0;
    

    At the top of your file, and then change your subsequent if calls to elseifs and instead of returning the value, just update $compare_val and return it at the end of your function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am kinda new to this Regex thing. When analyzing some code I frequently
I'm analyzing some code that utilizes empty OVER clauses in the contest of Count().
I am analyzing someone else's PHP code and I've noticed that the input HTML
I'm analyzing some Python code and I don't know what pop = population[:] means.
I'm analyzing some legacy code. It is about 80.000 lines of old plsql code.
I'm analyzing some old SQL code in a stored procedure. Declare @Var1 money, @Var2
I am analyzing some .net desktop application. I have found that code: if (System.Environment.ProcessorCount
I do lots of work manipulating and analyzing PHP code. Normally I just use
I'm analyzing some code and I'm looking for string literals, to check if I
Does Ruby have any tools along the lines of pylint for analyzing source code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.