Posts in category “Programming”

发布 Game scripting mastery 中文精要 - 第八章

Game scripting mastery 是一本不可多得的好书,除了写得啰嗦。

豆瓣网友 全棧法師张解靈 2018-08-01 16:37:13 写道:
说实话,难看。 但是内容完全没问题,就是实在太啰嗦了,即使英语原著也是...看得有点难受,想跳呢又怕错过好东西,想慢慢看呢又...还是慢慢啃吧 如果谁重排一个脱水版,我绝对打满分

另一个网友 TerryX 2017-01-06 18:44:19 写道:
这本书可以说是被严重低估,可能因为这个专职院校味道的书名。从第9章开始简直不能再好,从编译器底层到高级虚拟机一气呵成。这本书真不仅仅是教你做游戏,而是教你怎么成为master。

所以我启动了这个脱水版项目:初步计划是从第8章开始到全书结束,借助chatGPT神力完成这个项目,希望不要烂尾。

哦,点击本文标题,或者这里,查看第八章全文。 对了,这个项目在gitHub上。如果你发现有可改进之处,欢迎改进并提交PR。

.NET Core/6: Prevent .NET runtime from logging errors that are already handled in ExceptionFilter

We use Graylog to log everything, on Windows machines we also log into EventLog. I recently noticed for every exception I got two items in EventViewer, one is Logged by our code, and the other is logged by the .net runtime I guess. It is a little bit annoying. TLDR; here's the answer:

        public override void OnException(ExceptionContext actionExecutedContext)
        {
            _logger.Error(actionExecutedContext.Exception);
            actionExecutedContext.ExceptionHandled = true;
            actionExecutedContext.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
            actionExecutedContext.Result = new ObjectResult(new ErrorResponse()
            {
                StatusCode = (HttpStatusCode) StatusCodes.Status500InternalServerError,
                Message = actionExecutedContext.Exception.Message,
            });
            base.OnException(actionExecutedContext);
        }

The key is this line actionExecutedContext.ExceptionHandled = true;. Reference

PL/SQL: Local function definition in a public procedure / function

Occasionally, we will need to define local used functions in a public procedure or a function. Fortunately, PL/SQL supports embedded functions. I spent more than an hour trying to find the correct way to use this feature. Yes. forgive me, I am a little dumb. According to my retries, the best position to put your local functions is sharp before the BEGIN symbol. And here's a sample.

set serveroutput on;
CREATE OR REPLACE PROCEDURE tmp12345(v_full_name IN VARCHAR2) AS
	localv varchar2(100);
	-- local function definitaions start ---
	FUNCTION FETCH_REAL_NAME(v_statement_name IN VARCHAR2) RETURN VARCHAR2
	AS
		pattern VARCHAR2(100) := '^(Dr|Ms|Mx|Mr|Mrs)\s+';
		full_name varchar2(100);
	BEGIN
		-- replace possible multiple spaces to one space
		full_name := TRIM(REGEXP_REPLACE(v_statement_name, '\s+',  ' '));
		IF (REGEXP_LIKE(full_name, pattern, 'i')) THEN
			-- REGEXP_REPLACE(string, pattern [, replacement_string [, start_position [, nth_appearance [, match_parameter ] ] ] ]) 
			RETURN REGEXP_REPLACE(full_name, pattern, '', 1, 0, 'i');
		ELSE
			RETURN full_name;
		END IF;
	END;
	FUNCTION FETCH_MIDDLE_NAME(full_name IN VARCHAR) RETURN VARCHAR AS
		middle_name VARCHAR2(100);
	BEGIN
		middle_name := SUBSTR(full_name, INSTR(full_name, ' ') + 1, INSTR(full_name, ' ', 1, 2) - INSTR(full_name, ' ') - 1);
		IF (LENGTH(middle_name) > 10) THEN
			-- return initial instead
			RETURN SUBSTR(middle_name, 1, 1);
		ELSE
			RETURN middle_name;
		END IF;
	END;
	-- local functions definitaions end ---
BEGIN
	-- main logic here
	localv := v_full_name;
	DBMS_OUTPUT.PUT_LINE(localv);
	DBMS_OUTPUT.PUT_LINE(FETCH_MIDDLE_NAME(FETCH_REAL_NAME(v_full_name)));
END;
/

Approval Testing is tasty

Just record a few tips here:

[UseApprovalSubdirectory("Approvals")] can be used to set up the directory that saves the approval results.

It can be used for a test class or a test method.

Links

ApprovalTests.Net Project

An awesome entry to better code: The gilded rose kata

For some reason, I was told an interesting github repo, The Gilded Rose Kata. It really grasps my attention. The code's behavior is correct, but both code quality and code style are terrible. The author of the repo made it so bad on purpose. So it is the best material to teach newbies how to refactor.

From the Readme file of the repo, I learnt some new tools, one is the text based approval test. I actually know that concept before many years ago, but never practiced it. I just found it is easy to setup and easy to run. It tests your code from a new angle. I believe it could belong to a kind of greybox test. You need to know the code to test, but you don't know too much like other unit test methods.

I have done the refactoring, and my result has been put on Github.com

  1. first approach is on the main branch https://github.com/shukebeta/GildedRose-Refactoring-Kata/tree/main/csharpcore
  2. a better approach is on the other branch https://github.com/shukebeta/GildedRose-Refactoring-Kata/tree/simple-factory-pattern-approch

Here are some good links, and I would update it timely.

Writing good tests for the gilded rose kata
Principles for agile test automation
The gilded rose refactoring kata
How to refactor gildedrose refactoring kata with simple factory pattern and strategy pattern