What is good code? and what attributes do you think an ideal software developer demonstrates?

I was asked those questions recently and the following is my answer:

What is good code?

Good code has at least the following characteristics:

  • Good code has good readability, so it is easy to understand.

    • Good code is well organized, a clear and reasonable directory structure with meaningful filename is the basic element of good code.
    • Every class, method, variable has a deliberate and meaningful name. You can know the purpose of an identifier through the name at first glance.
    • Methods should be short, every method should only do one thing.
    • Good code does not use magic numbers and hard coded strings.
  • Good code has automated tests, so it is easy to maintain or change.

  • Good code has a good architecture, so its components can easily be replaced by another.

  • Good code has explicit dependencies.

  • Good code has good performance.

    • It does not do database queries or other time-consuming jobs in a loop.
  • It should use a queue for certain situations

  • It should use a full-text search engine instead of the 'LIKE' query on the database.

  • If part of the code is really complex, a carefully written comment would be helpful. By the way, do not forget to update the comment when you change the code later.

  • Good code is highly cohesive and low coupling. Components should be well defined, that is, they are self-contained with one and only one purpose. Each component should know as little as possible about another component.

  • Good code is modular. The Business logic (the controller) should be separated from the data storage layer (the model), while the view layer should be separated from the controller layer.

What attributes do you think an ideal software developer demonstrates?

  • They should always have enthusiasm for programming, which means that they programming not only at work.
  • They should know Linux or macOS well. I mean they should know the command line well.
  • They care about the user experience.
  • They are responsible and willing to do their best to meet the deadline.
  • They are knowledgeable, I mean they know not only programming, they have wide knowledge in various fields.
  • They love sharing and teaching, or mentoring. I mean they are kind to newbies.

What do you believe is the difference in behaviours and skills between a Senior Software Engineer and an Intermediate Software Engineer?

  • A senior engineer not only knows how to code or how to implement a feature but also knows how to code in a better way. They not only implement a feature, they help refine the feature. I mean they do extra work to make the feature better.
  • Senior engineers know how to plan, separate big tasks into smaller ones, and can recognize which task has the higher priority.
  • Senior engineers not only program, they help other engineers grow up. They do mentoring, tutoring, or teaching in a team.
  • Senior engineers are people beyond the bounds of skillset. They could be a potential leader, or just a leader without a title.

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

Git Tips: Remove tracking branches no longer on remote

My colleage Joe asked me tonight, "how to remove those branches that no longer exist on remote?"

In short, you have two options

  1. run git remote prune origin at times
  2. run git config --global fetch.prune true command to config your git to delete those branches every time when you run git fetch

I prefer the second option, how about you?

Reference

Fix swagger stuck on extend an API: How to configure Swashbuckle to ignore property on model

Referenced

I found the root cause is that the JSON generated by swagger is too large: Swagger outputs over 18000 lines for that API! Further research told me that two property fields contribute about 17000 lines. So how to configure swagger to ignore these two properties is the key. After googling a while, I found VeganHunter's answer in this thread Reference is the simplest.

Solution for .NET Core 3.1 and .NET Standard 2.1:

Use JsonIgnore from System.Text.Json.Serialization namespace.

( JsonIgnore from Newtonsoft.Json will NOT work )

public class Test
{
    [System.Text.Json.Serialization.JsonIgnore]
    public int HiddenProperty { get; set; }
    public int VisibleProperty { get; set; }
}

Hope it could also help someone else. 😀