Topic: A few pretty anti-patterns


Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/04/11
Raw View


Rob Stewart schrieb:
>
> Robert Klemme wrote:
> >
> > Ken Bloom schrieb:
> > >
> > > If you need to construct an empty string from a null pointer:
> > > const char* ifnotnull(const char* x)
> > > {
> > >     static const char* empty="";
> > >     if (x) return x;
> > >     else return empty;
> > > }
> >
> > const char* ifnotnull(const char* x)
> > {
> >     static const char* empty="";
> >     return x ? x : empty;
> >     // or, a bit more obvious: return (x == NULL) ?  empty : x;
> > }
>
> Why not this?
>
> std::string ifnotnull(char const * x)
> {
>     std::string result;
>     if (x)
>     {
>         result = x;
>     }
>     return result;
> }
>
> Isn't the goal to construct a std::string?

:-))

yes, much better!

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra_e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/04/03
Raw View
Robert Klemme wrote:
>
> Ken Bloom schrieb:
> >
> > If you need to construct an empty string from a null pointer:
> > const char* ifnotnull(const char* x)
> > {
> >     static const char* empty="";
> >     if (x) return x;
> >     else return empty;
> > }
>
> const char* ifnotnull(const char* x)
> {
>     static const char* empty="";
>     return x ? x : empty;
>     // or, a bit more obvious: return (x == NULL) ?  empty : x;
> }

Why not this?

std::string ifnotnull(char const * x)
{
    std::string result;
    if (x)
    {
        result = x;
    }
    return result;
}

Isn't the goal to construct a std::string?

--
Robert Stewart     |  rob-at-giage-dot-com
Software Engineer  |  using std::disclaimer;
Giage, Ltd.        |  http://www.giage.com

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: "Ken Bloom" <ken195@forfree.at>
Date: 2000/03/28
Raw View
Robert Klemme <robert.klemme@myview.de> wrote in message
news:38D9DCED.B6DE4459@myview.de...
>
>
>
> Marcin Kasperski schrieb:
> >
> > I just would like to share a few small anti-patterns. All are real-life
> > examples from the project I participated in and all costed considerable
> > amount of time to diagnose. While presenting them I would like to ask
> > the question: does there exist any 'lint-type' tool which detects errors
> > of this kind?
> >
> > [other patterns deleted]
> >
> > 3) That #$#@$@ string(0)
> >
> > I have seen a lot of coredumps caused by constructing strings from null
> > pointer. It is particularly frequent while mixing C and C++ worlds. Take
> > a look at this (this is real world coredump reason):
> >
> >         string s = getlogin();
> >
> > Most times it works, but _very rarely_ it happen, that getlogin returns
> > NULL. And then .... buuuuum. Moreover, usually coredump happens not
> > while performing the above constructor but later, while using this
> > string in some way (or while using its copy).
>
> the standard says:
>
> basic_string(const charT* s, const Allocator& a = Allocator());
> Requires: s shall not be a null pointer.
>
> > BTW: In my opinion string constructor should take care for such
> > happenings and construct empty string whenever given NULL pointer.
>
> one can argue about that one: you could say that NULL is not a valid
> string and announces a serious error condition.  so, in order to make
> this error clear, the process cores.  this forces you to handle this
> situation yourself.  if the string would do this, you could not
> distinguish these situations:
>
> string s(NULL);
> string s("");
>
> what do others think?  can anybody provide the reasoning behind this?
>

Sounds good. If you need to construct an empty string from a null pointer:
const char* ifnotnull(const char* x)
{
    static const char* empty="";
    if (x) return x;
    else return empty;
}




      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/03/30
Raw View


Ken Bloom schrieb:
>
> Sounds good.

thank you.

> If you need to construct an empty string from a null pointer:
> const char* ifnotnull(const char* x)
> {
>     static const char* empty="";
>     if (x) return x;
>     else return empty;
> }

may i apply a slight change, which seems to be an improvement to me?

const char* ifnotnull(const char* x)
{
    static const char* empty="";
    return x ? x : empty;
    // or, a bit more obvious: return (x == NULL) ?  empty : x;
}

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Robert Klemme <robert.klemme@myview.de>
Date: 2000/03/24
Raw View


Marcin Kasperski schrieb:
>
> I just would like to share a few small anti-patterns. All are real-life
> examples from the project I participated in and all costed considerable
> amount of time to diagnose. While presenting them I would like to ask
> the question: does there exist any 'lint-type' tool which detects errors
> of this kind?
>
> [other patterns deleted]
>
> 3) That #$#@$@ string(0)
>
> I have seen a lot of coredumps caused by constructing strings from null
> pointer. It is particularly frequent while mixing C and C++ worlds. Take
> a look at this (this is real world coredump reason):
>
>         string s = getlogin();
>
> Most times it works, but _very rarely_ it happen, that getlogin returns
> NULL. And then .... buuuuum. Moreover, usually coredump happens not
> while performing the above constructor but later, while using this
> string in some way (or while using its copy).

the standard says:

basic_string(const charT* s, const Allocator& a = Allocator());
Requires: s shall not be a null pointer.

> BTW: In my opinion string constructor should take care for such
> happenings and construct empty string whenever given NULL pointer.

one can argue about that one: you could say that NULL is not a valid
string and announces a serious error condition.  so, in order to make
this error clear, the process cores.  this forces you to handle this
situation yourself.  if the string would do this, you could not
distinguish these situations:

string s(NULL);
string s("");

what do others think?  can anybody provide the reasoning behind this?

regards

 robert

--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra   e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]