Random pet peeve: '\0'

Pages: 1234... 7
Maybe the default settings are messed up?
Here's my pet peeve:

1
2
3
class MyClass {
    // stuff
};


instead of:

1
2
3
4
class MyClass
{
    // stuff
};


I don't know why, but it still makes me twitch... however since half the programmers out there do that... I'll just have to get use to it at some point.
@naraku VC++2008 Express Edition (I have to use it for compatibility with an SDK)

@Oria My pet peeve is when people define a class with the class keyword instead of the struct keyword, because 99% of the time they only use public inheritance and have the public: keyword right up front.
@ LB: so you really can't compile my C++98 standard compliant code?

That's pretty surreal, and inviting ridicule of Microsoft. But it can't be right, it just can't.
@Catfish3: I also can't compile anything that prefixes c function names with std:: even if you include the <c****> headers, which the standard mandates should be in the std:: namespace. For example, this won't compile for me:
1
2
3
4
5
6
#include <cstdio>

int main()
{
    std::printf("Hello, cstdio!");
}
http://ideone.com/T98C9f
Disch wrote:
I was going to write a rant to explain the reasons why I don't like it... but I am getting extremely sleepy so I'll just say it annoys me and I wish people wouldn't do it.


Agreed. An explanation is not even necessary.
@LB
Try clang or gcc, they're both much better compilers.
@chrisname I use clang as my primary compiler. As I already explained, I have to use VC++2008 for compatibility with an SDK.
LB wrote:
@Oria My pet peeve is when people define a class with the class keyword instead of the struct keyword, because 99% of the time they only use public inheritance and have the public: keyword right up front.

Yeah I would have to agree that if you use a "class", it should be because you intent to use private fields, etc. However I usually use classes most of the time because I believe you should keep your members private.
It's just personal preference, but I like to put the members at the very bottom to keep them out of the way when looking at the public interface.
@LB:
I do miss C#'s regions for that. (#region #endregion)
@LB
I declare classes like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class my_class {
public:
    int hypothetical_public_member;

    my_class(int _value);
    void do_the_things();
private:
    int value;
    int some_other_members();

    void do_the_first_thing();
    void do_the_second_thing();
    void do_the_third_thing();
};

I like to put the public interface first so that someone looking at the header file doesn't have to scroll down through the private stuff - which they don't need to know - to find it.

I use struct for very simple classes which only (or mostly) use public members and are POD (or almost-POD). For example, this is a struct I used in a game (so that less code would need to be changed to port the game from SFML to, say, SDL):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct color {
    std::uint32_t  r, g, b, a;

    color(std::uint32_t _r, std::uint32_t _g, std::uint32_t _b, std::uint32_t _a = 255)
    : r(_r), g(_g), b(_b), a(_a)
    {
          // Do nothing.
    }

    sf::Color to_sfml()
    {
        return sf::Color(r, g, b, a);
    }
};
Last edited on
chrisname wrote:
I use struct for very simple classes which only (or mostly) use public members[...]


I use class/struct interchangeably--often whichever one saves a line. A lot of developers think it's a big deal or something to define a constructor in a "struct", etc..
I'm with moorecm, I just use struct or class based on which one is less typing. Which means I use struct 100% of the time...
1
2
3
4
5
class Blah : public Meh
{
public:
    void Bleh();
};
1
2
3
4
struct Blah : Meh
{
    void Bleh();
};
My Pet Peeve:

1
2
while(package!= 'A' && package!='a' && package!='B' && package!='b'&&
package!='C' && package!='c')


This just plain ugly & non scalable.

I much prefer a switch with a quit case & a default clause to handle bad input, with the whole thing in a bool controlled while loop.

With structs, I like to use them to create types to help achieve function overloading where there is only 1 member variable . For example with geometric classes where there are lots of points & double values. Point objects would still be classes, because they can be used on their own where public access isn't correct. That way I can have public access to the members of the struct, but have the structs as private members of the class. Think of an arc with centre points, start & end points, points on the arc, start & end angles, and various distances. I think it is important to always have the structs as private members in a class object.

With private variables, I like them at the start of the class for 2 reasons: I like to know straight away what member variables are in a class; I don't like seeing the use of private variables, in initialiser lists or otherwise, before the declaration of the variables.


@OP what about L"\0" and other variants?
@TheIdeasMan
In that case, I would do this:
1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
bool match(const T& key, const std::vector<T>& values, bool sorted = false)
{
    if (sorted)
        return std::binary_search(values.begin(), values.end(), key);
    return std::find(values.begin(), values.end(), key);
}

// ...

while (match(std::tolower(package), { 'a', 'b', 'c' }, true))
    // ... 

It might be a bit slower, especially for lots of unsorted data, but it's better IMO.
The best part is that without changing the code it will slowly get faster as compilers get better at optimizing abstracted code that uses the standard library :)
@chrisname

I meant that I see this sort of thing from newbies all the time, so my solution caters for them - templates are way too much for a newbies.

Also, this sort of thing is usually for a menu of some kind, or a basic calculator - so a switch inside a while seems perfectly OK IMO. The newbies code is only there to validate the input, I prefer to use the default clause for that. And it is scalable in the switch, rather than trying to get all the options on 1 LOC.
Your solution caters to those that insist on misusing the console as though it were a user interface ;)
Pages: 1234... 7