Initial scroll to needs to wait for images to load (or I need to add sizes on to images, which is probably the 'right' solution)
I've been thinking about how to add pre- and post- condition checks to (C#) functions.
Roughly, preconditions are conditions that should be true at the start of a function, and post conditions should be true at the function exit. e.g.:
/**
* Return x divided by y
*/
public int Divide(int x, int y) { ... }
has a precondition that y
should not be zero.
It's easy to add these kinds of checks to a function:
public int Divide(int x, int y) {
if (y == 0) {
throw new ArgumentException("y must not be zero");
}
...
but a) they're not obviously condition checks and b) it might be nice to be able to turn them off in Release mode. (Potentially also c) they could be extracted for a test suite)
I've been thinking in terms of Attributes, but C# doesn't support Lambdas in Attributes, and probably won't any time soon. However, a new idea arrived today:
public int Divide(int x, int y) {
Contract.Pre(y, q => q != 0, "y must not be zero");
...
With a definition something like
[StackTraceHidden]
[Conditional("Development")
public void Pre<TParam>(TParam value, Func<TParam, bool> test, string onFailMessage) {
if (!test(value)) {
throw new ArgumentException(onFailMessage);
}
}
it would transparently (i.e., not showing in a stack trace) run the test in development mode, and not even be included as a call in Release.
I imagine that it would be worth adding some overrides (or sibilings) for common cases, such as Contract.PreIsNotNull
, Contract.IsOfType
. (Alternativly, include a bunch of predicates in the library).
Of course, the next question is: What do I do with this idea?
It's clearly a stand alone library. Maybe it's time to get a nuget.org account.
More plumbing/infrastructure work on the blog this evening. Post meta data is now written to a separate (JSON) file next to the entry. This makes it much easier to add stuff to meta (e.g., version number).
I've also swapped out IAsyncEnumerable
for Task<IEnumerable>
. I was having trouble working out when things (like reading the entry from disk) were actually happening, and worrying that they were happening more than needed.
Instead, BlogService.GetEntriesAsync
pulls all the meta files, and then individual entries pull (and cache) content.
Two thoughts from writing that last sentence: Is it worth running the load metas in parallel? And I should look at the proper in memory cache for entries (including content) and do things like invalidate the entry if/when edited.
I should benchmark the load stuff, and maybe set something going at process start time to pre-fill the cache.
(On the other hand, I actually want to get editing working and this is feeling like a distraction)