Disclaimer: I am not very experienced neither in C#, programming, or clean code.
We would still want a function since we’re calling it on a different class. And we don’t want the Tile to be the one instantiating Towers.
The TryCreateTower()
is on option. On my (beginner) level of programming, I’d rather see all the info in the call instead of having to look inside the code to see what it checks for. This might look something like
if(Bank.HaveFundsFor(selectedBuilding.GetCost()){
Bank.DecreaseFunds(selectedBuilding.GetCost());
Factory.SpawnBuiding(selectedBuilding);
}
Yes, this is much more words than TryCreateTower()
, but it also states exactly what is going on.
There’s more to comment on this approach in regard to clean code.
Firstly, the course introduces features (or ideas) mid-lecture. Thus, we don’t have a clear overview from the start of the project, which makes planning the code architecture not very effective. Rather, we go along with the need of the lectures, and then maybe sometimes refactor (on our own if we want, or by prompts of the course material).
Secondly, if we focus on things other than the naming (which I agree is very important), then there would be more work needed to make the code for this course cleaner.
If we just take the example of the Waypoint.OnMouseDown() that should lead to the Tower class checking with the Bank class and then possibly Instantiating here are a couple of thoughts:
- The Waypoint should not need to know of any Tower or Bank, it should just send an Event that it was clicked on;
- We would probably have a Game State Machine that would enable filtering clicks in different states
- We would have a class that holds data on all possible buildings that we can build (Scriptable Objects)
- We would have a UI that changes based on what we selected to build and based on our available credits/currency
- We would want most of these to have as little knowledge and dependency on each other to have them be as decoupled as possible since it makes changes to the system much easier
Again, I am not very knowledgeable about this. And although it sometimes takes a bit more classes and events to setup up initially, the further we go along with the project scope, it stays “easy and elegant” to build upon such a system.