Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDate doc: Null date in comparison

QDate doc: Null date in comparison

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sentret_C
    wrote on last edited by Sentret_C
    #1

    If I read the source code correctly, default constructed QDate instances are considered to be less than any valid ones and equal to each other. However this behaviour is not documented, so the question is: can I rely on it, or if it's better to explicitly address the null case?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You mean like
      QDate date;
      qDebug() << date << "is valid" << date.isValid();
      result:
      QDate(Invalid) is valid false

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sentret_C
        wrote on last edited by Sentret_C
        #3

        Hi @mrjj,
        I mean to get the latest date in a loop:

        QDate latest;
        for (auto obj : objList) {
            if (someCondition(obj) && obj.date() > latest) {
                latest = obj.date();
            }
        }
        

        Since (anyValidDate > QDate()) is true, the code above works. However the doc doesn't say what the result of comparison will be if either or both comparands are null, so should I write

        if (someCondition(obj) && (latest.isNull() || obj.date() > latest))
        

        or (if obj.date() can be null too and I really want a strict comparison)

        if (someCondition(obj) && ((latest.isNull() && obj.date().isValid) || (latest.isValid && obj.date().isValid && obj.date() > latest)))
        

        instead in case that the behavior changes in a later version?
        It seems that the documentation generally lack details like edge cases and often I need to read the source code.

        JKSHJ 1 Reply Last reply
        0
        • S Sentret_C

          Hi @mrjj,
          I mean to get the latest date in a loop:

          QDate latest;
          for (auto obj : objList) {
              if (someCondition(obj) && obj.date() > latest) {
                  latest = obj.date();
              }
          }
          

          Since (anyValidDate > QDate()) is true, the code above works. However the doc doesn't say what the result of comparison will be if either or both comparands are null, so should I write

          if (someCondition(obj) && (latest.isNull() || obj.date() > latest))
          

          or (if obj.date() can be null too and I really want a strict comparison)

          if (someCondition(obj) && ((latest.isNull() && obj.date().isValid) || (latest.isValid && obj.date().isValid && obj.date() > latest)))
          

          instead in case that the behavior changes in a later version?
          It seems that the documentation generally lack details like edge cases and often I need to read the source code.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Sentret_C said in QDate doc: Null date in comparison:

          should I write

          if (someCondition(obj) && (latest.isNull() || obj.date() > latest))
          

          I think you should, IMHO.

          in case that the behavior changes in a later version?

          I don't think this is the most important reason.

          The most important reason is that the longer code is more explicit about your intentions. Someone who reads your code in the future will think, "Ah, so the author intended to compare against null dates this way", instead of "I wonder if the author thought about the possibility of the date being null...?"

          It seems that the documentation generally lack details like edge cases

          If it's not documented, a good rule of thumb is to treat it like undefined behaviour and don't rely on it. (The only decent exception I can think of is when you need to really squeeze every bit of optimization out of your code -- even then, proceed with caution)

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved