A well-known programmer planted a lot of doubt in me by telling me that the following code can be achieved in the same way without the need of if
calling the abstract class.
abstract class Clases
{
public abstract int[] Atributos();
}
class Guerrero : Clases
{
public int _StatFuerza = 10;
public int _StatDestreza = 10;
public int _StatConstitucion = 10;
public int _StatInteligencia = 10;
public int _StatSabiduria = 10;
public int _StatCarisma = 10;
public int _StatHealthPower = 10;
public override int[] Atributos()
{
int[] atr = new int[6];
atr[0] = _StatFuerza;
atr[1] = _StatDestreza;
atr[2] = _StatConstitucion;
atr[3] = _StatInteligencia;
atr[4] = _StatSabiduria;
atr[5] = _StatCarisma;
atr[6] = _StatHealthPower;
return atr;
}
}
class Mago : Clases
{
public int _StatFuerza = 15;
public int _StatDestreza = 15;
public int _StatConstitucion = 15;
public int _StatInteligencia = 15;
public int _StatSabiduria = 15;
public int _StatCarisma = 15;
public int _StatHealthPower = 15;
public override int[] Atributos()
{
int[] atr = new int[6];
atr[0] = _StatFuerza;
atr[1] = _StatDestreza;
atr[2] = _StatConstitucion;
atr[3] = _StatInteligencia;
atr[4] = _StatSabiduria;
atr[5] = _StatCarisma;
atr[6] = _StatHealthPower;
return atr;
}
}
That's what I call that class
public void Estadisticas(int _mainEXP, string especializacionSelec, string razaSelec)
{
int bonusClassFuerza;
int bonusClassDestreza;
int bonusClassConstitucion;
int bonusClassInteligencia;
int bonusClassSabiduria;
int bonusClassCarisma;
int bonusClassHp;
if (especializacionSelec == "Guerrero")
{
int[] especialidad = new Guerrero().Atributos();
bonusClassFuerza = especialidad[0];
bonusClassDestreza = especialidad[1];
bonusClassConstitucion = especialidad[2];
bonusClassInteligencia = especialidad[3];
bonusClassSabiduria = especialidad[4];
bonusClassCarisma = especialidad[5];
bonusClassHp = especialidad[6];
}
else if (especializacionSelec == "Mago")
{
int[] especialidad = new Mago().Atributos();
bonusClassFuerza = especialidad[0];
bonusClassDestreza = especialidad[1];
bonusClassConstitucion = especialidad[2];
bonusClassInteligencia = especialidad[3];
bonusClassSabiduria = especialidad[4];
bonusClassCarisma = especialidad[5];
bonusClassHp = especialidad[6];
}
}
Is this the correct way to program?
Can the code be further simplified? .
To what extent does the rule of not repeating apply? .
Does the use of ifs affect the correct development of the code?
Search for information related to the topic and thanks to this example I learned how to create abstract classes.
It is difficult to define what is correct, for this you have to base yourself on software quality criteria in order to arrive at the best solution for your problem. What if one could say that it is okay to use abstract classes to define a base class type.
You can always simplify the code further, in this case you could overload the Stats method to generate logic for warriors and mages.
You have to try to always apply it, but it depends on the experience of the programmer in this case to see that there is an order and legibility in the code.
It will depend on who programs. if a method is generated from hundreds of lines of code with multiple IFs, there is a problem. the important thing is to analyze if there is any design pattern that helps me to program in a more modular way.
If we stick to object-oriented programming principles, lines of code that include IFs to check a property value of an object "break" the encapsulation principle.
If we see it as sources of errors, it will depend on the programmer to have the code well implemented so that the ifs are fulfilled and an error is not generated when asking for a property of an object that does not exist or forgetting that it is not the same " hello" than "Hello" to ask for string equality and omit a block of instructions for that. Etc.
In terms of performance, compilers see If / Else as a jump to another block of code. this operation is very standard and well optimized so that it is easily fulfilled for any processor, so in this area it is NO.
Warning: It's a bit confusing to talk about classes (warrior, mage, etc.) and C++ classes, I've tried to make it clear by marking it in parentheses. There are also times when I refer to both at the same time...
Using the abstract class allows you to have separate code. Each class (Wizard, Warrior, and whatever else there is) can go in a separate file, so you can keep your code tidier.
The more functions you add (now you have only shown us
Atributos
orEstadisticas
, but surely there are more or there will be more), the longer the file would be if you did it withif-else
. Imagine that you have 5 classes (warrior, mage), each class needs 10 functions, each function is at least 15 lines of code (and that's not much). They are5 * 10 * 15 = 750
lines in the same file, and it seems little to me that it has 10 functions and 15 lines. Imagine how that will scale as you need more and more features or classes (warrior, mage), etc.Also, it will be easier for you to make mistakes with the ones
if-else
in the same file than if you have 5 different files (one per class (warrior, mage)).On the other hand, by having the abstract class, you can see at a glance the essential functions of each class (warrior, mage, etc), because they are defined there.
If you also use inheritance, you can also define functions that are common to all classes, so you also avoid having to repeat code that is the same for each type of class (warrior, mage). So you can also define sub-classes (Wizard could be: sorcerer, druid, dark wizard, necromancer, etc., Warrior could be: Archer, soldier, mercenary, etc.).
In the end you have a program with many files, but if you order them well there is no problem, the files are shorter and clearer, each one refers to one thing. If you change any characteristic of the warrior, you only have to open a file and everything you change will affect only the warrior, if you have to search them
if-else
you have to be careful not to change another.There is no rule to always do it this way. It is a good idea for what you have in hand, but in other cases it will be convenient to use
if-else
. What you have to do, and it's part of the programmer's job, is to know the tools that the programming language gives you, know your needs, and then make the best decision you can about what to use. You can always take a step back and change it, but making the best possible decision from the first moment avoids having to change everything later, because you will waste time making changes that at the user level will not be noticed, instead of being able to add improvements to the program .I could not tell you, it will depend on how all the code is, from what you have shown us, it seems quite simple, but surely it is more complex in the background. The idea of using inheritance can be useful, but you'll have to see if it applies to your case or not.
Quite simply, common sense will tell you. If you're going to lose code readability by not repeating, then you might want to repeat, keep in mind that you probably won't be programming alone. But yes, always try not to repeat code, it is one of the points on which more emphasis is placed, and it seems correct.
It
if
is one more tool provided by the programming language, you have to use it when it makes sense, there is no more. The thing is to know the other tools to know when theif
is the best and when you will have to use aswitch
,for
,while
,do-while
,clases
,herencia
, etc.To solve the problem
if
you must do the following.The abstract class is created
The class is called as follows
taking advantage of the fact that we pass it as a list we can fill a
datagridview
with this new informationIf we want to extract the information about any of them,
Razas
we use one of the triggers that the datagridview offers us, such as when clicking on the cell it triggers a certain void, and taking advantage of the fact that we already clicked on one that brings us the reference of in What position in the table are we in so that it can access the specific value and thus return the data referring to our search parameter.Capture of the final result.
I answer myself because it is a personal way to continue growing and so that someone else who is in the same situation as me and has come across this publication by chance has a little more idea of how to solve some of their problems.