lib/datetime: Reduce cognitive complexity of core datetime functions#7598
lib/datetime: Reduce cognitive complexity of core datetime functions#7598NeelGhoshal wants to merge 11 commits into
Conversation
…exity from 68 to the 25 allowed.
…|while|switch statements
…|while|switch statements
…exity from 41 to the 25 allowed
…exity from 98 to the 25 allowed
…exity from 101 to the 25 allowed
…xity from 47 to the 25 allowed
…|while|switch statements
…|while|switch statements
…exity from 56 to the 25 allowed
…exity from 35 to the 25 allowed
|
Could you at least compile locally first? It doesn’t compile on any platform and configuration yet |
|
|
||
| static void make_incr(DateTime *, int, int, DateTime *); | ||
|
|
||
| /* Handles the "round == 0" branch for (to < dt->to): increment using all lost values */ |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| /* Handles the "round == 0" branch for (to < dt->to): increment using all lost values */ | |
| /* Handles the "round == 0" branch for (to < dt->to): increment using all lost | |
| * values */ |
| break; | ||
| } | ||
| } | ||
| /* Returns 1 if any “lost” component between dt->to down to (to+1) is non-default */ |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| /* Returns 1 if any “lost” component between dt->to down to (to+1) is non-default */ | |
| /* Returns 1 if any “lost” component between dt->to down to (to+1) is | |
| * non-default */ |
| static void round_lost_precision(DateTime *dt, int to, int round) | ||
| { | ||
| DateTime incr; | ||
| int pos, carry, ndays; | ||
|
|
||
| if (to >= dt->to) | ||
| return; | ||
|
|
||
| if (round > 0) { | ||
| int x = datetime_is_absolute(dt) ? 1 : 0; | ||
|
|
||
| carry = has_lost_non_default(dt, to, x); | ||
| } | ||
|
|
||
| if (carry) { | ||
| make_incr(&incr, to, to, dt); | ||
| incr.year = 1; incr.month = 1; incr.day = 1; | ||
| incr.hour = 1; incr.minute = 1; incr.second = 1.0; | ||
| datetime_increment(dt, &incr); | ||
| } | ||
| } | ||
|
|
||
| if (round == 0) { | ||
| apply_round0_lost(dt, to, &incr); | ||
| } | ||
| } |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| static void round_lost_precision(DateTime *dt, int to, int round) | |
| { | |
| DateTime incr; | |
| int pos, carry, ndays; | |
| if (to >= dt->to) | |
| return; | |
| if (round > 0) { | |
| int x = datetime_is_absolute(dt) ? 1 : 0; | |
| carry = has_lost_non_default(dt, to, x); | |
| } | |
| if (carry) { | |
| make_incr(&incr, to, to, dt); | |
| incr.year = 1; incr.month = 1; incr.day = 1; | |
| incr.hour = 1; incr.minute = 1; incr.second = 1.0; | |
| datetime_increment(dt, &incr); | |
| } | |
| } | |
| if (round == 0) { | |
| apply_round0_lost(dt, to, &incr); | |
| } | |
| } | |
| static void round_lost_precision(DateTime * dt, int to, int round) | |
| { | |
| DateTime incr; | |
| int pos, carry, ndays; |
| } | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| if (to >= dt->to) | |
| return; | |
| } | ||
|
|
||
|
|
||
| round_lost_precision(dt, to, round); |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| round_lost_precision(dt, to, round); | |
| if (round > 0) { | |
| int x = datetime_is_absolute(dt) ? 1 : 0; |
| static int apply_relative_fields(DateTime *dt, int from, int to, | ||
| int year, int month, int day, | ||
| int hour, int minute, double second); |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| static int apply_relative_fields(DateTime *dt, int from, int to, | |
| int year, int month, int day, | |
| int hour, int minute, double second); | |
| static int apply_relative_fields(DateTime *dt, int from, int to, int year, | |
| int month, int day, int hour, int minute, | |
| double second); |
| if (datetime_set_type(dt, DATETIME_ABSOLUTE, | ||
| DATETIME_YEAR, a.to, a.fracsec)) |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| if (datetime_set_type(dt, DATETIME_ABSOLUTE, | |
| DATETIME_YEAR, a.to, a.fracsec)) | |
| if (datetime_set_type(dt, DATETIME_ABSOLUTE, DATETIME_YEAR, a.to, | |
| a.fracsec)) |
| return 0; | ||
| if (datetime_set_type(dt, DATETIME_RELATIVE, from, to, fracsec)) | ||
| return 0; | ||
| if (!apply_relative_fields(dt, from, to, year, month, day, hour, minute, second)) |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| if (!apply_relative_fields(dt, from, to, year, month, day, hour, minute, second)) | |
| if (!apply_relative_fields(dt, from, to, year, month, day, hour, minute, | |
| second)) |
| static int apply_relative_fields(DateTime *dt, int from, int to, | ||
| int year, int month, int day, | ||
| int hour, int minute, double second) |
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
| static int apply_relative_fields(DateTime *dt, int from, int to, | |
| int year, int month, int day, | |
| int hour, int minute, double second) | |
| static int apply_relative_fields(DateTime *dt, int from, int to, int year, | |
| int month, int day, int hour, int minute, | |
| double second) |
|
|
||
|
|
There was a problem hiding this comment.
[pre-commit] reported by reviewdog 🐶
Description
Refactor five files in
lib/datetimeto bring all functions within thecognitive complexity limit of 25 (previously flagged by static analysis).
change.cdatetime_change_from_*diff.cdatetime_diffformat.cdatetime_formatincr1.cdatetime_increment_*scan.cdatetime_scanRefactoring techniques used: early returns to reduce nesting, extraction
of repeated conditional blocks, and elimination of redundant else-after-return
patterns.
Motivation and context
High cognitive complexity makes functions hard to read, test, and maintain.
These functions in
lib/datetimehad among the highest complexity scores inthe GRASS codebase. All changes are behaviour-preserving refactors — no
logic has been altered.
How has this been tested?
lib/datetimehas no dedicated C unit test suite. These are structuralrefactors with no logic changes; correctness depends on careful review.
Reviewers are encouraged to diff the original and refactored versions
function by function.
Note: adding tests to
lib/datetimewould be a valuable follow-up.Types of changes
This is a refactoring/maintainability change only.
Checklist