patterncppMinor
String Token Generator for ExpressGenGen
Viewed 0 times
expressgengengeneratortokenforstring
Problem
StringTokenGenerator is an interface (or abstract class) I've created to the specific task of generating a string token of a given character set. It allows appending characters to it and then it can be used to receive a string token. I wrote this for the second version of ExpressGenGen a code generator generator. Second version uses TDD as the development practice.At first there is the header only abstract class, and after that I have included it's implementations, and then Test class. I'm using Google Mock and Google Test for testing.
Note that I'm generating the
TestHelper. See here to see how it's done.What I want reviewed:
- Am I following C++/C++11 idioms correctly?
- Does it need a better API design?
- Is there better ways to utilize Google Mock and Google Test?
- Is it too much abstraction?
- Does this adhere to the const correctness principle?
- Am I following good object oriented design principles?
- Do I need to apply DRY principle somewhere in this code?
- Am I following good practices when It comes to TDD?
- Can the test cases be written better?
- Am I missing any corner cases in the test cases?
StringTokenGenerator.h
#ifndef _STRING_TOKEN_GENERATOR_H_
#define _STRING_TOKEN_GENERATOR_H_
//--------------------------------------------
// Has
//
//--------------------------------------------
// Inherits
//
//--------------------------------------------
// Uses
#include
//--------------------------------------------
class StringTokenGenerator {
public:
virtual void Append(char c) = 0;
virtual std::string GetToken() = 0 ;
virtual ~StringTokenGenerator() {}
private:
};
//-------------------------------------------
#endif // _STRING_TOKEN_GENERATOR_H_DoubleQuoteStringTokenGenerator.h
```
#ifndef _DOUBLE_QUOTE_STRING_TOKEN_GENERATOR_H_
#define _DOUBLE_QUOTE_STRING_TOKEN_GENERATOR_H_
//--------------------------------------------
// Has
//
//--------------------------------------------
// Inherits
#include "Stri
Solution
No names starting with underscore followed by uppercase:
A name like this:
Is using a reserved naming convention, and thus should not be used. You could safely be using just
Verbose comments can easily become visual pollution:
Your sentry comments / markers are way too verbose. Some are not even serving a purpose, as they mark empty sections:
That's a huge waste of lines and also quite distracting. Please trim them down.
Use
C++11 introduced
You can default it, which is a more up-to-date style:
Use
C++11 also introduced the very useful
You can add
Don't use pointers/dynamic-memory if you don't have to:
In your child classes (or implementations if you will), you are using a pointer to a string (
The use of pointers is usually associated with extending the lifetime of an object beyond its scope of declaration. Prefer declaring by value where viable.
A name like this:
_STRING_TOKEN_GENERATOR_H_Is using a reserved naming convention, and thus should not be used. You could safely be using just
STRING_TOKEN_GENERATOR_H.Verbose comments can easily become visual pollution:
Your sentry comments / markers are way too verbose. Some are not even serving a purpose, as they mark empty sections:
//--------------------------------------------
// Has
//
//--------------------------------------------
// Inherits
//
//--------------------------------------------
// Uses
#include
//--------------------------------------------That's a huge waste of lines and also quite distracting. Please trim them down.
Use
default for empty virtual destructor:C++11 introduced
default member functions. Instead of defining an empty virtual destructor, such as in:virtual ~StringTokenGenerator() {}You can default it, which is a more up-to-date style:
virtual ~StringTokenGenerator() = default;Use
override for overridden virtual methods:C++11 also introduced the very useful
override keyword, which can be applied to virtual methods overridden by a child class. Doing so will improve compiler diagnostics if you shadow a member function name by accident, and could potentially allow for some extra compile-time optimizations related to de-virtualization.You can add
override to any virtual method implemented by child classes of the StringTokenGenerator interface.Don't use pointers/dynamic-memory if you don't have to:
In your child classes (or implementations if you will), you are using a pointer to a string (
mToken). There doesn't seem to be any need for that, you could have just declared the string by value. That would also result in a slightly more performance efficient program.The use of pointers is usually associated with extending the lifetime of an object beyond its scope of declaration. Prefer declaring by value where viable.
Code Snippets
_STRING_TOKEN_GENERATOR_H_//--------------------------------------------
// Has
//
//--------------------------------------------
// Inherits
//
//--------------------------------------------
// Uses
#include <string>
//--------------------------------------------virtual ~StringTokenGenerator() {}virtual ~StringTokenGenerator() = default;Context
StackExchange Code Review Q#82141, answer score: 4
Revisions (0)
No revisions yet.