Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79463

Re: C++ may sometimes be *too* simple (to use)

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: C++ may sometimes be *too* simple (to use)
Date 2021-05-08 20:27 +0000
Organization Aioe.org NNTP Server
Message-ID <s76s75$6lp$1@gioia.aioe.org> (permalink)
References (6 earlier) <566e0717-f1e7-4b47-8b1f-4a4d0d9685bfn@googlegroups.com> <s6r8h6$9ar$1@gioia.aioe.org> <f5ed4c25-f259-43fc-8a31-737f00433bb2n@googlegroups.com> <s73066$l0j$1@dont-email.me> <37932bde-400e-40ea-9812-5bf2b622c4c6n@googlegroups.com>

Show all headers | View raw


?? Tiib <ootiib@hot.ee> wrote:
> Also its starship
> nature is good bear trap for novices if you want to be asshole:
> 
> if (!a.compare(b)) {
>     // get here when a and b are equal   
>     // but majority of novices read opposite
> } 

Good programming practices, and common sense in programming, would dictate
to avoid deliberate obfuscation and "fancy syntactic tricks" (when much
more readable and understandable 100% equivalent alternatives exist).

Many a programmer, especially those who never grew out of their beginner
programmer fascination phase, may engage in fancy syntactical "tricks"
just because they like them, find them fancy and cool, but a good programmer
is distinguished by being able to write clear readable code that's still
reasonably simple and reasonably efficient.

Anyway, rather obviously there are benefits to the trinary comparison
result in many situations. Consider, for example, that std::lower_bound
does not tell you whether the searched element exists in the range or not,
it only tells you where the element should be inserted to keep the range
sorted. While there exists a std::binary_search, that only tells you whether
it exists or not, but not where to insert the element if you wanted.
Thus, if you wanted to check (using a binary search) whether the element
exists and insert it only if it didn't exist, std::binary_search would tell
you the point of insertion, but you still need to make an additional equality
check to see if it already exists. Meaning that the last element compared
in the search would have to be compared twice. If the comparison is
relatively expensive (eg. the elements are large strings), this is a waste
of time which the trinary comparison operator solves: It directly tells you
if that last element that was compared was larger or equal to the searched
element, so you just need to check the integer value to know.

(Unfortunately std::lower_bound doesn't give you this value, so you would
need to perform the binary search yourself. But the trinary comparison
result will help you in this case to avoid the double comparison of the
same element at the end of the search.)

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 12:07 +0000
  Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-27 17:57 +0300
    Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-27 18:14 -0700
      Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-28 05:21 +0000
        Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-28 09:09 -0700
          Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-29 04:45 +0000
            Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-29 15:16 -0700
              Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-30 10:10 +0000
                Re: C++ may sometimes be *too* simple (to use) Bo Persson <bo@bo-persson.se> - 2021-04-30 15:33 +0200
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:32 +0000
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-01 06:51 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-03 21:38 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 18:21 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:05 +0300
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:43 +0000
                Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-04 15:20 +0200
                Re: C++ may sometimes be *too* simple (to use) Manfred <noname@add.invalid> - 2021-05-04 16:41 +0200
                Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-04 23:42 -0400
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-05 05:01 +0000
                Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-05 11:26 +0200
                Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-05 09:53 -0400
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 21:40 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-07 07:38 +0000
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 22:32 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:27 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:10 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-07 09:40 -0700
                Re: C++ may sometimes be *too* simple (to use) red floyd <no.spam.here@its.invalid> - 2021-05-07 17:59 -0700
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:42 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-08 20:27 +0000
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-09 09:56 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-10 05:07 +0000
                Re: C++ may sometimes be *too* simple (to use) Richard Damon <Richard@Damon-Family.org> - 2021-05-10 07:29 -0400
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-10 20:18 -0700
                Re: C++ may sometimes be *too* simple (to use) Hans Bos <hans.bos@xelion.nl> - 2021-05-07 16:06 +0200
  Re: C++ may sometimes be *too* simple (to use) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-27 19:57 -0700
  Re: C++ may sometimes be *too* simple (to use) wij <wyniijj@gmail.com> - 2021-04-27 21:15 -0700
    Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-28 11:13 +0300
  Re: C++ may sometimes be *too* simple (to use) "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2021-05-03 10:28 +0200
    Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:53 +0000

csiph-web