Clean your code, not your laptop

A person cleaning a Laptop. Is this clean code?
enum EmailValidationError { invalid }class Form1 extends FormzInput<String, Form1ValidationError> {
const Form1.pure() : super.pure('');
const Form1.dirty([String value = '']) : super.dirty(value);

static final String _form1RegexString =
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+";

static final RegExp _form1Regex = RegExp(_form1RegexString);

@override
Form1ValidationError? validator(String? value) {
return _form1Regex.hasMatch(value!) ? null : Form1ValidationError.invalid;
}
}
enum EmailValidationError { invalid }

class Email extends FormzInput<String, EmailValidationError> {
const Email.pure() : super.pure('');
const Email.dirty([String value = '']) : super.dirty(value);

static final String _emailRegexString =
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+";

static final RegExp _emailRegex = RegExp(_emailRegexString);

@override
EmailValidationError? validator(String? value) {
return _emailRegex.hasMatch(value!) ? null : EmailValidationError.invalid;
}
}
  1. Use meaningful names
    Here, you should understand this step by the example I’ve given you. Just give your variable name a meaningful one, it’s not that hard. Don’t use x or y in naming any variable, please.
  2. Single Responsibility Principle
    By seeing the name, a function should only has a single responsibility. New programmers tends to write a function that do almost everything. This will make your code hard to read, making it harder also to debug. A function should be small and do only one thing.
  3. Avoid writing unnecessary comments
    In the foundation of programming course in my faculty, it is encouraged to give documentation in your code. But in reality, many students understand this mindset as to give almost everything a comment/documentation. I have seen someone write a comment for almost every row he/she created. The good example for this is if your code uses a third party API, you should give a brief explanation about the API, so another developer can understand the code you’ve written.
  4. It’s broken if it’s not tested
    Start writing your test with small unit tests to make sure the small part works correctly as you want. Then, try to test subsystems together, until you started to tes the whole system. With using TDD principle, you can easily identify sources of issues if one occurs.
  5. Write HUMAN-readable code
    Most programming language has each barrier to read, like semicolons in Java, indentation in Python, etc. So, in order to help this, you may use several extensions in your text editor to help you with that. In my case, I’m using Dart at my PPL project. Everytime I save a file, if there are no syntax error, VS Code will automatically give indentations and tidy the code so it’s easier to read.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store