Answer the question
In order to leave comments, you need to log in
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');
}
получить_температуру_прибора(){
return прочитать_из_регистра('0x01')/100;
}
получить_параметр_прибора(имя_параметра){
switch(имя_параметра){
case 'температура':
ответ = прочитать_из_регистра('0x00');
break;
case 'статус':
ответ = прочитать_из_регистра('0x01')/100;
break;
}
return ответ;
}
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question