objective c - Why is public/private such an important programming aspect? -
why in programming languages ability have private , or public methods/functions classes , properties?
does make of difrence let's say.. have classes, methods , properties public?
i know if have methods, classe ans properties set private nothing work. @ least know much.
but distinction between 2 matter? what's big deal if 1 class knows class "that meant private" exists?
when make public, enter contract user class: "hey, is, offer, use or not." changing public interface expensive, because have change code using public interface, too. think of developer of framework cocoa used thousands of developers. if change 1 public methods, example removing one, thousands of apps break. have changed, too.
so making public means cannot change anymore. (you can, people angry @ 1 point.)
let's think of having class implementing list. there method sort it: sortlistwithkey. make public because want users of class sorted list. good.
there several algorithms sorting. let's say, implement 1 needs calculate meridian (the middle element). need method internally sorting algorithm. enough, implement privately. changing whole structure of data holding including implemented sorting algorithm no problem , not break existing code using class.
but if made meridian method public (remember: implemented it, because needed internally), still have keep it, new sorting algorithm not need it. cannot remove anymore, new structure hard (and/or expensive) keep method.
so make part of implementation public useful users, no more. otherwise shackle yourself.
Comments
Post a Comment