Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/message/MQMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const string MQMessage::PROPERTY_TRANSACTION_CHECK_TIMES = "TRANSACTION_CHECK_TI
const string MQMessage::PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS = "CHECK_IMMUNITY_TIME_IN_SECONDS";

const string MQMessage::KEY_SEPARATOR = " ";

static const string EMPTY_STRING = "";
Copy link
Contributor

@jovany-wang jovany-wang Jan 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think defining an empty str as a static global var is the best approach.
Maybe we could add a new file constant.h like this:

class Constants {
public:
    static const string EMPTY_STRING = "";
};

Any way, we should avoid to use global variables and global static variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jovany-wang the visibility of global static variables is only in this file. i think depend on other file is unnecessary for only this one. if we use EMPTY_STRING in other files, it's ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to move it to constant.h, let's use return ""; directly?
Because I don't think we should use a global static variable in such case.:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to move it to constant.h, let's use return ""; directly?
Because I don't think we should use a global static variable in such case.:)

@jovany-wang now, we return a reference, and return a temp object is a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifplusor Thanks, I see.
So if the method may be return a temp variable, it is pretty make sense to declare it as string MQMessage::getProperty(const string& name);.

If the method always return his member variables, we could return a const ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jovany-wang i don't change method signature, we can discuss it.


//<!************************************************************************
MQMessage::MQMessage() { Init("", "", "", 0, "", true); }

Expand Down Expand Up @@ -109,9 +112,9 @@ void MQMessage::setPropertyInternal(const string& name, const string& value) {

const string & MQMessage::getProperty(const string& name) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifplusor hi, if we can change getProperty return string but not string& ? because i don't think there is a need to return reference string for getProperty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonnxu I think 'const string&' is ok. Here, we need return a read-only object or a new one, and return a reference don't allocate a new object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of them are fine.
It will not affect efficiency too much.

map<string, string>::const_iterator it = m_properties.find(name);
if(it == m_properties.end()){
return "";
}else{
if (it == m_properties.end()) {
return EMPTY_STRING;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch for the code style.

return it->second;
}
}
Expand Down