Opinions: Is this template worthwhile as a stand-alone project?

Ranged Numeric Types in C++

I've started writing a simple template class for automating range checking in specialized numeric types, inspired by the ranged types in languages such as Pascal and Ada. At the moment it is mainly meant for integer types, though I've made it flexible enough that it should work with floating-point types (I may need to revisit this later).

It is just a stub at this point, but it should make it easier to define numeric types which don't fit the usual 8/16/32/64 bit signed and unsigned sizes. It allow the client-programmer to select a base type define both a lower and an upper bound, and define how it should behave on either underflow or overflow (with the choices at the moment being to wrap around; to saturate at the boundary; or to throw an exception). The usual arithmetic operators will all be supported.

The question I have is this: is this really a useful class to write, and will others find it useful enough that it is worth providing publicly? Can you think of use cases where this would make things easier for someone working on a real-world project?

Cam someone please assist me? what I have so far is dreadful

You must write a small application in JAVA for items and their prices. Your program must:

  1. Create an array of item names. Use the following item names: Bread, Milk, Butter, Chips,
    Honey, Soap, Towel, Carrots, Beans, Samp, Dress, Pants, Shoes, Socks.
  2. Create an array of item prices. Use the following item prices: 15.50, 12.00, 56.00, 20.00, 45.00,
    7.00, 75.00, 8.00, 5.00, 12.00, 200.00, 150.00, 400.00, 25.00.
  3. Display every item name and its corresponding price. See the sample output given.
  4. Print the average price of all the items.
  5. Find and display the price of a specific item
    a. Read in the name of the item.
    b. Loop through the items array and look for the specific item.
    c. If the item is found, display the price of the item.
    SAMPLE OUTPUT
    Display every item name and its corresponding price.
    Items in stock with prices

    Bread - R15.5
    Milk - R12.0
    Butter - R56.0
    Chips - R20.0
    Honey - R45.0
    Soap - R7.0
    Towel - R75.0
    Carrots - R8.0
    Beans - R5.0
    Samp - R12.0
    Dress - R200.0
    Pants - R150.0
    Shoes - R400.0
    Socks - R25.0
    Display the average price of all the items
    The average price of all the items is R73.6
    Find and display the price of a specific item Sample input and output
    Sample run 1 Sample run 2
    Item Look-up
    What item are you looking for?
    Dress
    The price of item Dress is R200.0
    Item Look-up
    What item are you looking for?
    hjgjg
    Item hjgjg is not stocked in the shop.

What I have:

public class ShopApp


public static void main(String args[])


Shop items[]=new Shop[14];
items[0]=new Shop("Bread", (int) 15.00);
items[1]=new Shop("Milk", (int) 12.00);
items[2]=new Shop("Butter", (int) 56.00);
items[4]=new Shop("Chips", (int) 20.00);
items[5]=new Shop("Honey", (int) 45.00);
items[6]=new Shop("Soap", (int) 7.00);
items[7]=new Shop("Towel", (int) 75.00);
items[8]=new Shop("Carrots", (int) 8.00);
items[9]=new Shop("Beans", (int) 5.00);
items[10]=new Shop("Samp", (int) 12.00);
items[11]=new Shop("Dress", (int) 200.00);
items[12]=new Shop("Pants", (int) 150.00);
items[13]=new Shop("Shoes", (int) 400.00);
items[14]=new Shop("Socks", (int) 25.00);

    for (Shop item : items) {
        System.out.println("Item: " + item.item + ", Price:" + item.price);
    } 

    }

class Shop


String item;
int price;

public Shop(String item,int price)

this.item=item;
this.price=price;

When Microservices Are a Bad Idea

On paper, microservices sound wonderful. They are modular, scalable, and fault tolerant. A lot of companies have had great success using this model, so microservices might naturally seem to be the superior architecture and the best way to start new applications.

However, most firms that have succeeded with microservices did not begin with them. Consider the examples of Airbnb and Twitter, which went the microservice route after outgrowing their monoliths and are now battling its complexities. Even successful companies that use microservices appear to still be figuring out the best way to make them work. It is evident that microservices come with their share of tradeoffs.

How to Calculate the Percentage Score in a Google Forms Quiz

Google Form Quiz

We have created a simple quiz in Google Forms that has 3 questions and each correct answer gives you 10 points. The maximum score that can be attained in the quiz in thus 30 points.

When someone takes the quiz and submits the form, the responses are recorded in a Google Sheet that is set as the response destination for your Google Form.

There’s something interesting here as well. If the associated form is a quiz, Google Sheets will automatically add an extra column in the response sheet titled “Score” and this column will be populated with the total marks obtained by respondent in the quiz.

Google Forms Quiz Score in Google Sheets

Convert Quiz Score to Percentage

A teacher may want to calculate the percentage score obtained by students in the Quiz and assign grades accordingly. This can be easily done with the help of Array Formulas in Google Sheets but before we get there, let’s see how we can convert the quiz score (say, 20/30) into a percentage.

Extract the Score Obtained

There are at least three way to extract the quiz score obtained from the cell B2. Let’s explore some of them.

The REGEXREPLACE function will replace any string value matching the RegEx with another value. Here, we start with the first character in the cell that is not a digit, match everything until the end of the string and replace it with a blank. Thus the slash (/) and everything after the slash is replaced and we are only left with the score.

=REGEXREPLACE(TO_TEXT(B2),"\D.+$","")

For the second approach, we use the SPLIT function to split the text in the score column, with slash as the delimiter, and then use the INDEX function to get the first value of the split array which contains the score.

=INDEX(SPLIT(B2,"/"),1)

In the next approach, we use the SEARCH function to determine the position of the slash in the cell and use the LEFT function to get everything before the slash.

=LEFT(B2,SEARCH("/",B2)-1)

Extract the Total Quiz Score

We can use a similar approach to obtain the maximum score of a quiz and that number is after the slash in the Score column.

=REGEXREPLACE(TO_TEXT(B2),"\d.+/","")
=INDEX(SPLIT(B2,"/"),2)
=RIGHT(B2,SEARCH("/",B2)-1)

Calculate the Quiz Percentage

Now that we have formulas to separately extract the quiz score and total score, we can combine these to get the percentage score.

Your options are:

=REGEXREPLACE(TO_TEXT(B2),"\D.+$","")/REGEXREPLACE(TO_TEXT(B2),"\d.+/","")
=INDEX(SPLIT(B2,"/"),1)/INDEX(SPLIT(B2,"/"),2)
=LEFT(B2,SEARCH("/",B2)-1)/RIGHT(B2,SEARCH("/",B2)-1)

Right-click the score column, choose Insert 1 column left from the contextual menu and paste any of the above formula in the cell C2. You may then copy the formula down to other rows that contain the quiz responses.

Copy Down Quiz Score Percentage Automatically

One drawback of the previous approach is that you have to add the formulas in the row each time a new quiz is submitted.

A simple workaround to the problem is the copy formula down approach that will automatically add the formulas whenever a new quiz form is submitted.

Quiz Percentage Formula

Go to cell C1 and paste the formula below.

=ArrayFormula(IF(ROW(B:B)=1, "Percentage",
   IF(NOT(ISBLANK(B:B)),LEFT(B:B,SEARCH("/",B:B)-1)/RIGHT(B:B,SEARCH("/",B:B)-1),)))

It looks at the row index and if it is the first row, it adds the column title. Next, it checks if there’s a score value in the column B and then calculates the percentage score.

Next, select the C column, go to Format > Number > Percent to properly format the calculated percentage.

You can also Document Studio to send certificates based on Quiz scores.

Amazon Lightsail: Virtual Cloud Server

Amazon Lightsail, launched in 2016, is essentially a virtual private server (VPS) that offers you everything needed to deploy an application or website in a cost-effective and easy-to-understand manner. It is a basic service, but extremely convenient.

It is designed for customers, developers, small businesses, or startups to get quickly started in AWS. It reduces the learning curve in the beginning, and users can later adopt the broad AWS Services as they get more familiar with the AWS cloud and/or solution workload increases.

How I Added Scroll Snapping To My Twitter Timeline

CSS Scroll Snap allows websites to snap the web page or any other scroll container to a specific scroll position when the user performs a scrolling operation. This feature has been supported in all modern browsers for over two years, but many websites that could benefit from it are still not using it.

Scroll snapping is probably most associated with horizontal carousels (see Chris’s CSS-only approach) and particular web pages divided into full-screen slides. But why stop there? I believe that snapping can improve the scrolling experience on any web page that lays out items in a grid or feed.

For example, most shopping websites show products in a grid. Ideally, the user would like to jump between the grid rows with minimal effort. The user can press Space to scroll the page by roughly one screen (viewport height), but depending on the height of the grid rows, the scroll position will eventually get “out of sync” with the grid, and the user will have to re-adjust it manually.

If we add scroll snapping to this page, the user can consistently scroll to the next row with the Space key (pressing Shift + Space will scroll to the previous row). It’s pretty effortless.

I think that scroll snapping would be a welcome addition to this website. And it’s not even that complicated to implement. The CSS code that I used for this example is relatively simple:

html {
  scroll-snap-type: y proximity;
}

.product-item {
  scroll-snap-align: start;
  scroll-margin-top: 75px; /* height of web page’s sticky header */
}

You don’t have to wait if a website you visit regularly hasn’t yet added scroll snapping and you think it would improve your scrolling experience. You can add scroll snapping yourself — with user styles.

Adding user styles to websites

In the video above, you can see that I selected a user.css file in Safari’s advanced preferences. This file is a user style sheet. It contains CSS styles that I’ve written, stored in a local .css file, and added to Safari. These “user styles” are then applied to every web page I open in Safari.

Chrome and Firefox do not allow users to select a user style sheet. Firefox supported a similar feature called userContent.css in the past, but that feature was deprecated and disabled by default in 2019. I recommend the Stylus browser extension for these two browsers (and other Chromium-based browsers).

One significant advantage of Stylus is that it allows you to write user styles for specific websites and URLs. Safari’s user style sheet applies to all websites, but this can be worked around, e.g., by using the new :has() pseudo-class to create selectors that only match specific websites.

The Stylus extension has been reviewed by both Chrome and Firefox teams and received a badge that denotes high standards.

The CSS Cascading module defines a User Origin for styles the user adds. Safari’s user style sheet belongs to this origin, but the Stylus extension injects user styles to the Author Origin, where the website’s style sheets live. Specifically, Stylus inserts user styles directly to the page via a <style> element at the end of <html> which makes it the final style sheet on the page. Technically, this means styles added via Stylus are classified as author styles since they’re not in the User Origin, but I will continue to call them user styles because the user adds them.

However, it’s worth keeping this distinction in mind because it affects the cascade. When selector specificity is equal, a real user style is weaker than the page’s own style. This makes user styles an excellent fit for user defaults. Under the same conditions, a style added via Stylus is stronger than the page‘s style, so Stylus cannot as easily be used to define user defaults.

If we add !important to the mix, both real user styles and styles added via Stylus are stronger than the page’s styles. So when you want to impose your user styles on a website, it doesn’t matter if you use Safari’s “Style sheet” option or the Stylus extension. Your !important styles will win either way.

In the next section, I will use a set of !important user styles to enforce scroll snapping on the timeline page of Twitter’s website. My goal is to speed up the process of reading my Twitter timeline by avoiding awkward scroll positions where the topmost tweet is only partially on screen.

Scroll snap for Twitter’s timeline

After some experimentation, I’ve settled on the following CSS code. These styles work well in Firefox, but I’ve experienced some issues in Chrome and Safari. I will describe these issues in more detail later in the article, but for now, let’s focus on the behavior in Firefox.

html {
  scroll-snap-type: y mandatory !important;
}

/* tweets in the timeline are <article> elements */
article {
  scroll-snap-align: start !important;
}

/* un-stick the sticky header and make it “snappable” as well */
[aria-label="Home timeline"] > :first-child {
  position: static !important;
  scroll-snap-align: start !important;
}

/* hide the “new Tweets available” floating toast notification */
[aria-label="New Tweets are available."] {
  display: none !important;
}

It is necessary to add !important to each declaration because all the user styles must win over the web page’s own styles for our custom scroll snapping implementation to work correctly. I wish that instead of repeatedly writing !important, I could just put my user styles in an “important layer,” but such a CSS feature does not exist (yet).

Watch the video below to see my scroll snap user styles in action. Notice how each press on the Space key scrolls the next set of tweets into view, and the first tweet of each set is aligned to the top edge of the viewport. This allows me to read my timeline more quickly. When I need to go back to the previous set of tweets, I can press Shift + Space.

What I like about this type of scroll snapping is that it allows me to predict how far the page will scroll whenever I press Space. Each scroll distance equals the combined heights of the visible tweets that are entirely on the screen. In other words, the partially visible tweet at the bottom of the screen will move to the top of the screen, which is precisely what I want.

I know in advance that pressing Space will scroll Dave’s tweet to the top of the screen.

To try out my scroll snap user styles on your own Twitter timeline, follow these steps:

  1. Install the Stylus extension with Firefox Add-ons or the Chrome Web Store.
  2. Navigate to your Twitter timeline at https://twitter.com/home.
  3. Click the Stylus icon in the browser’s toolbar and click “this URL” in the pop-up.
  4. Stylus will open a code editor in a new browser tab. Copy-paste my scroll snap user styles into the editor and press the Save button in the sidebar on the left. The styles will be applied to your Twitter timeline immediately (no need to reload the page).
  5. You can update the styles at any time. Click the Stylus icon and the Pencil icon to open the editor again.

Inability to override snapping

My implementation of scroll snapping for Twitter’s timeline has one major flaw. If a tweet is taller than the viewport, it is impossible to scroll the page to reveal the bottom part of that tweet (e.g., if you want to like or retweet that tweet) because the browser forcefully snaps the page to show the top of the tweet (or the top of the following tweet).

The severity of this problem depends on the user’s display. Viewing Twitter’s timeline on a large desktop monitor at a small page zoom factor, you may not encounter any tweets taller than the viewport.

I have asked the CSS Working Group if it would be possible to add a mechanism allowing the user to override the browser’s mandatory scroll snapping. I should probably mention that this problem could, at least in theory, be resolved by switching from mandatory to proximity snapping. I’ve tested proximity snapping in Chrome and Firefox, and I found it inconsistent and confusing. The browser would often snap when I didn’t expect it to, and vice versa. Maybe Twitter’s code is interfering with the proximity algorithm, the browsers are still a bit buggy, or perhaps I’m just “scrolling it wrong,” if that’s even possible. I don’t know.

But the main reason why I went with mandatory snapping is that I wanted to avoid situations where the topmost tweet is only partially on screen after a scroll. The type of fast-scrolling between sets of tweets that I’ve shown in the video above is only possible with mandatory snapping.

If you, like me, prefer mandatory snapping, I can suggest the following two workarounds for the “tall tweet” problem:

  • You can open the tweet on its own page and return to the timeline afterward.
  • If you only want to click the Like or Retweet buttons, you can Shift-click the tweet to select it and then press L to like it, or T followed by Enter to retweet it.

Issues in Chrome and Safari

My scroll snap user styles produce noticeably different scroll snapping behaviors in Chrome, Safari, and Firefox. Those differences are in part since the exact implementation of the snapping mechanism is left up to the browser:

The CSS Scroll Snap Module intentionally does not specify nor mandate any precise animations or physics used to enforce snap positions; this is left up to the user agent.

The current version of Safari has a bug that prevents scroll snapping from working correctly on the Twitter timeline. I have reported this bug.

In Chrome, I have encountered the following problems:

  • The scrolling operations animate inconsistently. Sometimes the animation is slow, sometimes it’s instant, and sometimes it starts slow but is then cut short. I found this irritating.
  • The scrolling operations animate too slowly in general. I performed a test in Chrome and Firefox (20 Space presses), and it took me 70% more time to cover the same distance on my Twitter timeline in Chrome than in Firefox (18.5 seconds in Chrome vs. 11 seconds in Firefox).
  • When I scroll using my laptop’s trackpad, the page flickers a lot. When I attempt to scroll fast by holding down the Space key, the page scrolls very slowly and oscillates. I suspect that both issues are caused by the same algorithm. It seems that Chrome re-snaps at a very high rate in these cases. I have reported this bug.

These browser bugs and differences between browsers can be a problem for websites considering implementing scroll snapping. For example, a web developer might hold back because they don’t like how scroll snapping behaves in one particular browser. Browsers can mitigate this problem by becoming more interoperable. In fact, Scroll Snap is one of the areas of focus of the cross-browser Interop 2022 effort.

Another way the situation could be improved is by introducing new CSS properties that would make scroll snapping more configurable. This could include the duration of the snapping animation, the length of the proximity threshold for snapping, and a mechanism to override mandatory snapping.

To snap or not to snap?

I’ve been using my scroll snap user styles on Twitter’s timeline for a couple of weeks, and I don’t want to go back. The ability to quickly flip through my feed with only the Space key is just on another level.

However, I consider this an advanced feature that probably isn’t for everyone. There’s a reason why I’ve enabled it only on the timeline (/home path) and nowhere else on Twitter’s website. Snapping is a significant change in how the page scrolls, and it takes some time to get used to. It can work great for a specific use case, but it can also get in the way and frustrate the user.

Websites with feeds should therefore consider offering scroll snapping only as an optional feature, after careful consideration and plenty of testing in different browsers and with different input methods (mouse, keyboard, trackpad, touch screen, etc.).

Before you go…

Finally, I highly recommend installing and trying out the Stylus browser extension. Web developers (or anyone who knows CSS) have the power to style any website in their browser. You can apply minor improvements and fixes to your favorite websites. I mostly use it to hide page elements that I find annoying, such as sticky headers, video pop-ups, and vote counts.

But more importantly, Stylus allows you to quickly test new CSS features on any website and report browser bugs, if necessary. By doing this, you can help make the web platform a little better.


How I Added Scroll Snapping To My Twitter Timeline originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

Diving Deep into Smart Contracts

The “Moving From Full-Stack Developer To Web3 Pioneer” publication provided a high-level overview to give full-stack developers a glimpse into the world of Web3 development. If you haven’t had a chance to review that article, consider taking a look, since it provides a good introduction into Web3 there as well.

The end result of my original article demonstrated how a homeowners association (HOA) could use Web3 technology to host their election ballot. The problem with the original design is that the underlying smart contract only allowed for a single yes or no answer. This was by design to keep the smart contract simple, while introducing other concepts required to create an HOA ballot using Web3 technologies.