• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to understand the degrees_cw in The Art of Readable Code  RSS feed

 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the book of <<the art of readable code>>, page 16, there's a suggestion for naming variables
angle -> degree_cw

what i want to know is what does the "cw" mean? thanks.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at that page in Google Books I assume it's the units it's using.
The other examples given have a timing variable ending in _secs, a cache size ending in _mb and a download throttle ending in _kbps.

I will say I disagree with the whole underscore thing.
What's wrong with delaySeconds?

And I also dislike picking out instance variables by things like sticking an underscore at the start.
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Looking at that page in Google Books I assume it's the units it's using.
The other examples given have a timing variable ending in _secs, a cache size ending in _mb and a download throttle ending in _kbps.

I will say I disagree with the whole underscore thing.
What's wrong with delaySeconds?

And I also dislike picking out instance variables by things like sticking an underscore at the start.

yes, you are right, at least in java world we nearly never use underscore for a variable name(prefer camel case), maybe except for the static final ones.

but anyway, do you know the "cw"?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drac yang wrote:
yes, you are right, at least in java world we nearly never use underscore for a variable name(prefer camel case), maybe except for the static final ones.


In Java static final things are usually ALL CAPS.

drac yang wrote:
but anyway, do you know the "cw"?


Nope, sorry.
I can't think, in terms of angle, what that is shorthand for...which sort of highlights the drawbacks of the technique.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh the irony. I guess these authors hadn't read the "Naming" chapter in Uncle Bob Martin's book, Clean Code, where he suggests that readable names are pronounceable. How do you pronounce degrees_cw? "degrees see double you"? "degrees see eew"? "degrees cow"?

You left out an important part of the context. The full entry in the table that you cited is this:

Function parameterRenaming parameter to encode units
Rotate(float angle )angle → degrees_cw

I suspect "cw" stands for "clockwise" as opposed to "ccw" which would stand for "counter-clockwise" although this is not exactly a unit. "degrees" is already a unit. The little info wart, cw, is supposed to tell the reader in which direction the rotation is made although, as you can see, it fell short of that goal.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I submit that the method could have been more readable if you refactored and designed the method call like this instead:

Rotate(float degrees, int direction)

In Java, I would have this:

public void rotate(double degrees, Direction dir)

where Direction is an enum with two values: CLOCKWISE and COUNTER_CLOCKWISE.  I think those choice of names makes that code reasonably readable and unambiguous.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!