nus-cs2103-AY2223S1 / forum

14 stars 1 forks source link

Lots of guard clauses and activity diagrams #446

Open parnikkapore opened 1 year ago

parnikkapore commented 1 year ago

What's the best practice on drawing an activity diagram for a process with lots of guard clauses? Combining them into a single diamond loses information about the order that the checks are done (important if more than one fails), while writing multiple nested diamonds leads to large diagrams that look unwieldy.

parnikkapore commented 1 year ago

Say, how to diagram this?

String a(int n) {
  if (a < -10) return "!a";
  if (a > 10)  return "!b";
  if (a < 0)   return "-";
  if (a == 0)  return "0";
  if (a > 0)   return "+";
  else         return "?";
}
teekaytai commented 1 year ago

I don't think capturing the order of checks is a focus of activity diagrams. Instead, activity diagrams require that exactly one condition is true. So for the example above, you cannot copy the conditions verbatim. You would need to separate it out into [a < -10], [-10<=a<0], [a==0], [0<a<=10] and [10>a]. This way, we don't need to be concerned about the actual order the checks are being done in the code

image

parnikkapore commented 1 year ago

I think that would be the correct way too; in the context of the tp, we would have something like [index between 1 and INT_MAX is present but is larger than highest index in list] on one of the branches

damithc commented 1 year ago

I don't think capturing the order of checks is a focus of activity diagrams. Instead, activity diagrams require that exactly one condition is true. So for the example above, you cannot copy the conditions verbatim. You would need to separate it out into [a < -10], [-10<=a<0], [a==0], [0<a<=10] and [10>a]. This way, we don't need to be concerned about the actual order the checks are being done in the code

image

Good answer @teekaytai