First Function

I have completed my first function:

Header File

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void InitGame();

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives; 
};

CPP File

{
  Super::BeginPlay();
    
  // Welcomeing The Player
  PrintLine(TEXT("Welcome to Bull Cow!"));
  PrintLine(TEXT("Guess the 4 letter word!")); // Magic number remove
  PrintLine(TEXT("Press enter to mooove forward"));

  InitGame();// Setting Up Game


  // Prompt Player Guess

}

...

void UBullCowCartridge::InitGame()
{
  HiddenWord = TEXT("word"); 
  Lives = 6;
}

Interesting note

I can see the use for the “One use per function” rule. In Game terms, lets say you create a “movement” code. Movement, it would seem, is a class of some sorts. This could include individual functions walk, run, crawl, swim, jump, ect. But say you create another program that only utilizes swim or crawl? Copy and paste your functions, declare them as necessary, and you are off to the races.

If all were under one complex function, it would be harder to catch any issues that may crop up between the interplay of these actions. What if various uses of the one function were the problem? The whole function would still not work until properly all uses were dealt with. Whereas one function, one use = one problem. You could ignore which functions to call up to see which is the troublemaker and then work in the fella.

Technically, you can still wind up with multiple problems regardless in more complicated situations. One use does help to simply things with that function but won’t strictly keep it to one problem. You could have multiple missed problems with that simple function requiring to make it longer or make more functions.

Jumping all over the place can also add to complications with trying to catch problems since its harder to remember everything that’s happening and more functions to check.

To me, programming ultimately equals unforeseen consequences no matter what you do. Programmers do tend to debate the length of functions but small is considered better than large. I don’t care either way as long as its hard to find a bug and easy to understand.

1 Like

Sounds about right. My father was an old-school “pioneering” programmer: he dealt with punch cards, and “coding-to-the-metal” programming. He built many programs from scratch for various different companies as a free-lance programmer.

He talks about how many programmers argue for this or that method over others. His opinion on the matter is: all methods are simply tools for the tool belt- use the right tool for the right situation, not one tool for all situations. After that it really depends on personal preference.

1 Like

Privacy & Terms