Sub topic 2
Functional requirements: what the system does
This chapter assumes you know how client-server applications and web APIs work, and that you can read short Python snippets. It covers what a functional requirement is, how to write one that can be tested, and how to tell one apart from a quality target.
What a functional requirement is
A functional requirement (FR) states a behavior the system must perform. It names who triggers the behavior, what the system does in response, and what result can be observed. "A user can submit a long URL and receive a short URL" is an FR. You can check it by doing it.
Functional requirements answer "what does the system do?" Non-functional requirements answer "how well does it do it?" The next chapter covers those. Here it is enough to remember that an FR is either satisfied or not, while a quality target has a measured value.
Writing one requirement
A well-formed FR has three parts:
- Actor: who or what triggers the behavior (a user, an admin, a scheduled job, another service).
- Behavior: one action, stated in the active voice.
- Observable outcome: what the actor can see or verify afterwards.
Two templates cover most cases:
- "A <actor> can <action> so that <outcome>."
- "When <trigger>, the system <response>."
Keep each requirement to one behavior. "Users can create and delete short URLs" is two requirements. If one half is cut or delayed, a combined statement cannot be tracked. Give each requirement a stable ID such as FR-1 so that tests, tickets and design notes can refer to it.
Example: a URL shortener
The requirements document from the topic plan lists three FRs:
service: url-shortener
functional:
- id: FR-1
text: A user can submit a long URL and receive a short URL.
- id: FR-2
text: Requesting a short URL redirects to the original long URL.
- id: FR-3
text: A user can delete a short URL they created.
out_of_scope:
- Click analytics
- Custom domainsEach one names an actor, one behavior and a result. The out_of_scope list matters as much as the FRs. It records what a reviewer should not expect, which prevents later arguments about missing features.
Cover the unhappy paths
Most FR lists describe only the success case. Real systems spend much of their code on failure cases, and those are functional behavior too. For each FR, ask what happens when the input is bad, the target does not exist, or the actor is not allowed to act.
| Base requirement | Missing case | Added requirement |
|---|---|---|
| FR-1 create | Input is not a valid URL | FR-4: Submitting a string that is not an http or https URL is rejected with a validation error, and no short URL is created. |
| FR-2 redirect | Short code does not exist | FR-5: Requesting an unknown or deleted short URL returns a "not found" response. |
| FR-3 delete | Caller is not the creator | FR-6: A user cannot delete a short URL created by another user. The request is refused and the short URL remains. |
Notice that FR-6 is a functional rule about permissions: who may do what. It differs from a security quality target such as "traffic is encrypted", which belongs with the non-functional requirements.
Turn requirements into acceptance criteria
A requirement is useful only if you can tell when it is met. An acceptance criterion states a concrete check. The simplest form is a test. The sketch below uses a minimal in-memory implementation and one check per FR. The code is not a design. It shows that each FR maps directly to something you can run.
import secrets
import string
ALPHABET = string.ascii_letters + string.digits
class Shortener:
def __init__(self):
self.urls = {} # code -> (long_url, owner)
def create(self, owner, long_url):
if not long_url.startswith(("http://", "https://")):
raise ValueError("invalid URL")
code = "".join(secrets.choice(ALPHABET) for _ in range(7))
while code in self.urls: # regenerate on the rare collision
code = "".join(secrets.choice(ALPHABET) for _ in range(7))
self.urls[code] = (long_url, owner)
return code
def resolve(self, code):
if code not in self.urls:
raise KeyError("not found")
return self.urls[code][0]
def delete(self, owner, code):
long_url, creator = self.urls[code] # KeyError if unknown
if creator != owner:
raise PermissionError("not the creator")
del self.urls[code]
def test_fr1_fr2_create_then_redirect():
s = Shortener()
code = s.create("alice", "https://example.com/a/very/long/path")
assert s.resolve(code) == "https://example.com/a/very/long/path"
def test_fr3_creator_can_delete():
s = Shortener()
code = s.create("alice", "https://example.com")
s.delete("alice", code)
try:
s.resolve(code)
assert False, "expected KeyError"
except KeyError:
pass
def test_fr4_invalid_url_rejected():
s = Shortener()
try:
s.create("alice", "not a url")
assert False, "expected ValueError"
except ValueError:
assert s.urls == {}
def test_fr6_other_user_cannot_delete():
s = Shortener()
code = s.create("alice", "https://example.com")
try:
s.delete("bob", code)
assert False, "expected PermissionError"
except PermissionError:
assert s.resolve(code) == "https://example.com"
for test in (test_fr1_fr2_create_then_redirect, test_fr3_creator_can_delete,
test_fr4_invalid_url_rejected, test_fr6_other_user_cannot_delete):
test()
print("all functional checks passed")If you cannot write a check like this for a requirement, the requirement is probably vague. "The system handles bad URLs gracefully" fails that test. FR-4 passes it.
Choosing the level of detail
Requirements sit at different levels. "Users can manage their short URLs" is a goal. "A user can delete a short URL they created" is a requirement. "The delete endpoint is DELETE /urls/{code} and returns 204" is a design decision. Write requirements at the middle level.
- State what must happen, not how. Naming an endpoint, table or technology too early removes options the design phase should weigh.
- Go detailed enough that two engineers would build the same observable behavior.
- Specify an interface only when it is a real constraint, such as an existing client that cannot change. Record such cases as constraints, not as FRs.
Common errors
- Hidden quality targets. "The redirect is fast" looks functional because it names a behavior, but "fast" is a quality. Split the statement: the FR says that a redirect occurs, and a separate NFR gives the latency figure.
- Solution disguised as requirement. "Store URLs in a relational database" describes a design choice. The underlying FR is that created URLs can be retrieved later.
- Compound requirements. Words like "and", "or" and "as well as" often mean two behaviors in one line.
- Missing actors. An FR with no actor ("URLs are deleted") hides who may trigger the action and who must be refused.
- Untracked scope. A feature mentioned in a meeting but absent from both the FR list and the out-of-scope list will be argued over later.
Exercise
Classify each statement as functional (F), non-functional (N), or mixed (M). If mixed, split it. (a) "An admin can suspend an account." (b) "Search results appear within 200 ms for 95% of queries." (c) "A user can export their orders as CSV, and the export finishes in under 10 seconds."
Show answer
(a) F. It has an actor, one behavior and an observable outcome. (b) N. It is a latency target stated as a percentile, with no new behavior. (c) M. Split it into FR: "A user can export their orders as CSV," and NFR: "Exports complete in under 10 seconds" (state the percentile and data size when you refine it).
Exercise
A draft FR reads: "Users can delete short URLs." Rewrite it as one FR with an actor, behavior and outcome, then add two unhappy-path requirements.
Show answer
Rewrite: "A user can delete a short URL they created, after which requesting it returns 'not found'." Unhappy paths, for example: "Deleting a short URL created by another user is refused and the short URL remains" and "Deleting a short URL that does not exist returns a 'not found' response." Any two cases covering permission, missing target or bad input are acceptable.