Skip to content

chore(Docs): Replaced instances if - throw to require() where applicable.#2207

Merged
chriseth merged 4 commits intodevelopfrom
wski-develop
May 3, 2017
Merged

chore(Docs): Replaced instances if - throw to require() where applicable.#2207
chriseth merged 4 commits intodevelopfrom
wski-develop

Conversation

@chriseth
Copy link
Contributor

@chriseth chriseth commented May 2, 2017

Fixes #2134 fixes #2118

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't 0.4.10 be enough here for transfer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's keep it there, people should upgrade.


if (balanceOf[_to] + _value < balanceOf[_to])
throw;
require(balanceOf[_to] + _value >= balanceOf[_to]);
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 we should have parentheses here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are the FAQ, hey are horribly outdated.

write your contract using a pattern where the recipient can withdraw Ether instead.
3. Sending Ether can also fail because the execution of the recipient contract
requires more than the allotted amount of gas (explicitly by using ``throw`` or
requires more than the allotted amount of gas (explicitly by using ``revert`` or
Copy link
Contributor

@axic axic May 2, 2017

Choose a reason for hiding this comment

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

Perhaps extend that to:
"explicitly by using revert, require or assert"

// called incorrectly. But watch out, this
// will currently also consume all provided gas
// (this is planned to change in the future).
require(msg.sender == chairperson || !voters[voter].voted);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add parentheses to the left.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also replace || with &&.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep :-)


// Revert the call if the bidding
// period is over.
require(now < auctionStart + biddingTime);
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses wouldn't hurt here. Also <=.


// If the bid is not higher, send the
// money back.
require(msg.value <= highestBid);
Copy link
Contributor

Choose a reason for hiding this comment

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

<= to >

throw; // auction did not yet end
if (ended)
throw; // this function has already been called
require(now >= auctionStart + biddingTime); // auction did not yet end
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses and >.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really don't like to add parentheses around all operations. This is not javascript, it would make no sense to have (now >= auctionStart) + biddingTime...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add them here, though, because it is one of the first examples.

Copy link
Contributor

Choose a reason for hiding this comment

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

I know the type check would catch it if the precedence resolution would be broken, but I don't think it is bad practice having parentheses.

throw;
}
require(
_values.length == length &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing parentheses

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we should do that...
I will change it into three separate requires for readability.

seller = msg.sender;
value = msg.value / 2;
if (2 * value != msg.value) throw;
require(2 * value == msg.value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses could help readability here too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually don't think so. it is a simple mathematical equation, nobody would add parentheses there.

function confirmPurchase()
inState(State.Created)
require(msg.value == 2 * value)
condition(msg.value == 2 * value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses for readability :)

// block the refund.
if (!buyer.send(value) || !seller.send(this.balance))
throw;
// block the refund - the withdraw pattern should be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe make the withdraw patter comment more dominant, with putting it on a new line: Note: the with...

@chriseth chriseth merged commit 4af0451 into develop May 3, 2017
@axic axic deleted the wski-develop branch May 3, 2017 10:31
potomak added a commit to issuehunter/issuehunter that referenced this pull request Aug 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce "require()" and "assert()" into documentation

2 participants