Osa 6

Moniosaiset ohjelmat

Ohjelmointia harjoitellessa kehittyy luomiensa ohjelmien (sekä muiden luomien ohjelmien) tarkastelussa. Tässä osassa syvennyttiin listojen käyttöön sekä harjoiteltiin käyttöliittymän eriyttämistä sovelluksen logiikasta.

Seuraava katkelma on Edsger W. Dijkstran artikkelista On the role of scientific thought.

Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. It is, that one is willing to study in depth an aspect of one's subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. We know that a program must be correct and we can study it from that viewpoint only; we also know that it should be efficient and we can study its efficiency on another day, so to speak. In another mood we may ask ourselves whether, and if so: why, the program is desirable. But nothing is gained - on the contrary! - by tackling these various aspects simultaneously. It is what I sometimes have called "the separation of concerns", which, even if not perfectly possible, is yet the only available technique for effective ordering of one's thoughts, that I know of. This is what I mean by "focusing one's attention upon some aspect": it does not mean ignoring the other aspects, it is just doing justice to the fact that from this aspect's point of view, the other is irrelevant. It is being one- and multiple-track minded simultaneously.

Dijkstran kirjoitelman ydinajatus on se, että ohjelmissa olevat huolenaiheet tulee erottaa toisistaan — olemme tehneet juuri tätä olio-ohjelmoinnissa sekä käyttöliittymän ja sovelluslogiikan erottamisessa. Kukin huolenaihe on jaettu omaan osaan. Tätä voidaan tarkastella myös ohjelmien vastuiden näkökulmasta. Robert "Uncle Bob" C. Martin kuvaa blogissaan termiä "single responsibility principle" seuraavasti.

When you write a software module, you want to make sure that when changes are requested, those changes can only originate from a single person, or rather, a single tightly coupled group of people representing a single narrowly defined business function. You want to isolate your modules from the complexities of the organization as a whole, and design your systems such that each module is responsible (responds to) the needs of just that one business function.

[..in other words..] Gather together the things that change for the same reasons. Separate those things that change for different reasons.

Selkeys ohjelmaan luodaan sopivalla ohjelman rakenteella sekä nimeämiskäytänteiden seuraamisella. Opit ohjelmoidessasi että jokaiselle ohjelman osalle voidaan määritellä yksi selkeä vastuu.

Vastaa vielä lopuksi seuraavaan kuudennen osan osaamistavoitteita tarkastelevaan itsearviointikyselyyn.

Pääsit aliluvun loppuun!

Muistathan tarkistaa pistetilanteesi materiaalin oikeassa alareunassa olevasta pallosta!