“It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures.”
-Structure and Interpretation of Computer Programs, pg xvii
“It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures.”
-Structure and Interpretation of Computer Programs, pg xvii
The book, Structure and Interpretation of Computer Programs, is considered one of the top necessary reads for software engineers. It is a fairly expensive book to buy hard copies of. Luckily, the authors generously provide a PDF version for free. Currently, you can download the free version from the sicp-pdf on GitHub. Also, MIT used to offer a free download at https://web.mit.edu/alexmv/6.037/sicp.pdf, but that link seems to be broken these days. I recomment looking at the GitHub version.
Structure and Interpretation of Computer Programs is written in Scheme, but is all applicable to Clojure. Functional programming is taught thoroughly, along with lazy sequences and higher order functions.
Structure and Interpretation of Computer Programs doesn’t ignore the other side of the fence. It also goes over objects, state, and mutable data.
This is a Computer Science book, so expect some deep thinking and descriptions. But, if you’re looking to improve your software engineering skills, give reading this book a go.
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 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.
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.
1 |
Cleaner.<em>create</em>().register(this, () –>{<br> //Put your cleanup code here.<br>});<br> |
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:
1 |
package com.terrancedavis;<br><br>import java.lang.ref.Cleaner;<br>import java.util.concurrent.atomic.AtomicInteger;<br><br>public class CleanersExample<br>{<br> private static AtomicInteger <em>instanceCount </em>= new AtomicInteger(0);<br><br> public CleanersExample()<br> {<br><br> Cleaner.<em>create</em>().register(this, () –>{<br> System.<em>out</em>.println(<br> “Current instance count is “+<br> <em>instanceCount</em>.decrementAndGet()<br> );<br> });<br><br> System.<em>out</em>.println(<br> “Current instance count is “+<br> <em>instanceCount</em>.incrementAndGet()<br> );<br> }<br><br> public void sayHi()<br> {<br> System.<em>out</em>.println(“Hi!”);<br> }<br>}<br> |
Next the Main class to use the CleanersExample class:
1 |
import com.terrancedavis.CleanersExample;<br><br>public class Main<br>{<br><br> public static void main(String[] args)<br> {<br> new Main().usingSomeObjects();<br><br> System.<em>gc</em>();<br> try<br> {<br> Thread.<em>sleep</em>(5000);<br> }<br> catch (Exception e)<br> {<br> }<br><br> System.<em>out</em>.println(“All done!”);<br> }<br><br> private void usingSomeObjects()<br> {<br> CleanersExample ce1 = new CleanersExample();<br> CleanersExample ce2 = new CleanersExample();<br><br> ce1.sayHi();<br> ce2.sayHi();<br> }<br>} |
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!
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/
Project Euler
https://projecteuler.net/
Code Golf
https://codegolf.
Rosetta Code
https://rosettacode.org/wiki/
Coderbyte
https://www.coderbyte.com/
HackerRank
https://www.hackerrank.com/
CodeChef
https://www.codechef.com/
LeetCode
https://leetcode.com/
SPOJ
https://www.spoj.com/problems/
**** CodinGame ****
https://www.codingame.com/
https://www.codingame.com/
codeabbey
http://www.codeabbey.com/
r/dailyprogrammer
https://www.reddit.com/r/
Ruby Quiz
http://rubyquiz.com/
code_by_math
https://www.codebymath.com/
Codeforces
http://codeforces.com/
Timus Online Judge
http://acm.timus.ru/
L-99 (Lisp-like languages … like Clojure)
http://www.ic.unicamp.br/~
4Clojure (Clojure)
http://www.4clojure.com/
I Deserve
https://www.ideserve.co.in/
code jam
https://code.google.com/
Peking University POJ
http://poj.org/problemlist
HIT Online Judge
http://acm.hit.edu.cn/
React classes have a few quirks that aren’t obvious to developers that are new to React (and maybe the JavaScript universe).
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
1 |
let somevar = 42; |
or
1 |
const somevar = 42; |
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.
1 |
somevar = 42; |
or
1 |
static somevar = 42; |
The first you access using this, as in this.somevar and the second you access using a the class name, as in MyClass.somevar.
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.
1 2 3 |
myMethod = () => { return this.something; } |
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.)
1 2 3 |
myMethod() { return this.anything; } |
In previous case, bind the class’s this to the method explicitly in the constructor of the class with the following code.
1 |
this.myMethod.bind(this); |
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.
I used npx to run create–react–app . Then I added contexts and components folders and the following files to my React src folder.