Jump to content

  • Log In with Google      Sign In   
  • Create Account

Industrial Strength Hash Table

  • You cannot reply to this topic
17 replies to this topic

#1 Artemiye   Members   -  Reputation: 1255

Like
0Likes
Like

Posted 12 March 2014 - 12:26 PM

I've been enjoying the coding horrors forum for a while, so I decided to post a snippet of code that I found in a large c++ project that I'm (unfortunately) a part of.  Normally I wouldn't poke fun at code like this, but I couldn't resist this one.

 

Enjoy.

#pragma once

#include "Includes.h"



template<class T>

class Dictionary

{

private:

    std::vector<std::string> keys;

    std::vector<T> values;

public:

    Dictionary()

    {

        keys = new std::vector<std::string>{};

        values = new std::vector<T>{};

    }



    ~Dictionary()

    {

        delete[] keys;

        delete[] values;

    };



    std::vector* Values() { return &values; };

    std::vector* Keys() { return &keys; };



    size_t size() { return keys.size(); }



    void Add(std::string _key, T value)

    {

        keys.push_back(_key);

        values.push_back(value);

    };

    void Remove(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                keys.erase(keys.begin() + i);

                values.erase(values.begin() + i);

                break;

            }

        }

    };

    T Get(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                return values.at(i);

            }

        }

        return nullptr;

    };

};



#2 TheComet   Crossbones+   -  Reputation: 2536

Like
12Likes
Like

Posted 12 March 2014 - 01:08 PM

So let's see what the WTFs basically are...

  • Assigning a pointer generated from new to a type that is not a pointer
  • Using delete[] on objects created with new and not new[]
  • Using braces instead of brackets for trying to call the constructor of an object
  • Returning a pointer pointing to what is supposed to be an array of pointers pointing to pointers (given the new[] and delete[]), even though the value being returned isn't of type pointer (which is a double-WTF because if it were to compile, that would still be retarded)
  • using a lookup method of complexity O(n)
  • Attempting to declare a return type without template arguments.
  • Returning a nullptr in the case of not finding an entry, implying that the templated type is always a pointer.
  • Method names begin with a capital letter
  • not using std::map
  • code doesn't even compile

Evaluation:

 

tumblr_mc2w7cEoiz1qmiwbe.png


Edited by TheComet, 13 March 2014 - 05:52 AM.

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#3 Rattrap   Members   -  Reputation: 2909

Like
8Likes
Like

Posted 12 March 2014 - 01:19 PM

#include "Includes.h"

 

Got to love the all encompassing include.


Method names begin with a capital letter

This one is excusable, since that is just personal preference.  I personally don't like camelCase.


"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]


#4 ferrous   Members   -  Reputation: 5079

Like
5Likes
Like

Posted 12 March 2014 - 01:40 PM

Yeah, my favorite is that the person is using std::Vector, but didn't bother to look for existing hash table functionality.  Or maybe they did but their google fu sucks.  

 

And I agree with Rattrap, the capital letter on method names is actually the coding guideline here at my work.  



#5 MaxDZ8   Crossbones+   -  Reputation: 4948

Like
0Likes
Like

Posted 13 March 2014 - 02:07 AM

One also wonders why they didn't made a pair to use a single vector.

Want to talk about exception safety?


Previously "Krohm"


#6 Hodgman   Moderators   -  Reputation: 48430

Like
1Likes
Like

Posted 13 March 2014 - 02:30 AM

Does that code even compile? In what compiler?



#7 Mona2000   Members   -  Reputation: 1781

Like
1Likes
Like

Posted 13 March 2014 - 05:09 AM

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.



#8 Juliean   GDNet+   -  Reputation: 5824

Like
1Likes
Like

Posted 13 March 2014 - 05:42 AM


So let's see what the WTFs basically are...

 

I would add:

 

- Creating the std::vectors with new, where they would better be created on the stack. Its not like one of the vectors benefits is that it takes away the memory management for dynamic arrays.

 

- Returning both vectors as non-const pointer, basically breaking every concept of encapsulation of that class. Especially hard since both vectors are meant to store dependant values for one entry, imagine someone deleting values out of only the Value-vector.

 

- Also, returning the vectors by pointer instead of reference when they can never be nullptr.



#9 ByteTroll   Crossbones+   -  Reputation: 2755

Like
0Likes
Like

Posted 13 March 2014 - 05:49 AM


Method names begin with a capital letter

 

Hey! What's wrong with this? rolleyes.gif

 

I would like to add one.  How about every line being spaced with the beginning of each function being double spaced.  That might be "personal" preference, but it is a hell of an annoying one.

 

 

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.

 

 

Hehehehe.

EDIT: Or the fact that every function is terminated with a semicolon.  Again, personal preference; hell of an annoying one.


Edited by ByteTroll, 13 March 2014 - 05:52 AM.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

 

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob


#10 TheComet   Crossbones+   -  Reputation: 2536

Like
0Likes
Like

Posted 13 March 2014 - 05:49 AM

 

Does that code even compile? In what compiler?

 

In every compiler.

 

As long as you don't use that template.

 

Not instantiating a template doesn't prevent the first compilation pass, which will catch syntactical errors such as:

std::vector* Values() { return &values; }; // invalid use of template-name "std::vector" without an argument list

Edited by TheComet, 13 March 2014 - 05:50 AM.

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#11 l0calh05t   Members   -  Reputation: 1489

Like
3Likes
Like

Posted 13 March 2014 - 06:11 AM


Using braces instead of brackets for trying to call the constructor of an object

 

That's actually valid in C++11 and called uniform initialization syntax.



#12 TheComet   Crossbones+   -  Reputation: 2536

Like
3Likes
Like

Posted 13 March 2014 - 06:23 AM

 


Using braces instead of brackets for trying to call the constructor of an object

 

That's actually valid in C++11 and called uniform initialization syntax.

 

Thanks for the clarification. I learned something new today!


"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#13 HyperV   Crossbones+   -  Reputation: 1535

Like
0Likes
Like

Posted 14 March 2014 - 03:53 AM

i'm starting to have a trauma



#14 TheComet   Crossbones+   -  Reputation: 2536

Like
0Likes
Like

Posted 14 March 2014 - 05:46 AM


EDIT: Or the fact that every function is terminated with a semicolon.  Again, personal preference; hell of an annoying one.

WTF I didn't notice that at first.


"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#15 samoth   Crossbones+   -  Reputation: 8339

Like
1Likes
Like

Posted 14 March 2014 - 05:48 AM

Using delete[] on objects created with new and not new[]

Relax, it is much worse than you think.laugh.png

 

delete[] is being called on objects which are not explicitly allocated via new at all. The vectors that are allocated on the heap are copied (if the compiler accepts this code at all!) and then leaked. I wonder how you can assign a pointer-to-vector to a vector though, since to my knowledge it only accepts const T& and T&& and std::initializer_list<T>.

 

Both keys and values are objects, not pointers, and part of the Dictionary object. Which means that you call delete (with or without brackets, who cares) on an object that is either allocated on the stack or inside a larger heap-allocated object. In either case, this is very bad mojo.

 

The Dictionary class has no virtual functions and keys is the first member, so likely (void*) keys == (void*) dictionary_object

Which means if you somehow get the compiler to accept this code (I would be surprised, though), it would double-delete the Dictionary object, once explicitly and once from its own destructor via a type-punned std::vector pointer.


Edited by samoth, 14 March 2014 - 05:49 AM.


#16 Artemiye   Members   -  Reputation: 1255

Like
0Likes
Like

Posted 14 March 2014 - 12:48 PM


EDIT: Or the fact that every function is terminated with a semicolon. Again, personal preference; hell of an annoying one.

 

Haha, but get this, only some functions are terminated with a semicolon.  Others aren't.



#17 Oberon_Command   Crossbones+   -  Reputation: 4736

Like
4Likes
Like

Posted 16 March 2014 - 03:36 PM

This looks like Java code that someone tried to port without an actual understanding of C++.



#18 swiftcoder   Senior Moderators   -  Reputation: 17600

Like
0Likes
Like

Posted 16 March 2014 - 03:49 PM

This looks like Java code that someone tried to port without an actual understanding of C++.

That reminds me of a joke which has become decidedly unfunny on my current project:

 

What's worse than C++ written by a Java developer?

 

Spoiler


Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]






PARTNERS