RSS
 

Archive for the ‘Programming’ Category

20 controversial programming opinions

06 May 2018

This is my response to the 20 controversial programming opinions stated in this Software Engineering Stack Exchange blog post.

  1. Programmers who don’t code in their spare time for fun will never become as good as those that do.
    I somewhat agree with this statement. Being interested in programming in your spare time by creating side projects and reading programming related material make you a better programmer. However, I don’t think one needs to devote their entire life to programming to become very capable. Even an hour or two a week working on a side project can make a difference.
  2. Unit testing won’t help you write good code.
    I disagree with this statement. Unit testing and test driven development (TDD) can help you write good code. With TDD you can write a test that fails and then write/alter code until it passes meaning you solved the problems. Edge cases can of course be discovered during development and a good programmer will revise/update tests if they encounter edge cases when creating their solution.
  3. The only “best practice” you should be using all the time is “Use Your Brain”.
    I agree with this statement. You don’t always have to apply a method, pattern, framework to every problem.
  4. Most comments in code are in fact a pernicious form of code duplication.
    I somewhat agree with this statement. I myself am guilty of writing some redundant comments. Actually I tend to over-comment quite a bit (which I still believe is better than under-commenting). Comments like this are obviously redundant but aren’t very common overall:
  5. “Googling it” is okay!
    I agree with this statement. Googling is always okay. Referring to references/documentation is efficient. Sitting down and struggling for an hour or recreating the wheel when you could have looked up a solution/idea/etc is just a waste of time.
  6. Not all programmers are created equal.
    I agree with this statement. There will always be a difference (minor or major) in the skill set of every programmer. Some will be more knowledgeable in some areas and others may be able to churn out code faster.
  7. I fail to understand why people think that Java is absolutely the best “first” programming language to be taught in universities.
    I agree with this statement. I think C is by far the best programming language to learn as your “first programming language”. Its very low level, bare bones and makes you take care of almost everything (memory management, complex data structures, etc). I had to code in purely C for my first programming class in University and it was a great start.
  8. If you only know one language, no matter how well you know it, you’re not a great programmer.
    I agree with this statement. A programming language is simply some predefined human-friendly syntax which is then compiled into machine code or parsed. A good programmer should know general programming concepts. They should in theory be able to pick up any language after a quick tutorial and become more advanced after learning language specific quirks.
  9. It’s OK to write garbage code once in a while.
    I somewhat agree with this statement. Sometimes to meet a deadline or address a time sensitive issue, you may have to write ‘garbage code’. This can always be refactored at a later date but keep in a mind its much more time consuming to refactor bad than to write good code initially. Thus, writing garbage code is rarely OK. 
  10. Print statements are a valid way to debug code.
    I agree with this statement. Although many don’t like it, a quick print statement for a very small bug may be more than sufficient. However, with more complex bugs, you’ll find a debugger is basically a necessity and print statements won’t cut it. I still don’t understand why some programmers have very strong opinions about using print statements to debug small bugs.
  11. Your job is to put yourself out of work.
    I agree with this statement. Writing code that is well designed, clearly and consistently written, formatted cleanly, documented where it needs to be, builds daily as expected, checked into the repository, and appropriately versioned makes you great at your job. I also believe programmers that do this become very valuable over time.
  12. Getters and Setters are highly overused.
    I agree with this statement. Obviously this depends on the specific case but typically having a standard getter and setter for a private field means its more or less public.
  13. SQL is code. Treat it as such.
    I agree with this statement. A lot of people are careless when writing SQL statements making them inefficient, prone to security flaws or hard to understand. Treat SQL like code and design clean, clear and efficient statements.
  14. UML diagrams are highly overrated.
    I agree with this statement. I don’t believe UML diagrams are required for planning a well designed solution. They can be useful for documentation/showcasing a solution though.
  15. Readability is the most important aspect of your code.
    I agree with this statement. Readability often comes from the code itself. However, code style, variable names and comments can also contribute to the readability of your code. It would be fine to optimise some frequently used code at the cost of less readability if the changes are properly documented.
  16. XML is highly overrated.
    I somewhat agree with this statement. Depending on the scenario, using XML can be great.
  17. Software development is just a job.
    I disagree with this statement. For some people it may be. For others (like myself) its also very much a hobby. I wrote my first program and studied basic programming as a young kid. I wasn’t doing it because I was prepping for a job in 10 years, I was doing it because it was fun and interesting. I’m happy that I can do something I enjoy as a job rather than something menial.
  18. If you’re a developer, you should be able to write code.
    I agree with this statement. For obvious reasons.
  19. Design patterns are hurting good design more than they’re helping it.
    I agree with this statement.  I think trying to apply a design pattern in every scenario is problematic. Design patterns are useful and its good to be aware of them when designing solutions.
  20. Less code is better than more!
    I disagree with this statement. Sometimes less code can be better but lines of code (LOC) is a useless metric. The readability of the code is more important. I would take 1000 lines of readable, well designed code over 200 lines of garbage.

 

It looks like I agree with 17 of the 20 controversial programming opinions made.
As a graduate software engineer, I’m interested to know how my opinions will change in a couple of years when I’ve gained more real world experience.

 
No Comments

Posted in Programming

 

Discord Discriminator Farming

30 Apr 2018

This post outlines how I got the Discord discriminator that I wanted.

This process is old and is no longer required as Discord now allow you to change your discriminator via Discord Nitro.

What are discriminators?

Discord is a free modern voice and text chat application. Discord uses usernames to identify its users. However, instead of a username becoming unavailable after just 1 person uses it, Discord allows 9999 people to share the same username. It does this by using the combination of a name (unicode string) and a discriminator (4 digits) as a Discord tag.  SomeUserName#1234 is an example of a Discord tag. This is a great idea (and more services should use it) as it allows many extra people to use their first name or a common alias as their username.

Each Discord account still has an underlying Discord user ID (i.e. 356005924638224307 ) that can is echo’d if you type in  \@SomeUserName#1234 into any text channel. This is typically hidden from most users and used by developers when making plugins and bots.

Why do discriminators matter?

They don’t but just like usernames, discriminators are visible and thus can be considered to be a cosmetic name tag attached to your account. This is why people want specific Discord discriminators. Because it looks nice! User#0001  or User#1337  look a lot nicer than User#6519. For a long time, Discord has been strictly against scripts/bots which were designed to change a users Discriminator. This is because they believed the aesthetics of discriminators did not matter and their only purpose was to allow more people to share the same usernames. This statement was somewhat unfair given that almost all Discord devs had manually changed their discriminators!

Writing a Discord Discriminator Farmer

Back in mid 2017, I decided I wanted a new fancy Discord discriminator to replace my randomly generated one of 8471.

There is only one way to change your discriminator. Discord offer a name change feature in which you can change your username but not your discriminator. However, what happens if the username you want is already taken by someone with the same discriminator as you? Two users can’t have the same Discord tag. In this case, Discord changes your username (assuming all 9999 discriminator for that username aren’t taken) and then randomly generates a new discriminator for you!

This is the key behaviour that I used to write some python scripts to farm Discord discriminators.

Step 1: Gathering a list of usernames and discriminators

The first step in the process was to write a script ( get_discrim.py) to store a large number of Discord usernames and discriminators belonging to existing users. Recall that we needed to change our username to a new username that already had a user with our current discriminator. For example, if our Discord username was  SomeUserName#6513 and a user existed called  Tony#6513, we could change our username to Tony and because  Tony#6513 already exists, Discord would generate a new random Discriminator for us.

Overall, this script was fairly simple to make. We simply made a new Discord account and joined a lot of guilds (aka servers) with very high member counts. We then uses the Discord API to return a list of all members in the guild that were currently online. By joining a few massive guilds like /r/Overwatch and /r/PUBATTLEGROUNDS, we had access to over 60000+ Discord tags which meant we had an existing username for 99% of discriminators. Our script stored the results as a dict which was dumped as a pickle so our second script could use it. This file was regenerated every 15 minutes to ensure we wouldn’t get stuck (in the rare case where our dump contained no matching usernames for a particular discriminator).

Step 2: Farming them Discriminators

The second script ( farm_web.py) would authenticate with Discord, load in our pickle from our first script and begin changing usernames. However, there was a problem. Discord initially allowed you to change your username as many times as you wanted. Then they restricted username changes to once per hour. Then once per two hours. Then once per day which is what the “secret” time window was when I was testing. This was to combat people doing exactly what I did. Now, given that I wanted a very small subset of target discriminators (22 total) out of a possible 9999, this would not do. As there was no way to get around this username change time limit I was forced to use multiple accounts and change each of their usernames daily.

This worked well initially before I ran into another issue. IP rate limiting. Discord would rate limit my servers IP address causing a lot of the username change API requests to fail. I overcame this quite easily by spanning out my name changes throughout the day rather than making them all at once.

Another issue was the fact that each account needed an Authorization Token to authenticate with the API. I ended up manually fetching and storing the authorization tokens for all the accounts I used by logging into each account, filling in a captcha if one was presented and then retrieving the authorization token from the browsers local storage. As long as you did not log out, the authorization token remained valid indefinitely (this is why you can stay signed into the same Discord account forever on the same machine/browser).

Finally I was able to run my script successfully with about ~150 accounts at once. Meaning I had 150 new discriminators generated over a 24 hour period. Again, this isn’t a huge amount but it was enough to make bruteforcing feasible.

Discord Account List

Once a name changed resulted in a new random discriminator that was in my target list, that thread would end and an entry would be written to my log file to alert me.

Step 3: One final name change

Over 2 weeks later running my scripts 24/7, I finally had 1 alert letting me know that a target discriminator was found. I really liked the result so I decided I would keep it and turn off my scripts. However, the account with the final discriminator did not have the username I wanted.

The final step involved changing the username to the username that I wanted to use. This part is important, you had to ensure the username you were changing your name to DID NOT have a discriminator that matched your new discriminator. Otherwise, Discord would just give you a randomly generated discriminator. This was easy enough, all you had to do was try to add a user via the friend system and see if the friend request was send successfully indicating the account existed or not indicating the account did not exist and it was safe to proceed. For example, if my final account with my target discriminator was  RandomUserName#1337  and I wanted my tag to be  MyName#1337, I would send a friend request to  MyName#1337 to see if that tag existed. If it did not, I could proceed and get it for myself! Otherwise, you would unfortunately be out of luck.

Changes to Date

After I stopped running my scripts, Discord eventually increased the username change time window and enforced harsher IP rate limiting. Finally, they allowed users to change their discriminator via Discord Nitro although most of the good ones are probably taken by now.

Source Code

The source code for this project is available here:
https://github.com/mohammadg/Discord-Farmer

 
2 Comments

Posted in Programming

 

Making my First libGDX Game

15 Apr 2018

Recently I decided to create a simple Android game and it was much easier than I thought it would be!
I had never made an Android game before but I had made a few Android apps as well as a JOGL game which is great as it meant I had a good idea of what was happening behind the scenes.

Game development frameworks like libGDX often make it easy to create games because they handle a lot of the low level work. libGDX is more or less a wrapper over OpenGL and allows you to make raw OpenGL API calls if you so desire. Even though libGDX is very simple to use, I would still recommend making a simple game with raw OpenGL to fully grasp what is happening behind the scenes.

Picking a game to make

The first step in making a game is deciding what you actually want to make. For my first game I thought I’d stick to something simple that other developers had made tutorials on. Turns out that Flappy Bird clones are the Hello World App’s of the game development world. As I own a green cheek conure called Siavash it was only reasonable I made a clone called Flappy Siavash.

Brent Aureli’s Tutorial

I’m not going to go through the actual game making process in this post.
I ended up following this tutorial by Brent Aureli up to around part 7 before I got a hang of things and started to finish off the game myself.

Features

My game ended up having the following features:

  • Main Menu Screen (very basic with a play button)
  • Custom sprite (coloured to match my bird Siavash)
  • Obstacle crash animation (rotating into nose dive into ground)
  • Obstacle crash screen flash
  • Using cages as the obstacle
  • Number of cages active at any given time can be changed
  • Fade in/Fade out for various text/sprites/etc
  • Pause menu to pause game
  • Game over menu with option to play again
  • High score functionality (using local storage)
  • Works and tested on Android, Java, Web
  • Uses custom distance field font with fragment/vertex shader
  • Scrolling backgrounds
  • Randomised obstacles
  • Endless mode

 

Gameplay

Flappy Siavash Gameplay

 

Tips

Here are some tips which may help you when creating your own libGDX game:

  • Make sure you dispose resources (textures etc) when you are done using them or you will have memory leaks.
  • Don’t remake the same Texture etc all the time, store the Texture somewhere (in the class or put in in an asset manager and retrieve it whenever needed)
  • If possible, keep assets with widths/heights that are powers of 2. Assets seem to fail to load correctly with GWT (Google Web Toolkit) otherwise.
  • Organise assets in subfolders (shaders/images/sounds/sprites)! Don’t dump everything into the same folder.
  • When updating your states, always use dt (delta time) to scale your changes appropriately.
  • Place sprites in your word using coords relative to the cameras viewport width/height.
  • Keep a helper class with various static methods useful for performing common tasks (do the same with a debug class for debugging).

 

Source Code

The code from my project is available on Github if you are interested:
https://github.com/mohammadg/Flappy-Siavash

Play Online

Play Online At:
https://mohammadg.com/projects/flappysiavash/

Play Store App

I decided to release this app to the Google Play Store to learn about the Play Store publishing process.

Available at:
https://play.google.com/store/apps/details?id=com.mohammadg.flappysiavash

My Next Project

I am now working on a much more complicated/unique game which I plan to polish up.
This project will likely take much, much longer but I will also learn a lot along the way.

 
No Comments

Posted in Programming

 

Run Adobe Audition in the Background to Reduce your Microphone’s Background Noise

08 Apr 2017

Recently I have been looking for a way to reduce the background noise my microphone produces. I own a Blue Yeti Microphone mounted on a RODE Arm Stand and I like to keep my microphone fairly far away so its not in my face and doesn’t distract me while I record audio or play video games.

However, at this distance, the microphone unfortunately picks up a lot of background noise including computer fans, outside noises and even small things like picking up/putting down a cup of water.

Part of the Solution

To solve this I found a wonderful video by SaaiTV linked below. I have built onto this solution to make it better but the first thing you should do is follow the Youtube video tutorial and come back here to continue. Keep in mind I am using Adobe Audition CS6 and would recommend you use the same version (it will help later on in this tutorial).

The Problem

If you followed the instructions in the video and are happy with the result, you may want to keep the noise reduction effect so its always on. For recording audio and small tasks, you can simply run Adobe Audition and open your saved session and then close it when you are done. If this is all you want to do then this post won’t help you.

However, if you want to keep the noise reduction effect 24/7 so you can take advantage of it all the time, I will tell you how to run Adobe Audition when your computers boots up in the background so its out of the way.

Rest of the Solution

When you run Adobe Audition in the background keep in mind it will always be running. On my machine (which is quite good), it used 150MB of RAM and 1-3% CPU constantly.

Task Manager Adobe Audition CPU/Memory Usage

This is no issue for me at all but might reduce performance significantly on other machines.

Now, the first thing you want to do is download a program called AutoHotKey from https://autohotkey.com/

Once installed, remember the full path to the  AutoHotkey.exe executable. We will need it later.

In my case, the full path is:

Now, pick a folder on your computer to store a new AutoHotKey script file ( .ahk  file).
In this example I’ll pick:

We are going to put a new file here called  adobe_audition_microphone.ahk. Download the file I have prepared and copy and paste it in this folder: Download Link

Now, open this file using Notepad (or your favourite text editor). You should see this text:

There are a few adjustments you need to make.
Firstly, replace ADOBE_AUDITION_PATH with the path to your Adobe Audition executable. Make sure to keep the quotes around the path intact.
Mine was located at:

Next, replace SESX_PROFILE_FILE with the path to your Adobe Audition session file. This is the session file you should have saved when following the video tutorial. You can copy this file to the same folder as the .ahk  file we are modifying right now.

I called my file Microphone_Noise_Reduction.sesx and moved it to:

Finally, you will need to change  #32770 and  audition5  if you are NOT using Adobe Audition CS6. You can find the correct values to use using AutoHotKeys Window Spy tool. I will not cover that in this post but leave a comment below if you have trouble with this.

Your adobe_audition_microphone.ahk  file should now be complete.
This is what mine looks like when completed:

Make sure to save your changes before exiting your text editor.
Now, perform a little test by double clicking your  adobe_audition_microphone.ahk  file.
If everything is working, it should open up Adobe Audition, and then hide the window once its open.
It should not appear in your taskbar as a minimised window.
Make sure you see Adobe Audition running in Task Manager.

Now go to Windows Sound options, go to the Recording tab, right click on your Virtual Audio Cable Line, click properties, go to the Listen tab and press Listen to this device. You should now hear your microphone output as processed by Adobe Audition. If you do not, then you have done something wrong.

Windows Sounds Listen to this Device

 

The last step is to make our AutoHotKey script ( C:\MyAhkFolder\adobe_audition_microphone.ahk ) start with Windows so your microphone’s input is always being processed and output to your virtual audio cable.
There are plenty of tutorials on how to do this on the internet.

Here is an easy one to follow:
https://www.autohotkey.com/docs/FAQ.htm#Startup

Good luck!

 
13 Comments

Posted in Programming

 

Simple PHP File Download Script

18 Dec 2015

So I recently added a download.php script to my website so that I could force downloads of files instead of having users access them through an indexed directory or through their browser.

I found various scripts online but none of them were as clean as I’d have liked them to be so I wrote my own simple script after a bit of research.

In my setup, the download.php file sits at the root of my website and the filevault folder sites one level higher on the web server. This setup ensures users cannot hotlink to files or directly access them, the script must be used. A benefit of this is that you can add restrictions like allowing a file to be accessed by people from a particular country or by those who have a certain cookie set. If you do not have access to the directory above your websites root directory then you are forced into putting your filevault at the websites root directory.

This is the simple PHP File download script:

Usage

The following link would force the download of that_file.txt

 

Demo

You can also download the above script (using the script!):
Download download.php Script

 
8 Comments

Posted in Programming

 

How to fix your League of Legends Registry Paths (OP.GG fix)

30 Nov 2015

Amumu Sad Mummy

Artist: Awskitee

The issue

If you play League of Legends and use any third party tools (to record replays, enhance gameplay, among other things :p) then you have probably run into programs that ask for the path of your League of Legends directory. Fortunately, some programs can auto detect this using the Windows registry. However, if you have moved your League of Legends folder to another folder, reinstalled/upgraded Windows or used a registry cleaner in the past then its possible that your registry entries are corrupt. This means they either don’t exist or don’t point to the correct path.

This is a common issue for most people who cannot watch OP.GG replays as the OP.GG replay batch files rely on the RADs path to function correctly. You might have run into this error:

KR: LOL 경로를 자동으로 찾을 수 없습니다. 도움말에서 관전하기 문제 해결을 보시면 100% 해결 될 수 있습니다. 100% 해결 될 수 있으니, 채팅방에서 괜히 사서 고생해서 물어보지마세요!!!!!!!
EN: Cannot found LOL directory path for automatic. Please see our spectate help page: http://oce.op.gg/help/observer

 

OP.GG has a section on their website that can allow you to set the path manually (which will be reflected in downloaded batch files) but this must be done for each region and every now and again which is not ideal. For the OCE server, the instructions are under the League of legend.exe file can not be found header at http://oce.op.gg/help/observer/

 

A permanent solution

I decided to write a script that can fix this issue for third party applications and web apps.
Simply copy and paste the following code into a text file (.txt) using a text editor of your choice (ie notepad).
Then save the file with a .bat extension (call it anything you like such as fix.bat).
Now simply right click the file and Run as Administrator .

Finally, follow the on screen instructions and choose your system architecture (32 bit/64 bit) and enter the path to your LoL folder. If you do not know your system architecture then run the program twice and use both options.

Download Link:
LoL RADS Registry Fixer.bat

Batch script:

 
74 Comments

Posted in Programming