How do I write more maintainable regular expressions?

Use Expresso which gives a hierarchical, english breakdown of a regex. Or This tip from Darren Neimke: .NET allows regular expression patterns to be authored with embedded comments via the RegExOptions.IgnorePatternWhitespace compiler option and the (?#…) syntax embedded within each line of the pattern string. This allows for psuedo-code-like comments to be embedded in each …

Read more

Why STL implementation is so unreadable? How C++ could have been improved here?

Implementations use names starting with an underscore followed by an uppercase letter or two underscores to avoid conflicts with user-defined macros. Such names are reserved in C++. For example, one could define a macro called Type and then #include <vector>. If vector implementations used Type as a template parameter name, it would break. However, one …

Read more

Calling getters on an object vs. storing it as a local variable (memory footprint, performance)

I’d nearly always prefer the local variable solution. Memory footprint A single local variable costs 4 or 8 bytes. It’s a reference and there’s no recursion, so let’s ignore it. Performance If this is a simple getter, the JVM can memoize it itself, so there’s no difference. If it’s a expensive call which can’t be …

Read more

StringBuilder/StringBuffer vs. “+” Operator

Using String concatenation is translated into StringBuilder operations by the compiler. To see how the compiler is doing I’ll take a sample class, compile it and decompile it with jad to see what’s the generated bytecode. Original class: public void method1() { System.out.println(“The answer is: ” + 42); } public void method2(int value) { System.out.println(“The …

Read more

Usage of ‘if’ versus ‘unless’ for Perl conditionals

In Perl Best Practices, the advice is to never use unless. Personally, I think that’s lunacy. I use unless whenever there’s a simple condition that I would otherwise write as if( ! … ). I find the unless version to be more readable, especially when used as a postfix: do_something() unless $should_not_do_that; I recommend avoiding …

Read more

Best practices for turning jupyter notebooks into python scripts

Life saver: as you’re writing your notebooks, incrementally refactor your code into functions, writing some minimal assert tests and docstrings. After that, refactoring from notebook to script is natural. Not only that, but it makes your life easier when writing long notebooks, even if you have no plans to turn them into anything else. Basic …

Read more