Skip to content

Piero Bosio Social Web Site Personale Logo Fediverso

Social Forum federato con il resto del mondo. Non contano le istanze, contano le persone

I've been musing on #OOP style.

Uncategorized
5 4 14
  • I've been musing on style. I have a question for the more experienced programmers out there.

    How do you decide when a class's method doesn't actually need to be a method, but instead could just be a regular function?

    I'm not talking about functions that need to be shared between classes. I'm talking about intentionally moving a method outside of a class, even though it will only ever be used by that class.

    For example, if I've got a parser-related function that is only needed by the parser, I will normally just put it in the parser class as a method. It keeps things neat, I can make it private and lock down the class. But I've never before stopped to think whether this is the correct thing to do every time.

    After all, (my language of choice) has the concept of units. I could just have some functions just as functions, and only have methods for things that directly need to update the class's properties.

    It would make things easier for testing, too. I wouldn't need to create an instance of a class just to test a specific function works as intended.

    But I don't know if it's the "right" way to do things in OO, or for that matter.

  • I've been musing on style. I have a question for the more experienced programmers out there.

    How do you decide when a class's method doesn't actually need to be a method, but instead could just be a regular function?

    I'm not talking about functions that need to be shared between classes. I'm talking about intentionally moving a method outside of a class, even though it will only ever be used by that class.

    For example, if I've got a parser-related function that is only needed by the parser, I will normally just put it in the parser class as a method. It keeps things neat, I can make it private and lock down the class. But I've never before stopped to think whether this is the correct thing to do every time.

    After all, (my language of choice) has the concept of units. I could just have some functions just as functions, and only have methods for things that directly need to update the class's properties.

    It would make things easier for testing, too. I wouldn't need to create an instance of a class just to test a specific function works as intended.

    But I don't know if it's the "right" way to do things in OO, or for that matter.

    @thelastpsion if its a pure function, just operating on the parameters and no side effects then i think its a good candidate for just a function. Otherwise it would probably be just a class method.

  • @thelastpsion if its a pure function, just operating on the parameters and no side effects then i think its a good candidate for just a function. Otherwise it would probably be just a class method.

    @thelastpsion and as you say it simplifies testing

  • I've been musing on style. I have a question for the more experienced programmers out there.

    How do you decide when a class's method doesn't actually need to be a method, but instead could just be a regular function?

    I'm not talking about functions that need to be shared between classes. I'm talking about intentionally moving a method outside of a class, even though it will only ever be used by that class.

    For example, if I've got a parser-related function that is only needed by the parser, I will normally just put it in the parser class as a method. It keeps things neat, I can make it private and lock down the class. But I've never before stopped to think whether this is the correct thing to do every time.

    After all, (my language of choice) has the concept of units. I could just have some functions just as functions, and only have methods for things that directly need to update the class's properties.

    It would make things easier for testing, too. I wouldn't need to create an instance of a class just to test a specific function works as intended.

    But I don't know if it's the "right" way to do things in OO, or for that matter.

    @thelastpsion my 2 cents:

    If it’s tightly coupled to the structure of the class or breaks the class’s encapsulation somehow, it goes on the class

    Otherwise, if you’re using a language that allows you to control what functions are exported separately from what class members are exported, then it depends on the tradeoff between easy testing and a clean interface.

    If it’s a private method then it’s generally best to not unit test it directly (in a perfect world). If it’s a separate function then that’s much easier to unit test.

    If there’s no way to make it clear to consumers of your module that this code is supposed to be internal, then moving it out of the class increases the chances of people depending on it when you don’t want them to, or it can make your interfaces more cluttered.

    Once you have unit tests for the thing, it will want to settle towards where you’ve put it. So if you could see this function one day becoming part of its own class, I would lean towards extracting and testing it separately early.

  • I've been musing on style. I have a question for the more experienced programmers out there.

    How do you decide when a class's method doesn't actually need to be a method, but instead could just be a regular function?

    I'm not talking about functions that need to be shared between classes. I'm talking about intentionally moving a method outside of a class, even though it will only ever be used by that class.

    For example, if I've got a parser-related function that is only needed by the parser, I will normally just put it in the parser class as a method. It keeps things neat, I can make it private and lock down the class. But I've never before stopped to think whether this is the correct thing to do every time.

    After all, (my language of choice) has the concept of units. I could just have some functions just as functions, and only have methods for things that directly need to update the class's properties.

    It would make things easier for testing, too. I wouldn't need to create an instance of a class just to test a specific function works as intended.

    But I don't know if it's the "right" way to do things in OO, or for that matter.

    @thelastpsion - I don't know Pascal, but in Python you have static and class methods which can be used for this sort of thing.

    Python conventions do encourage using plain methods rather than making everything a class by default though. You could maybe extract an inner module (rather than an inner class) if you had several such methods and wanted to encapsulate them from the rest of the class.

  • amoroso@oldbytes.spaceundefined amoroso@oldbytes.space shared this topic on

Gli ultimi otto messaggi ricevuti dalla Federazione
  • @AdoroIlGenio non lo so perché non uso quei siti, ma ho letto che con le VPN si aggira il blocco

    read more

  • @AdoroIlGenio Questa striscia a fumetti, composta da sei vignette disposte in una griglia 2x3, presenta due personaggi (uno con i capelli rossi e uno con i capelli scuri) che interagiscono in varie pose e ambientazioni. Nella vignette in alto a sinistra, il personaggio con i capelli rossi afferma: "PENSAVO SE DAVVERO VOGLIAMO AVERE UN BAMBINO, FORSE È MEGLIO ANDARE IN SVEZIA", mentre il personaggio con i capelli scuri chiede: "A CASA TUA??". Nella vignette in alto a destra, il personaggio con i capelli scuri risponde: "HAM, NON LO SO, PANNY, QUI IN INGHILTERRA HO I MIEI AMICI, MA QUALI SONO I PRO' DI ANDARE FIN LÌ?", e il personaggio con i capelli rossi replica: "BEH...". Nella vignette al centro a sinistra, il personaggio con i capelli rossi elenca vantaggi come "L'ASSISTENZA ALL'INFANZIA È QUASI GRATIS", "BEN 16 MESI DI CONGEDO PARENTALE" e "SUPPORTO MEDICO PER OGNI BAMBINO", accanto a una piccola icona di un bambino. Nella vignette al centro a destra, il personaggio con i capelli scuri dice: "BOI, NON SONO CONVINTO...", mentre il personaggio con i capelli rossi aggiunge: "E poi...". Nella vignette in basso a sinistra, il personaggio con i capelli rossi spiega: "NON DEVI USARE VOTI O INVIARE DOCUMENTI PER ACCEDERE AI SITI PER PERSONE SU INTERNET", con un'icona di una busta. Nella vignette in basso a destra, un personaggio con abiti blu esclama: "POFF NÄER SKA VI OKA??" e include il testo "* QUANDO SI PARTE??" in basso a destra. Il creatore è identificato come "TARDAASA COMICS" nell'angolo in basso a sinistra.

    Fornito da @altbot, generato localmente e privatamente utilizzando Qwen3-Vl:8b

    🌱 Energia utilizzata: 0.632 Wh

    read more

  • read more

  • Il SI’ all’attacco della Costituzione


    @giornalismo
    articolo21.org/2026/03/il-si-a…
    Ultimi dieci giorni di campagna referendaria. Non è difficile prevedere quanta violenza, aggressività, volgarità, falsità il governo Meloni e i suoi tanti lacché, presenti anche nel mondo dell’informazione, scateneranno contro il NO a quell’inganno che

    read more

  • Thanks for the nice wishes!

    Today, we mostly explored Franconia. 🥨 🇩🇪

    read more

  • @Uilebheist @Alberto eh ricordo. Purtroppo non ho ora vicino queste storie... Vien voglia di rileggerle prima della fine del mondo

    read more

  • "Fai le cose difficili
    quando sono facili,
    e inizia le grandi cose
    quando sono piccole."

    (Lao Tzu)

    read more

  • @G3rt se è vero quel che dice, indica che la società americana si è trasformata in una società fondamentalista religiosa votata all'Armageddon, molto più pericolosa di 100 Isis, che vuole la fine del mondo e ha sotto il proprio controllo l'intero arsenale nucleare

    Spero di no
    Quel che credo è che sia stato fatto un rovesciamento, se non in colpo di stato in America, e vedremo alle prossime elezioni di mid term se e cosa è vero, se ad esempio dovessero perdere e come reagiranno

    read more
Post suggeriti