Category: Programming

  • Solidity

    Need Solidity based smart contracts for the Ethereum blockchain? Then you’re at the right place. Solidity based smart contracts are the next disruptive technology for the 21st century.

    By now, everyone has heard of Bitcoin. The longstanding second most popular cryptocurrency is Ethereum. However, the difference between Bitcoin and Ethereum is like the difference between paper dollars and gold. Paper dollars have no value, except good faith. Gold has uses in industry and science in addition to being tradable as money.

    Ethereum is like gold. You can trade Ethereum as a cryptocurrency, and many do. However, you can also use the Ethereum blockchain to create unstoppable, uncensorable, massively distributed applications. The Ethereum blockchain can host applications and data, making it the next big disruptive technology.

    Imagine if you could have got in on the ground level of social media, search engines or even the Internet. The next huge disruptive tech is sitting out there. It is the Ethereum blockchain.

    Solidity based smart contracts allow for the development of publicly verifiable information. You can place keys on the blockchain proving authenticity of everything from land titles to certifications and diplomas. Using blockchain apps made with solidity, you can set publicly verifiable contracts up, or set payments or information to be released at some future date.

  • The Lowly Comment

    The first rule taught in software development is, “Comment your code with a reason or purpose for its existence.” This is to say, “Why does this variable, method, or class exist?”

    Here’s why.

    Heavily used code always suffers from purpose creep. Purpose creep leads to expensive bugs. For example, I once found a bug in code used by hundreds of corporate customers that consisted of a variable being used for two purposes. Most of the time, the variable value was the same value for both purposes. However, after selling the software for many millions of dollars to 100’s of clients that expected to never change their integrations with our code, we had a problem that required new installations and new integrations.

    Talk about a preventable problem. One comment explaining the purpose of one variable could have saved the company millions of dollars worth of angry customers.

    To prevent purpose creep within a class, method, or variable, stick with the purpose described in the comment, or change the comment. It’s very easy for a maintainer of old code to assume the purpose of a well named variable, but not realize the actual purpose is similar, but not the same as he thinks it is.

    The first rule of programming that is thrown out the door in the workplace is, “Comment your code.” Even at the better companies I’ve worked for, almost no one comments their code. Some developers live in a fantasy world where class names, method names and variable names give all the architectural details needed to understand millions of lines of code. I once ran into a 45,000 line class with no comments. I even worked for a company that forbade comments, and you could get called in for a talk with your manager if you made the mistake of leaving a comment in your code–very Dilbertesque.

    Every codebase has blocks of code that are so important that they are called constantly. And when an edge case is found, those blocks of code get a conditional thrown in to handle that edge case. Then another edge case is found and a new parameter is passed in to handle that edge case. Another developer under tight deadlines realizes the code is perfect except, it needs to handle his case where ….

    As a result, almost every important method and class suffers quickly from purpose creep. By this I mean that if you found someone that actually understands the class or method in question, it will take minutes and sometimes hours for them to describe what is going on in the class or method.

    Soon, the most important code in your application is too fragile to change. Ten or twenty edge cases that no one remembers have infiltrated your most important code. And worse yet, there are no comments explaining what can brake if even a minor change happens to your code. Then a maintainer of the code gets stuck working nights and weekends making changes to once simple software.

    Detailed commenting of important code helps prevent fragile code. Have a concise explanation of purpose of your code. If the comments are getting hard to follow, then it is time to refactor the code into easily described chunks. Maybe move those edge cases out into their own methods or classes.

    Comments containing clear purpose statements can prevent working late nights, lost customers, and even save your company millions of dollars. There’s a lot to be said about the lowly comment.

  • Replacing Java’s finalize() Method

    If you’ve reached Java 9 or above, you’ll notice at some point that the Java’s Object.finalize() method is deprecated. A quick look at the JavaDocs doesn’t give any easy solutions for a quick replacement. (Good luck with PhantomReferences, btw.)

    So here’s a drop-in replacement that should have been documented, but I haven’t seen anyone mention this on Stack or anywhere else. Use a cleaner. If what you’re doing is simply destroying references, or something else similar, put the following code in your Java class constructor and copy the guts of finalize() into it. Then delete your old finalize() method.

    Now you’re Java 9 compliant.

    What you’re doing with this code change is creating a Runnable that is called when the Cleaner detects that your Object is in need of cleanup.

    If you want to get really fancy, create only one cleaner and reference that one cleaner to register your objects during construction. That’s probably what you’ll do in any app that has a lot of objects that need cleaning up.

    Here’s some code to make it a bit more clear how to use a Java Cleaner to replace a finalize() method.

    First the CleanersExample class:

    Next the Main class to use the CleanersExample class:

    The output will look something like this.

    Current instance count is 1
    Current instance count is 2
    Hi!
    Hi!
    Current instance count is 1
    Current instance count is 0
    All done!

  • Programming Puzzles and Challenges

    One of the funnest ways to learn new languages and sharpen your coding skills is to do coding challenges and puzzles. There are even competitions arranged around solving computer programming challenges and puzzles. Also, don’t be surprised if some of the problems listed show up in job interviews. This is a common way to filter out good programmers from not-so-good programmers when interviewing.

    Here’s a list of nice code challenge sites. If you have a favorite I don’t have listed, please contact me and let me know.

    Code challenges and puzzles

    Rosalind (bioinformatics problems)
    http://rosalind.info/problems/list-view/

    Project Euler
    https://projecteuler.net/

    Code Golf
    https://codegolf.stackexchange.com/questions

    Rosetta Code
    https://rosettacode.org/wiki/Category:Programming_Tasks

    Coderbyte
    https://www.coderbyte.com/challenges

    HackerRank
    https://www.hackerrank.com/dashboard

    CodeChef
    https://www.codechef.com/problems/school

    LeetCode
    https://leetcode.com/problemset/all/

    SPOJ
    https://www.spoj.com/problems/classical/

    **** CodinGame ****
    https://www.codingame.com/faq
    https://www.codingame.com/start

    codeabbey
    http://www.codeabbey.com/index/task_list

    r/dailyprogrammer
    https://www.reddit.com/r/dailyprogrammer/

    Ruby Quiz
    http://rubyquiz.com/

    code_by_math
    https://www.codebymath.com/index.php/welcome/challenges

    Codeforces
    http://codeforces.com/problemset

    Timus Online Judge
    http://acm.timus.ru/problemset.aspx

    L-99 (Lisp-like languages … like Clojure)
    http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

    4Clojure (Clojure)
    http://www.4clojure.com/problems

    I Deserve
    https://www.ideserve.co.in/

    code jam
    https://code.google.com/codejam/past-contests

    Peking University POJ
    http://poj.org/problemlist

    HIT Online Judge
    http://acm.hit.edu.cn/problemset

  • ReactJS Class Attributes

    React classes have a few quirks that aren’t obvious to developers that are new to React (and maybe the JavaScript universe).

    Classes and Attributes

    ReactJS classes are actually ES6 classes with Babel syntactic sugar thrown in. For example, coming from other languages, you might expect class attributes to use the form

    or

    However, that doesn’t work. In ES6 classes, you cannot add attributes directly to the class. They are actually added after the class is defined–very ugly and painful to look at.

    Babel compiles/transpiles React classes, though. So, you get the ability to add class attributes using either of the following syntaxes.

    or

    The first you access using this, as in this.somevar and the second you access using a the class name,  as in MyClass.somevar.

    Don’t Forget ‘this’

    Since we mentioned this, don’t forget about binding this to your methods before using it. (Bindings in JavaScript are always such a joy.)

    If you create your methods in your ReactJS classes using the arrow method, then you have the class’s this, already.

    However, if you create your methods using the following approach, then this may not be the this you are looking for. (It won’t be the class’s this when you call it.)

    In previous case, bind the class’s this to the method explicitly in the constructor of the class with the following code.

     

  • React Component Contexts

    Sometimes when creating a SPA with React, you realize you need some global variables. The easiest way to make values available at all levels of a complex ReactJS application is to use a Context. The following is an example of creating a React SPA with a single global context that shares values at all levels of the component tree. I give two examples of using the context. One is a class based React Component and the other is a function based React Component.

    Two React Components using one global context.
    Two React Components using one global context.

    I used npx  to run createreactapp . Then I added contexts and components folders and the following files to my React src folder.

    (more…)