Skip to main content
2 of 7
added 410 characters in body; added 4 characters in body
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60

##Enumerated Constants (enum)

The automated code generation process builds a number of domain objects that correspond to constructs of the StackOverflow API.

The most useful of these types, and the only one of interest to those who are not users of Visual Studio, are the enumerated constants, or enum, that are used both as route input parameters and as members of the response objects.

The advantage of using enums is the elimination of magic strings scattered about your codebase. If, for some reason, the API changes such that a particular string constant is no longer valid, this will become readily apparent in tests and can be resolved easily. With string constants, e.g. route.sort = 'active' become a bit more problematic as you will not get 'compile' errors, only 404 or 500 from the API.

If you code route.sort = Soapi.Domain.PostSort.active;, when Kevin responds to a valid concern and changes the value to 'activity' you will recieve JavaScript runtime errors, 'compile errors', declaring that Soapi.Domain.PostSort.active is undefined and then you can easily and safely update your codebase with a global S&R of Soapi.Domain.PostSort.active to Soapi.Domain.PostSort.activity.

There are other benefits of the use of enum, but those are left as an excercise to those that are not aware of them.

Currently the defined enums are as follows:

Soapi.Domain.SortOrder
  {desc:'desc',asc:'asc'}

Soapi.Domain.PostSort
  {activity:'activity',views:'views',creation:'creation',votes:'votes'}

Soapi.Domain.UserType
 {anonymous:'anonymous',unregistered:'unregistered',registered:'registered',moderator:'moderator'}

Soapi.Domain.PostType
  {question:'question',answer:'answer'}

Soapi.Domain.CommentSort
  {creation:'creation',votes:'votes'}

Soapi.Domain.QuestionSort
 {activity:'activity',votes:'votes',creation:'creation',featured:'featured',hot:'hot',week:'week',month:'month'}

Soapi.Domain.QuestionsByIdTimelineTimelineType
  {question:'question',answer:'answer',comment:'comment',revision:'revision',votes:'votes'}

Soapi.Domain.RevisionType
  {single_user:'single_user',vote_based:'vote_based'}

Soapi.Domain.TagSort
  {popular:'popular',activity:'activity',name:'name'}

Soapi.Domain.UserSort
  {reputation:'reputation',creation:'creation',name:'name'}

Soapi.Domain.UsersByIdFavoritesSort
  {activity:'activity',views:'views',creation:'creation',added:'added',votes:'votes'}

Soapi.Domain.UsersByIdTimelineTimelineType
 {comment:'comment',askoranswered:'askoranswered',badge:'badge',revision:'revision',accepted:'accepted'}

Users of Visual Studio will get the most benefit from the use of the defined enum types as they have the luxury of intellisense code completion, which will be covered in detail in the Intellisense Support post.

alt text http://soapi.info/images/soapi.js.enums1.png

soapi.js also defines one enum, Sites, that enumerate the supported target sites, eliminating another magic string from your codebase in the most critical call, factory instantiation.

Soapi.Sites
   { stackoverflow: "api.stackoverflow.com", serverfault: "api.serverfault.com", superuser: "api.superuser.com", metastackoverflow: "api.meta.stackoverflow.com", stackapps: "api.stackapps.com" }
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60