H
H
hypersib2020-11-06 15:08:55
Programming
hypersib, 2020-11-06 15:08:55

Does it make sense to create many simple methods?

How correct or incorrect it is to create many simple methods that essentially differ only in a few points, for example:

I have a class and it has a
read_from_register() method

how correct it is to create wrapper methods for easier data access, for example:

получить_статус_прибора(){
  return прочитать_из_регистра('0x00');
}


or

получить_температуру_прибора(){
  return прочитать_из_регистра('0x01')/100;
}

Will not a lot of unnecessary memory be spent or look stupid?

Or is it more correct to write something generic like:
получить_параметр_прибора(имя_параметра){
  switch(имя_параметра){
    case 'температура':
      ответ = прочитать_из_регистра('0x00');
    break;
    case 'статус':
      ответ = прочитать_из_регистра('0x01')/100;
    break;
  }
  return ответ;
}


Interesting opinion from all sides and the maintenance of the code and its "beauty", as well as in terms of saving resources.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-11-06
@hypersib

Many concrete wrapper methods are better than a big switch. maybe the calling code almost always knows what it is reading. Excessive memory will not be spent much - very often the compiler can inline this.
The option with a switch is both less efficient and less beautiful.

A
Alan Gibizov, 2020-11-06
@phaggi

Are the classes not suitable?

прочитать_из_регистра = print

class Device (object):
    def __init(self):
        pass
    
    def temperature(self):
        return прочитать_из_регистра('0x00')
    
    def status(self):
        return прочитать_из_регистра('0x01')


device = Device()
device.status()
device.temperature()

0x01
0x00

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question